public void shouldCorrectlyReadAfterDirectlySkippingMultipleChunks() throws Exception {
int chunkSize = InfinispanBinaryStore.DEFAULT_CHUNK_SIZE;
int totalSize = chunkSize * 3;
byte[] data = new byte[totalSize];
RANDOM.nextBytes(data);
BinaryKey dataKey = BinaryKey.keyFor(data);
ChunkOutputStream chunkOutputStream = new ChunkOutputStream(blobCache, dataKey.toString());
IoUtil.write(new ByteArrayInputStream(data), chunkOutputStream);
// skip 2 bytes and read the rest
ChunkInputStream chunkInputStream = new ChunkInputStream(blobCache, dataKey.toString(), chunkSize, totalSize);
chunkInputStream.skip(2);
byte[] expected = new byte[data.length - 2];
System.arraycopy(data, 2, expected, 0, expected.length);
assertArrayEquals(expected, IoUtil.readBytes(chunkInputStream));
// skip 1 chunk and read until nothing is left
chunkInputStream = new ChunkInputStream(blobCache, dataKey.toString(), chunkSize, totalSize);
chunkInputStream.skip(chunkSize);
expected = new byte[data.length - chunkSize];
System.arraycopy(data, chunkSize, expected, 0, expected.length);
assertArrayEquals(expected, IoUtil.readBytes(chunkInputStream));
// skip the equivalent of 2 chunks, read the rest
chunkInputStream = new ChunkInputStream(blobCache, dataKey.toString(), chunkSize, totalSize);
chunkInputStream.skip(chunkSize * 2);
expected = new byte[data.length - (chunkSize * 2)];
System.arraycopy(data, chunkSize * 2, expected, 0, expected.length);
assertArrayEquals(expected, IoUtil.readBytes(chunkInputStream));