Data { public String getPointer() { int length = getByte(); if (pointer + length > data.length) { throw new IndexOutOfBoundsException("Not enough data"); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append(getChar()); } if (length == 1 && Long.parseLong(sb.toString().toUpperCase(Locale.ENGLISH), 16) == 0) { return "0x0"; } return "0x" + sb.toString(); } 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 testGetPointer() throws UnsupportedEncodingException { long testcases[] = new long[] { 0x1a2b3c4d5L, 0x0, 0x11111, 0xdeadbeef, }; for (long t: testcases) { String test = String.format("%x",t); ByteBuffer bb = ByteBuffer.allocate(270); bb.order(ByteOrder.BIG_ENDIAN); bb.put((byte)(test.length() & 0xFF)); bb.put(test.getBytes("US-ASCII")); Data d = new Data(bb.array()); String p = d.getPointer(); assertEquals(p, "0x" + test); } }
@Test(expected = IndexOutOfBoundsException.class) public void testGetPointerException() { Data d = new Data(new byte[]{5}); d.getPointer(); }