Java Code Examples for com.google.protobuf.CodedInputStream#getBytesUntilLimit()

The following examples show how to use com.google.protobuf.CodedInputStream#getBytesUntilLimit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <T> T decodeSet(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        StringBuilder vals = new StringBuilder();
        while (inputStream.getBytesUntilLimit() > 0) {
            if (vals.length() > 0) {
                vals.append(",");
            }
            long valLen = inputStream.readUInt64();
            // TODO: charset
            vals.append(new String(inputStream.readRawBytes((int) valLen)));
        }
        // TODO: charset mess here
        byte[] buf = vals.toString().getBytes();
        return vf.createFromBytes(buf, 0, buf.length);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 2
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@Override
public <T> T decodeSet(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        StringBuilder vals = new StringBuilder();
        while (inputStream.getBytesUntilLimit() > 0) {
            if (vals.length() > 0) {
                vals.append(",");
            }
            long valLen = inputStream.readUInt64();
            // TODO: charset
            vals.append(new String(inputStream.readRawBytes((int) valLen)));
        }
        // TODO: charset mess here
        byte[] buf = vals.toString().getBytes();
        return vf.createFromBytes(buf, 0, buf.length);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 3
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        int year = (int) inputStream.readUInt64();
        int month = (int) inputStream.readUInt64();
        int day = (int) inputStream.readUInt64();

        // do we have a time too?
        if (inputStream.getBytesUntilLimit() > 0) {
            int hours = 0;
            int minutes = 0;
            int seconds = 0;

            int nanos = 0;

            if (!inputStream.isAtEnd()) {
                hours = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    minutes = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        seconds = (int) inputStream.readInt64();
                        if (!inputStream.isAtEnd()) {
                            nanos = 1000 * (int) inputStream.readInt64();
                        }
                    }
                }
            }

            return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
        }
        return vf.createFromDate(year, month, day);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 4
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeByteArray(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // c.f. Streaming_command_delegate::get_string()
        int size = inputStream.getBytesUntilLimit();
        size--; // for null terminator
        return vf.createFromBytes(inputStream.readRawBytes(size), 0, size);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 5
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        int year = (int) inputStream.readUInt64();
        int month = (int) inputStream.readUInt64();
        int day = (int) inputStream.readUInt64();

        // do we have a time too?
        if (inputStream.getBytesUntilLimit() > 0) {
            int hours = 0;
            int minutes = 0;
            int seconds = 0;

            int nanos = 0;

            if (!inputStream.isAtEnd()) {
                hours = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    minutes = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        seconds = (int) inputStream.readInt64();
                        if (!inputStream.isAtEnd()) {
                            nanos = 1000 * (int) inputStream.readInt64();
                        }
                    }
                }
            }

            return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
        }
        return vf.createFromDate(year, month, day);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 6
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public <T> T decodeByteArray(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // c.f. Streaming_command_delegate::get_string()
        int size = inputStream.getBytesUntilLimit();
        size--; // for null terminator
        return vf.createFromBytes(inputStream.readRawBytes(size), 0, size);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 7
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException
                    .shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch (sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        unscaledString.clear(); // reset position
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 8
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException
                    .shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch (sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        unscaledString.clear(); // reset position
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}