/** * @see ByteBuffer#wrap(byte[], int, int) */ public static IoBuffer wrap(byte[] array, int offset, int length) { return wrap(ByteBuffer.wrap(array, offset, length)); }
/** * @see ByteBuffer#allocateDirect(int) */ public static IoBuffer allocateDirect(int capacity) { return wrap(ByteBuffer.allocateDirect(capacity)); }
/** * @see ByteBuffer#allocate(int) */ public static IoBuffer allocate(int capacity) { return wrap(ByteBuffer.allocate(capacity)); }
/** * Test the getInt() method, on a buffer containing 1 incomplet int spread * in two ByteBuffers */ @Test(expected = BufferUnderflowException.class) public void testGetIntIncompletInt2BBs() { ByteBuffer bb1 = ByteBuffer.allocate(1); bb1.put((byte) 0x01); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(2); bb2.put(new byte[] { 0x02, 0x03 }); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); ioBuffer.getInt(); }
/** * Test the position method with a value above the buffer size */ @Test(expected = IllegalArgumentException.class) public void testPositionAboveValue() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("3456".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(4); bb3.put("789".getBytes()); bb3.flip(); // The resulting buffer will be seen as "0123456789" IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3); ioBuffer.position(11); }
/** * Test the position method with a negative value */ @Test(expected = IllegalArgumentException.class) public void testPositionNegativeValue() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("0123".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("4567".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(4); bb3.put("89".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3); ioBuffer.position(-1); }
/** * Test the getInt() method, on a buffer containing 1 int spread in two * ByteBuffers */ @Test public void testGetInt1Int2BBs() { ByteBuffer bb1 = ByteBuffer.allocate(1); bb1.put((byte) 0x01); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(3); bb2.put(new byte[] { 0x02, 0x03, 0x04 }); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); assertEquals(0x01020304, ioBuffer.getInt()); }
@Test public void testEquals() { String h = "Hello"; String w = "World"; IoBuffer hw1b = IoBuffer.wrap((h + w).getBytes()); IoBuffer wh1b = IoBuffer.wrap((w + h).getBytes()); IoBuffer hw2b = IoBuffer.newInstance(); hw2b.add(ByteBuffer.wrap(h.getBytes())); hw2b.add(ByteBuffer.wrap(w.getBytes())); assertEquals(hw2b, hw1b); Assert.assertThat(wh1b, is(not(hw1b))); } }
/** * Test the getInt() method, on a buffer containing 2 ints in two * ByteBuffers */ @Test public void testGetInt2Ints2BBs() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.putInt(12345); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.putInt(67890); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); assertEquals(12345, ioBuffer.getInt()); assertEquals(67890, ioBuffer.getInt()); }
/** * Test the getInt() method, on a buffer containing 2 ints in one ByteBuffer */ @Test public void testGetInt2IntsOneBB() { ByteBuffer bb = ByteBuffer.allocate(8); bb.putInt(12345); bb.putInt(67890); bb.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb); assertEquals(12345, ioBuffer.getInt()); assertEquals(67890, ioBuffer.getInt()); }
@Test public void testInputStreamGetByte() throws IOException { String hw = "HelloWorld"; IoBuffer bb = IoBuffer.wrap(hw.getBytes()); InputStream is = bb.asInputStream(); for (int i = 0; i < 10; i++) { assertEquals(i, bb.position()); assertEquals(hw.getBytes()[i], is.read()); } assertEquals(-1, is.read()); }
/** * 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 public void testSamples() { Map<Integer, ByteBuffer> samples = getEncodingSamples(); for (Integer val : samples.keySet()) { assertEquals(samples.get(val), encoder.encode(val)); try { assertEquals(val, decoder.decode(IoBuffer.wrap(samples.get(val)))); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } }
@Test public void testSizedValues() { for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) { ByteBuffer buffer = encoder.encode(value); try { assertEquals(value, decoder.decode(IoBuffer.wrap(buffer)).intValue()); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } }
@Test public void testOverflow() { for (ByteBuffer buffer : getIllegalBuffers()) { try { decoder.decode(IoBuffer.wrap(buffer)); fail("Should throw an overflow exception"); } catch (ProtocolDecoderException e) { // fine } } }
@Test public void testInputStreamGetByteArray() throws IOException { String hw = "HelloWorld"; IoBuffer bb = IoBuffer.wrap(hw.getBytes()); InputStream is = bb.asInputStream(); byte array[] = new byte[15]; assertEquals(5, is.read(array, 0, 5)); assertEquals(5, bb.position()); assertEquals(5, is.read(array, 5, 10)); assertEquals(10, bb.position()); for (int i = 0; i < 10; i++) { assertEquals(hw.getBytes()[i], array[i]); } }
@Test public void testEncodedSize() throws Exception { IoBufferDecoder<T> decoder = getDecoder(); ByteBufferEncoder<T> encoder = getEncoder(); for (T object : getObjects()) { int size = encoder.getEncodedSize(object); ByteBuffer out = ByteBuffer.allocate(size); encoder.writeTo(object, out); assertEquals(size, out.position()); out.rewind(); assertEquals(object, decoder.decode(IoBuffer.wrap(out))); } } }
@Test public void testSerialization() throws Exception { IoBufferDecoder<T> decoder = getDecoder(); ByteBufferEncoder<T> encoder = getEncoder(); for (T object : getObjects()) { assertEquals(object, decoder.decode(IoBuffer.wrap(encoder.encode(object)))); } }
@Test public void testFloat() { for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putFloat(-0.68f).rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); assertEquals(5, ioBuffer.capacity()); ioBuffer.extend(3); ioBuffer.position(4); assertEquals(8, ioBuffer.capacity()); ioBuffer.putFloat(3.14f); ioBuffer.rewind(); assertEquals(-0.68f, ioBuffer.getFloat(), 0.001f); assertEquals(3.14f, ioBuffer.getFloat(), 0.001f); ioBuffer.rewind(); ioBuffer.putFloat(2, -12.34f); assertEquals(-12.34f, ioBuffer.getFloat(2), 0.001f); } }
@Test public void testTruncatedValues() { for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) { IoBuffer buffer = IoBuffer.wrap(encoder.encode(value)); for (int i = 0; i < buffer.remaining(); i++) { IoBuffer partialBuffer = buffer.slice(); partialBuffer.limit(partialBuffer.position() + i); try { assertNull(decoder.decode(partialBuffer)); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } } }