BigByteBuffer implements Closeable { public byte get(long byteIdx) throws ArrayIndexOutOfBoundsException { if (byteIdx < 0 || byteIdx >= totalSize) throw new ArrayIndexOutOfBoundsException("Tried to access index " + byteIdx + " on buffer of size " + totalSize); int bufIdx = (int) (byteIdx / shardSize); int idx = (int) (byteIdx % shardSize); return byteBuffers[bufIdx].get(idx); } BigByteBuffer(FileChannel channel, MapMode mode, Consumer<MappedByteBuffer> initializer);  BigByteBuffer(FileChannel channel, MapMode mode, Consumer<MappedByteBuffer> initializer,
      int maxSingleShardSize);  BigByteBuffer(byte[] bytes);  BigByteBuffer(ByteBuffer[] byteBuffers); protected  BigByteBuffer(ByteBuffer[] byteBuffers, int bufferSize); byte get(long byteIdx); int get(long byteIdx, byte[] target, int targetOffset, int length); long size(); @Override void close(); InputStream createInputStream(); InputStream createPartialInputStream(long firstIdx, long lastIdxExclusive);  }
@Test public void singleShardTest() throws IOException { try (BigByteBuffer buf = new BigByteBuffer(new byte[] { 1, 2, 3, 4, 5 })) { Assert.assertEquals(1, buf.get(0)); Assert.assertEquals(5, buf.get(4)); byte[] tmp = new byte[2]; buf.get(0, tmp, 0, 2); Assert.assertEquals(new byte[] { 1, 2 }, tmp); tmp = new byte[3]; tmp[0] = 0; buf.get(0, tmp, 1, 2); Assert.assertEquals(new byte[] { 0, 1, 2 }, tmp); tmp = new byte[3]; buf.get(2, tmp, 0, 3); Assert.assertEquals(new byte[] { 3, 4, 5 }, tmp); tmp = new byte[5]; buf.get(0, tmp, 0, 5); Assert.assertEquals(new byte[] { 1, 2, 3, 4, 5 }, tmp); tmp = new byte[1]; buf.get(4, tmp, 0, 1); Assert.assertEquals(new byte[] { 5 }, tmp); } }
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) public void outOfBoundsTest() throws IOException { try (BigByteBuffer buf = new BigByteBuffer(new byte[] { 1 })) { buf.get(1); } }
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) public void negativeTest() throws IOException { try (BigByteBuffer buf = new BigByteBuffer(new byte[] { 1, 2 })) { buf.get(-1); } }
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) public void wrongTargetIndexTest() throws IOException { try (BigByteBuffer buf = new BigByteBuffer(new byte[] { 1, 2 })) { byte[] tmp = new byte[2]; buf.get(0, tmp, 1, 2); } }
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) public void negativeLengthTest() throws IOException { try (BigByteBuffer buf = new BigByteBuffer(new byte[] { 1, 2 })) { byte[] tmp = new byte[1]; buf.get(0, tmp, 1, -1); } }
@Test public void twoShardTest() throws IOException { ByteBuffer buf1 = ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5 }); ByteBuffer buf2 = ByteBuffer.wrap(new byte[] { 11, 12, 13, 14, 15 }); try (BigByteBuffer buf = new BigByteBuffer(new ByteBuffer[] { buf1, buf2 })) { Assert.assertEquals(1, buf.get(0)); Assert.assertEquals(5, buf.get(4)); Assert.assertEquals(11, buf.get(5)); Assert.assertEquals(15, buf.get(9)); byte[] tmp = new byte[5]; buf.get(0, tmp, 0, 5); Assert.assertEquals(new byte[] { 1, 2, 3, 4, 5 }, tmp); tmp = new byte[5]; buf.get(5, tmp, 0, 5); Assert.assertEquals(new byte[] { 11, 12, 13, 14, 15 }, tmp); tmp = new byte[2]; buf.get(4, tmp, 0, 2); Assert.assertEquals(new byte[] { 5, 11 }, tmp); tmp = new byte[10]; buf.get(0, tmp, 0, 10); Assert.assertEquals(new byte[] { 1, 2, 3, 4, 5, 11, 12, 13, 14, 15 }, tmp); tmp = new byte[9]; buf.get(1, tmp, 0, 9); Assert.assertEquals(new byte[] { 2, 3, 4, 5, 11, 12, 13, 14, 15 }, tmp); tmp = new byte[11]; tmp[0] = 0; buf.get(0, tmp, 1, 10); Assert.assertEquals(new byte[] { 0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15 }, tmp); } }