@Override public T deserialize(byte[] message) throws IOException { return coder.decode(new ByteArrayInputStream(message), Coder.Context.NESTED); }
@Override public T decode(InputStream inputStream) throws CoderException, IOException { if (internalAvroCoder == null) { @SuppressWarnings("unchecked") AvroCoder<IndexedRecord> tCoder = (AvroCoder<IndexedRecord>) (AvroCoder<? extends IndexedRecord>) AvroCoder .of(getSchema()); internalAvroCoder = tCoder; } return (T) internalAvroCoder.decode(inputStream); }
@Override public T decode(InputStream inputStream) throws CoderException, IOException { if (internalAvroCoder == null) { @SuppressWarnings("unchecked") AvroCoder<IndexedRecord> tCoder = (AvroCoder<IndexedRecord>) (AvroCoder<? extends IndexedRecord>) AvroCoder .of(getSchema()); internalAvroCoder = tCoder; } return (T) internalAvroCoder.decode(inputStream); }
@Test public void testEncodingNotBuffered() throws Exception { // This test ensures that the coder doesn't read ahead and buffer data. // Reading ahead causes a problem if the stream consists of records of different // types. Pojo before = new Pojo("Hello", 42); AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class); SerializableCoder<Integer> intCoder = SerializableCoder.of(Integer.class); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Context context = Context.NESTED; coder.encode(before, outStream, context); intCoder.encode(10, outStream, context); ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray()); Pojo after = coder.decode(inStream, context); Assert.assertEquals(before, after); Integer intAfter = intCoder.decode(inStream, context); Assert.assertEquals(Integer.valueOf(10), intAfter); }