@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; } }
/** * Test the array method for a IoBuffer containing one ByteBuffer not * initialized */ @Test public void testArrayByteBufferNotInitialized() { ByteBuffer bb = ByteBuffer.allocate(3); IoBuffer ioBuffer = IoBuffer.wrap(bb); byte[] array = ioBuffer.array(); assertNotNull(array); assertEquals(3, array.length); assertTrue(Arrays.equals(new byte[] { 0x00, 0x00, 0x00 }, array)); }
/** * Test the array method for a IoBuffer containing one empty ByteBuffer */ @Test public void testArrayEmptyByteBuffer() { IoBuffer ioBuffer = IoBuffer.newInstance(); byte[] array = ioBuffer.array(); assertNotNull(array); assertEquals(0, array.length); assertTrue(Arrays.equals(new byte[] {}, array)); }
/** * Test the array method for a IoBuffer containing one ByteBuffer (cases 2 * and 3) */ @Test public void testArrayOneByteBuffer() { ByteBuffer bb1 = ByteBuffer.allocate(5); IoBuffer ioBuffer = IoBuffer.wrap(bb1); // Empty buffer first byte[] array = ioBuffer.array(); assertNotNull(array); assertEquals(5, array.length); assertTrue(Arrays.equals(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00 }, array)); // Buffer with data bb1.put("012".getBytes()); bb1.flip(); ioBuffer = IoBuffer.wrap(bb1); assertNotNull(array); assertEquals(5, array.length); assertTrue(Arrays.equals(new byte[] { '0', '1', '2', 0, 0 }, array)); }