RAFRandomAccessSource implements IRandomAccessSource { public int get(long position) throws java.io.IOException { if (position > length) return -1; if (raf.getFilePointer() != position) { raf.seek(position); } return raf.read(); } RAFRandomAccessSource(RandomAccessFile raf); int get(long position); int get(long position, byte[] bytes, int off, int len); long length(); void close();  }
@Test public void getByIndexTest() throws IOException { File file = new File(SOURCE_FILE); try (RandomAccessFile raf = FileUtil.getRandomAccessFile(file)) { RAFRandomAccessSource source = new RAFRandomAccessSource(raf); for (int i = 0; i < content.length; i++) { Assert.assertEquals(content[i], source.get(i)); } } }
@Test public void getByIndexOutOfBoundsTest() throws IOException { File file = new File(SOURCE_FILE); int indexOutOfBounds = content.length; try (RandomAccessFile raf = FileUtil.getRandomAccessFile(file)) { RAFRandomAccessSource source = new RAFRandomAccessSource(raf); Assert.assertNotEquals(-1, source.get(indexOutOfBounds - 1)); Assert.assertEquals(-1, source.get(indexOutOfBounds)); } }
@Test public void getArrayByIndexesTest() throws IOException { File file = new File(SOURCE_FILE); final int beginIndex = 7; final int length = 5; try (RandomAccessFile raf = FileUtil.getRandomAccessFile(file)) { RAFRandomAccessSource source = new RAFRandomAccessSource(raf); byte[] dest = new byte[24]; int read = source.get(beginIndex, dest, 0, length); Assert.assertEquals(length, read); for (int i = 0; i < length; i++) { Assert.assertEquals(content[beginIndex + i], dest[i]); } } }
@Test public void getArrayByIndexesNotEnoughBytesTest() throws IOException { File file = new File(SOURCE_FILE); final int beginIndex = 7; final int length = 24; final int expectedLength = 6; try (RandomAccessFile raf = FileUtil.getRandomAccessFile(file)) { RAFRandomAccessSource source = new RAFRandomAccessSource(raf); byte[] dest = new byte[24]; int read = source.get(beginIndex, dest, 0, length); Assert.assertEquals(expectedLength, read); for (int i = 0; i < expectedLength; i++) { Assert.assertEquals(content[beginIndex + i], dest[i]); } } }
@Test public void getArrayByIndexesWithOffsetTest() throws IOException { File file = new File(SOURCE_FILE); final int beginIndex = 7; final int length = 5; final int offset = 2; try (RandomAccessFile raf = FileUtil.getRandomAccessFile(file)) { RAFRandomAccessSource source = new RAFRandomAccessSource(raf); byte[] dest = new byte[24]; int read = source.get(beginIndex, dest, offset, length); Assert.assertEquals(length, read); for (int i = 0; i < length; i++) { Assert.assertEquals(content[beginIndex + i], dest[offset + i]); } } }
@Test public void getArrayByIndexesOutOfBounds() throws IOException { File file = new File(SOURCE_FILE); final int beginIndex = content.length; final int length = 5; try (RandomAccessFile raf = FileUtil.getRandomAccessFile(file)) { RAFRandomAccessSource source = new RAFRandomAccessSource(raf); byte[] dest = new byte[24]; int read = source.get(beginIndex, dest, 0, length); Assert.assertEquals(-1, read); for (int i = 0; i < dest.length; i++) { Assert.assertEquals(0, dest[i]); } } }