/** * Creates a {@link JsonDecoder} using the InputStrim provided for reading * data that conforms to the Schema provided. * <p/> * * @param schema * The Schema for data read from this JsonEncoder. Cannot be null. * @param input * The InputStream to read from. Cannot be null. * @return A JsonEncoder configured with <i>input</i> and <i>schema</i> * @throws IOException */ public JsonDecoder jsonDecoder(Schema schema, InputStream input) throws IOException { return new JsonDecoder(schema, input); }
@Override public long readArrayStart() throws IOException { advance(Symbol.ARRAY_START); if (in.getCurrentToken() == JsonToken.START_ARRAY) { in.nextToken(); return doArrayNext(); } else { throw error("array-start"); } }
@Override public void readFixed(byte[] bytes, int start, int len) throws IOException { checkFixed(len); if (in.getCurrentToken() == JsonToken.VALUE_STRING) { byte[] result = readByteArray(); in.nextToken(); if (result.length != len) { throw new AvroTypeException("Expected fixed length " + len + ", but got" + result.length); } System.arraycopy(result, 0, bytes, start, len); } else { throw error("fixed"); } }
@Override public void skipFixed(int length) throws IOException { checkFixed(length); doSkipFixed(length); }
@Override public long arrayNext() throws IOException { advance(Symbol.ITEM_END); return doArrayNext(); }
@Override public ByteBuffer readBytes(ByteBuffer old) throws IOException { advance(Symbol.BYTES); if (in.getCurrentToken() == JsonToken.VALUE_STRING) { byte[] result = readByteArray(); in.nextToken(); return ByteBuffer.wrap(result); } else { throw error("bytes"); } }
private void doSkipFixed(int length) throws IOException { if (in.getCurrentToken() == JsonToken.VALUE_STRING) { byte[] result = readByteArray(); in.nextToken(); if (result.length != length) { throw new AvroTypeException("Expected fixed length " + length + ", but got" + result.length); } } else { throw error("fixed"); } }
@Override protected void skipFixed() throws IOException { advance(Symbol.FIXED); Symbol.IntCheckAction top = (Symbol.IntCheckAction) parser.popSymbol(); doSkipFixed(top.size); }
private JsonDecoder(Symbol root, InputStream in) throws IOException { super(root); configure(in); }
byte[] bytes = encoder.toBytes(map); KeyedMessage<String, byte[]> message =new KeyedMessage<String, byte[]>(this.topic, bytes); JsonDecoder decoder = new JsonDecoder(null); Map map = (Map) decoder.fromBytes(it.next().message());
private void checkFixed(int size) throws IOException { advance(Symbol.FIXED); Symbol.IntCheckAction top = (Symbol.IntCheckAction) parser.popSymbol(); if (size != top.size) { throw new AvroTypeException( "Incorrect length for fixed binary: expected " + top.size + " but received " + size + " bytes."); } }
@Override public ByteBuffer readBytes(ByteBuffer old) throws IOException { advance(Symbol.BYTES); if (in.getCurrentToken() == JsonToken.VALUE_STRING) { byte[] result = readByteArray(); in.nextToken(); return ByteBuffer.wrap(result); } else { throw error("bytes"); } }
private void doSkipFixed(int length) throws IOException { if (in.getCurrentToken() == JsonToken.VALUE_STRING) { byte[] result = readByteArray(); in.nextToken(); if (result.length != length) { throw new AvroTypeException("Expected fixed length " + length + ", but got" + result.length); } } else { throw error("fixed"); } }
@Override public void skipFixed(int length) throws IOException { checkFixed(length); doSkipFixed(length); }