@Override protected final void readInternal(Decoder d) throws IOException { for (int i = 0; i < count; i++) { reader.read(null, d); } }
String asString = (String) read(null, f.schema(), in); accessor.set(record, asString == null ? null
@Override public T decode(byte[] bytes) { try { BinaryDecoder decoderFromCache = decoders.get(); BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(bytes, decoderFromCache); if (decoderFromCache == null) { decoders.set(decoder); } return reader.read(null, DecoderFactory.get().binaryDecoder(bytes, decoder)); } catch (IOException e) { throw new SchemaSerializationException(e); } }
String asString = (String) read(null, f.schema(), in); accessor.set(record, asString == null ? null
public T toObject(byte[] bytes) { Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, null); ReflectDatumReader<T> reader = null; try { reader = new ReflectDatumReader<T>(clazz); return reader.read(null, decoder); } catch(IOException e) { throw new SerializationException(e); } } }
result = reader.read(reuse, decoder);
void checkReadWrite(Object object, Schema s) throws Exception { ReflectDatumWriter<Object> writer = new ReflectDatumWriter<>(s); ByteArrayOutputStream out = new ByteArrayOutputStream(); writer.write(object, factory.directBinaryEncoder(out, null)); ReflectDatumReader<Object> reader = new ReflectDatumReader<>(s); Object after = reader.read(null, DecoderFactory.get().binaryDecoder( out.toByteArray(), null)); assertEquals(object, after); // check reflective setField works for records if (s.getType().equals(Schema.Type.RECORD)) { Object copy = object.getClass().newInstance(); for (Field f : s.getFields()) { Object val = ReflectData.get().getField(object, f.name(), f.pos()); ReflectData.get().setField(copy, f.name(), f.pos(), val); } assertEquals("setField", object, copy); } }
@Test public void testNew() throws IOException { ByteBuffer payload = ByteBuffer.allocateDirect(8 * 1024); for (int i = 0; i < 500; i++) { payload.putInt(1); } payload.flip(); ByteBufferRecord bbr = new ByteBufferRecord(); bbr.setPayload(payload); bbr.setTp(TypeEnum.b); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ReflectDatumWriter<ByteBufferRecord> writer = new ReflectDatumWriter<ByteBufferRecord>(ByteBufferRecord.class); BinaryEncoder avroEncoder = EncoderFactory.get().blockingBinaryEncoder(outputStream, null); writer.write(bbr, avroEncoder); avroEncoder.flush(); byte[] bytes = outputStream.toByteArray(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ReflectDatumReader<ByteBufferRecord> datumReader = new ReflectDatumReader<ByteBufferRecord>(ByteBufferRecord.class); BinaryDecoder avroDecoder = DecoderFactory.get().binaryDecoder(inputStream, null); ByteBufferRecord deserialized = datumReader.read(null, avroDecoder); Assert.assertEquals(bbr, deserialized); }
@Test public void testAvroEncodeIO() throws IOException { Schema schm = ReflectData.get().getSchema(AvroEncRecord.class); ReflectDatumWriter<AvroEncRecord> writer = new ReflectDatumWriter<>(schm); ByteArrayOutputStream out = new ByteArrayOutputStream(); AvroEncRecord record = new AvroEncRecord(); record.date = new java.util.Date(948833323L); writer.write(record, factory.directBinaryEncoder(out, null)); ReflectDatumReader<AvroEncRecord> reader = new ReflectDatumReader<>(schm); AvroEncRecord decoded = reader.read(new AvroEncRecord(), DecoderFactory.get().binaryDecoder( out.toByteArray(), null)); assertEquals(record, decoded); }
private static void checkBinary(ReflectData reflectData, Schema schema, Object datum, boolean equals, boolean blocking) throws IOException { ReflectDatumWriter<Object> writer = new ReflectDatumWriter<>(schema); ByteArrayOutputStream out = new ByteArrayOutputStream(); if (!blocking) { writer.write(datum, EncoderFactory.get().directBinaryEncoder(out, null)); } else { writer.write(datum, new EncoderFactory().configureBlockSize(64) .blockingBinaryEncoder(out, null)); } writer.write(datum, EncoderFactory.get().directBinaryEncoder(out, null)); byte[] data = out.toByteArray(); ReflectDatumReader<Object> reader = new ReflectDatumReader<>(schema); Object decoded = reader.read(null, DecoderFactory.get().binaryDecoder(data, null)); assertEquals(0, reflectData.compare(datum, decoded, schema, equals)); }
@Test public void testRead_PojoWithList() throws IOException { PojoWithList pojoWithList = new PojoWithList(); pojoWithList.setId(42); pojoWithList.setRelatedIds(Arrays.asList(1, 2, 3)); byte[] serializedBytes = serializeWithReflectDatumWriter(pojoWithList, PojoWithList.class); Decoder decoder = DecoderFactory.get().binaryDecoder(serializedBytes, null); ReflectDatumReader<PojoWithList> reflectDatumReader = new ReflectDatumReader<>( PojoWithList.class); PojoWithList deserialized = new PojoWithList(); reflectDatumReader.read(deserialized, decoder); assertEquals(pojoWithList, deserialized); }
@Test public void testRecordIO() throws IOException { Schema schm = ReflectData.get().getSchema(SampleRecord.class); ReflectDatumWriter<SampleRecord> writer = new ReflectDatumWriter<>(schm); ByteArrayOutputStream out = new ByteArrayOutputStream(); SampleRecord record = new SampleRecord(); record.x = 5; record.y = 10; writer.write(record, factory.directBinaryEncoder(out, null)); ReflectDatumReader<SampleRecord> reader = new ReflectDatumReader<>(schm); SampleRecord decoded = reader.read(null, DecoderFactory.get().binaryDecoder( out.toByteArray(), null)); assertEquals(record, decoded); }
@Test public void testRecordWithNullIO() throws IOException { ReflectData reflectData = ReflectData.AllowNull.get(); Schema schm = reflectData.getSchema(AnotherSampleRecord.class); ReflectDatumWriter<AnotherSampleRecord> writer = new ReflectDatumWriter<>(schm); ByteArrayOutputStream out = new ByteArrayOutputStream(); // keep record.a null and see if that works Encoder e = factory.directBinaryEncoder(out, null); AnotherSampleRecord a = new AnotherSampleRecord(); writer.write(a, e); AnotherSampleRecord b = new AnotherSampleRecord(10); writer.write(b, e); e.flush(); ReflectDatumReader<AnotherSampleRecord> reader = new ReflectDatumReader<>(schm); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Decoder d = DecoderFactory.get().binaryDecoder(in, null); AnotherSampleRecord decoded = reader.read(null, d); assertEquals(a, decoded); decoded = reader.read(null, d); assertEquals(b, decoded); }
new ReflectDatumReader<>(schm); multipleAnnotationRecord decoded = reader.read(new multipleAnnotationRecord(), DecoderFactory.get().binaryDecoder( out.toByteArray(), null)); assertTrue(decoded.i1 == null);
@Test public void testRead_PojoWithArray() throws IOException { PojoWithArray pojoWithArray = new PojoWithArray(); pojoWithArray.setId(42); pojoWithArray.setRelatedIds(new int[] { 1, 2, 3 }); byte[] serializedBytes = serializeWithReflectDatumWriter(pojoWithArray, PojoWithArray.class); Decoder decoder = DecoderFactory.get().binaryDecoder(serializedBytes, null); ReflectDatumReader<PojoWithArray> reflectDatumReader = new ReflectDatumReader<>( PojoWithArray.class); PojoWithArray deserialized = new PojoWithArray(); reflectDatumReader.read(deserialized, decoder); assertEquals(pojoWithArray, deserialized); }
@Override public T decode(IoBuffer input) { BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(input.array(), null); ReflectDatumReader<T> reader = new ReflectDatumReader<T>(schema); T result = null; try { result = reader.read(null, binaryDecoder); }catch (IOException ioEx) { LOG.error("Error while decoding", ioEx); throw new ProtocolDecoderException(ioEx.getMessage()); } return result; } }
@Override public T decode(byte[] data) { ByteArrayInputStream bais = new ByteArrayInputStream(data); try { JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, bais); return reader.read(null, jsonDecoder); } catch (IOException e) { throw new IllegalArgumentException(e); } } }
@Override public T deserialize(T reuse, DataInputView source) throws IOException { checkAvroInitialized(); this.decoder.setIn(source); return this.reader.read(reuse, this.decoder); }
@Override public void read(DataInput in) throws IOException { // the null flag if (in.readBoolean()) { DataInputDecoder decoder = getDecoder(); decoder.setIn(in); datum(getReader().read(datum(), decoder)); } }
@Override public void copy(DataInputView source, DataOutputView target) throws IOException { checkAvroInitialized(); if (this.deepCopyInstance == null) { this.deepCopyInstance = InstantiationUtil.instantiate(type, Object.class); } this.decoder.setIn(source); this.encoder.setOut(target); T tmp = this.reader.read(this.deepCopyInstance, this.decoder); this.writer.write(tmp, this.encoder); }