Data { public byte[] getBuffer() { int length = getUnsignedInt(); if (pointer + length > data.length) { throw new IndexOutOfBoundsException("Not enough data"); } if (length == 0) { return new byte[0]; } if (length == -1) { return null; } byte[] ret = Helper.copyOfRange(data, pointer, pointer + length); pointer += length; return ret; } Data(byte[] data); int getUnsignedInt(); int getByte(); char getChar(); long getLongInteger(); String getString(); byte[] getBuffer(); String getPointer(); long getTime(); Hashtable getHashtable(); Hdata getHdata(); Info getInfo(); Infolist getInfolist(); Array getArray(); RelayObject getObject(); byte[] getByteArray(); boolean empty();  }
@Test public void testGetBuffer() { byte test[] = new byte[1024]; for(int i=0;i<test.length;i++) { test[i] = (byte) (i%255); } ByteBuffer bb = ByteBuffer.allocate(1028); bb.order(ByteOrder.BIG_ENDIAN); bb.putInt(test.length); bb.put(test); Data d = new Data(bb.array()); byte[] b = d.getBuffer(); for(int i=0;i<test.length;i++) { assertEquals(test[i], b[i]); } }
@Test public void testGetBufferEmpty() { ByteBuffer bb = ByteBuffer.allocate(6); bb.order(ByteOrder.BIG_ENDIAN); bb.putInt(0); Data d = new Data(bb.array()); byte[] b = d.getBuffer(); assertEquals(0, b.length); }
@Test public void testGetBufferNull() { ByteBuffer bb = ByteBuffer.allocate(6); bb.order(ByteOrder.BIG_ENDIAN); bb.putInt(-1); Data d = new Data(bb.array()); byte[] b = d.getBuffer(); assertEquals(null, b); }
@Test(expected = IndexOutOfBoundsException.class) public void testGetBufferException() { ByteBuffer bb = ByteBuffer.allocate(4); bb.order(ByteOrder.BIG_ENDIAN); bb.putInt(bb.capacity()); Data d = new Data(bb.array()); d.getBuffer(); }