PagedChannelRandomAccessSource extends GroupedRandomAccessSource implements RandomAccessSource { @Override public void close() throws IOException { super.close(); channel.close(); } PagedChannelRandomAccessSource(FileChannel channel);  PagedChannelRandomAccessSource(final FileChannel channel, final int totalBufferSize, final int maxOpenBuffers); @Override /** * {@inheritDoc} * Cleans the mapped bytebuffers and closes the channel */ void close(); static final int DEFAULT_TOTAL_BUFSIZE; static final int DEFAULT_MAX_OPEN_BUFFERS; }
@Test public void testGetArrayPastEOF() throws Exception{ PagedChannelRandomAccessSource s = new PagedChannelRandomAccessSource(channel); try{ byte[] chunk = new byte[(int)channel.size() * 2]; int len = s.get(0, chunk, 0, chunk.length); Assert.assertEquals((int)channel.size(), len); } finally { s.close(); } }
@Test public void testGet() throws Exception { PagedChannelRandomAccessSource s = new PagedChannelRandomAccessSource(channel); try{ for(int i = 0; i < data.length; i++){ int ch = s.get(i); Assert.assertFalse("EOF hit unexpectedly at " + i + " out of " + data.length, ch == -1); Assert.assertEquals("Position " + i, data[i], (byte)ch); } Assert.assertEquals(-1, s.get(data.length)); } finally { s.close(); } }
@Test public void testGetArray() throws Exception { byte[] chunk = new byte[257]; PagedChannelRandomAccessSource s = new PagedChannelRandomAccessSource(channel, data.length/10, 1); try{ int pos = 0; int count = s.get(pos, chunk, 0, chunk.length-1); while (count != -1){ assertArrayEqual(data, pos, chunk, 0, count); pos += count; count = s.get(pos, chunk, 0, chunk.length-1); } Assert.assertEquals(-1, s.get(pos, chunk, 0, chunk.length)); } finally { s.close(); } }
@Test public void testGetArrayMultiPages() throws Exception { byte[] chunk = new byte[257]; PagedChannelRandomAccessSource s = new PagedChannelRandomAccessSource(channel, data.length/10, 7); try{ int pos = 0; int count = s.get(pos, chunk, 0, chunk.length-1); while (count != -1){ assertArrayEqual(data, pos, chunk, 0, count); pos += count; count = s.get(pos, chunk, 0, chunk.length-1); } Assert.assertEquals(-1, s.get(pos, chunk, 0, chunk.length)); } finally { s.close(); } }