@Override public boolean readBool() throws IOException { final int size = buffer[offset++] | (buffer[offset++] << 8); if (size != 1) throw new ProtostuffException("Not a valid kvp boolean"); return buffer[offset++] != 0x30; }
@Override public <T> T mergeObject(T value, Schema<T> schema) throws IOException { throw new ProtostuffException("Unsupported."); }
@Override public byte[] readByteArray() throws IOException { final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return ByteString.EMPTY_BYTE_ARRAY; if (offset + size > limit) throw new ProtostuffException("Misreported size."); byte[] data = new byte[size]; System.arraycopy(buffer, offset, data, 0, size); offset += size; return data; }
@Override public void readBytes(final ByteBuffer bb) throws IOException { final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) bb.put(ByteString.EMPTY_BYTE_ARRAY); if (offset + size > limit) throw new ProtostuffException("Misreported size."); bb.put(buffer, offset, size); offset += size; }
@Override public boolean readBool() throws IOException { if (offset + 3 > limit && !readable(3)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (size != 1) throw new ProtostuffException("Invalid kvp boolean"); return buffer[offset++] != 0x30; }
private byte[] fill(final byte[] data, int dataOffset, final int len) throws IOException { final int existing = limit - offset; int toRead = len - existing, read = 0; if (existing != 0) { // copy existing System.arraycopy(buffer, offset, data, dataOffset, existing); dataOffset += existing; } // reset offset = 0; limit = 0; do { read = in.read(data, dataOffset, toRead); if (read == -1) throw new ProtostuffException("Truncated message."); dataOffset += read; toRead -= read; } while (toRead > 0); return data; }
@Override public int readInt32() throws IOException { final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return 0; if (offset + size > limit) throw new ProtostuffException("Misreported size."); final int number = parseInt(buffer, offset, size, 10); offset += size; return number; }
@Override public long readInt64() throws IOException { final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return 0; if (offset + size > limit) throw new ProtostuffException("Misreported size."); final long number = parseLong(buffer, offset, size, 10); offset += size; return number; }
@Override public String readString() throws IOException { final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return ByteString.EMPTY_STRING; if (offset + size > limit) throw new ProtostuffException("Misreported size."); final String str = STRING.deser(buffer, offset, size); offset += size; return str; }
@Override public <T> void handleUnknownField(int fieldNumber, Schema<T> schema) throws IOException { if (offset + 2 > limit && !readable(2)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (offset + size > limit) { int toRead = size - (limit - offset), read = 0; // read until toRead do { read = in.read(buffer, 0, buffer.length); if (read == -1) throw new ProtostuffException("Truncated message."); toRead -= read; } while (toRead > 0); offset = read - (-toRead); limit = read; return; } offset += size; }
@Override public byte[] readByteArray() throws IOException { if (offset + 2 > limit && !readable(2)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return ByteString.EMPTY_BYTE_ARRAY; if (size > MAX_VALUE_SIZE) throw new ProtostuffException("Exceeded kvp max value size."); if (offset + size > limit) return fill(new byte[size], 0, size); final byte[] data = new byte[size]; System.arraycopy(buffer, offset, data, 0, size); offset += size; return data; }
static Class<?> getArrayClass(Input input, Schema<?> schema, final Class<?> componentType) throws IOException { if (input.readFieldNumber(schema) != ID_ARRAY_DIMENSION) throw new ProtostuffException("Corrupt input."); final int dimensions = input.readUInt32(); // TODO is there another way (reflection) to obtain an array class? if (dimensions == 1) return Array.newInstance(componentType, 0).getClass(); final int[] arg = new int[dimensions]; arg[0] = 0; return Array.newInstance(componentType, arg).getClass(); }
static Class<?> getArrayClass(Input input, Schema<?> schema, final Class<?> componentType) throws IOException { if (input.readFieldNumber(schema) != ID_ARRAY_DIMENSION) throw new ProtostuffException("Corrupt input."); final int dimensions = input.readUInt32(); // TODO is there another way (reflection) to obtain an array class? if (dimensions == 1) return Array.newInstance(componentType, 0).getClass(); final int[] arg = new int[dimensions]; arg[0] = 0; return Array.newInstance(componentType, arg).getClass(); }
@Override public void readBytes(final ByteBuffer bb) throws IOException { if (offset + 2 > limit && !readable(2)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) { bb.put(ByteString.EMPTY_BYTE_ARRAY); return; } if (size > MAX_VALUE_SIZE) throw new ProtostuffException("Exceeded kvp max value size."); if (offset + size > limit) { fill(bb.array(), 0, size); } bb.put(buffer, offset, size); offset += size; }
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe, Input input, Output output, IdStrategy strategy) throws IOException { if (ID_ENUM != input.readFieldNumber(pipeSchema.wrappedSchema)) throw new ProtostuffException("Corrupt input."); strategy.transferEnumId(input, output, ID_ENUM); if (ID_ENUM_VALUE != input.readFieldNumber(pipeSchema.wrappedSchema)) throw new ProtostuffException("Corrupt input."); EnumIO.transfer(pipe, input, output, 1, false, strategy); if (0 != input.readFieldNumber(pipeSchema.wrappedSchema)) throw new ProtostuffException("Corrupt input."); }
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe, Input input, Output output, IdStrategy strategy) throws IOException { if (ID_ENUM != input.readFieldNumber(pipeSchema.wrappedSchema)) throw new ProtostuffException("Corrupt input."); strategy.transferEnumId(input, output, ID_ENUM); if (ID_ENUM_VALUE != input.readFieldNumber(pipeSchema.wrappedSchema)) throw new ProtostuffException("Corrupt input."); EnumIO.transfer(pipe, input, output, 1, false, strategy); if (0 != input.readFieldNumber(pipeSchema.wrappedSchema)) throw new ProtostuffException("Corrupt input."); }
@Override public int readInt32() throws IOException { if (offset + 2 > limit && !readable(2)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return 0; if (offset + size > limit && !readable(size)) throw new ProtostuffException("Truncated message."); final int number = parseInt(buffer, offset, size, 10); offset += size; return number; }
@Override public long readInt64() throws IOException { if (offset + 2 > limit && !readable(2)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return 0; if (offset + size > limit && !readable(size)) throw new ProtostuffException("Truncated message."); final long number = parseLong(buffer, offset, size, 10); offset += size; return number; }
static Object readObjectFrom(Input input, Schema<?> schema, Object owner, IdStrategy strategy) throws IOException { final int number = input.readFieldNumber(schema); if (number != ID_THROWABLE) throw new ProtostuffException("Corrupt input."); return readObjectFrom(input, schema, owner, strategy, number); }
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe, Input input, Output output, IdStrategy strategy) throws IOException { final int number = input.readFieldNumber(pipeSchema.wrappedSchema); if (number != ID_POJO) throw new ProtostuffException("Corrupt input."); transferObject(pipeSchema, pipe, input, output, strategy, number); }