GroupedRandomAccessSource implements RandomAccessSource { public int get(long position) throws IOException { SourceEntry entry = getSourceEntryForOffset(position); if (entry == null) return -1; return entry.source.get(entry.offsetN(position)); } GroupedRandomAccessSource(RandomAccessSource[] sources); int get(long position); int get(long position, byte[] bytes, int off, int len); long length(); void close();  }
@Test public void testGet() throws Exception { ArrayRandomAccessSource source1 = new ArrayRandomAccessSource(data); ArrayRandomAccessSource source2 = new ArrayRandomAccessSource(data); ArrayRandomAccessSource source3 = new ArrayRandomAccessSource(data); RandomAccessSource[] inputs = new RandomAccessSource[]{ source1, source2, source3 }; GroupedRandomAccessSource grouped = new GroupedRandomAccessSource(inputs); Assert.assertEquals(source1.length() + source2.length() + source3.length(), grouped.length()); Assert.assertEquals(source1.get(99), grouped.get(99)); Assert.assertEquals(source2.get(0), grouped.get(100)); Assert.assertEquals(source2.get(1), grouped.get(101)); Assert.assertEquals(source1.get(99), grouped.get(99)); Assert.assertEquals(source3.get(99), grouped.get(299)); Assert.assertEquals(-1, grouped.get(300)); }
@Test public void testGetArray() throws Exception { ArrayRandomAccessSource source1 = new ArrayRandomAccessSource(data); ArrayRandomAccessSource source2 = new ArrayRandomAccessSource(data); ArrayRandomAccessSource source3 = new ArrayRandomAccessSource(data); RandomAccessSource[] inputs = new RandomAccessSource[]{ source1, source2, source3 }; GroupedRandomAccessSource grouped = new GroupedRandomAccessSource(inputs); byte[] out = new byte[500]; Assert.assertEquals(300, grouped.get(0, out, 0, 300)); assertArrayEqual(rangeArray(0, 100), 0, out, 0, 100); assertArrayEqual(rangeArray(0, 100), 0, out, 100, 100); assertArrayEqual(rangeArray(0, 100), 0, out, 200, 100); Assert.assertEquals(300, grouped.get(0, out, 0, 301)); assertArrayEqual(rangeArray(0, 100), 0, out, 0, 100); assertArrayEqual(rangeArray(0, 100), 0, out, 100, 100); assertArrayEqual(rangeArray(0, 100), 0, out, 200, 100); Assert.assertEquals(100, grouped.get(150, out, 0, 100)); assertArrayEqual(rangeArray(50, 50), 0, out, 0, 50); assertArrayEqual(rangeArray(0, 50), 0, out, 50, 50); }