@Test public void slice() throws Exception { for (int begin = 0; begin < mChain.size(); begin++) { for (int end = begin + 1; end < mChain.size(); end++) { int size = end - begin; ByteBuffer buffer = mChain.slice(begin, size).getByteBuffer(0, size); assertByteBufferEquals( ByteBuffer.wrap("1234567890".substring(begin, end).getBytes(US_ASCII)), buffer); } } }
@Override public DataSource slice(long offset, long size) { Pair<Integer, Long> firstSource = locateDataSource(offset); int beginIndex = firstSource.getFirst(); long beginLocalOffset = firstSource.getSecond(); DataSource beginSource = mSources[beginIndex]; if (beginLocalOffset + size <= beginSource.size()) { return beginSource.slice(beginLocalOffset, size); } ArrayList<DataSource> sources = new ArrayList<>(); sources.add(beginSource.slice( beginLocalOffset, beginSource.size() - beginLocalOffset)); Pair<Integer, Long> lastSource = locateDataSource(offset + size - 1); int endIndex = lastSource.getFirst(); long endLocalOffset = lastSource.getSecond(); for (int i = beginIndex + 1; i < endIndex; i++) { sources.add(mSources[i]); } sources.add(mSources[endIndex].slice(0, endLocalOffset + 1)); return new ChainedDataSource(sources.toArray(new DataSource[0])); }
ChainedDataSource implements DataSource { @Override public DataSource slice(long offset, long size) { Pair<Integer, Long> firstSource = locateDataSource(offset); int beginIndex = firstSource.getFirst(); long beginLocalOffset = firstSource.getSecond(); DataSource beginSource = mSources[beginIndex]; if (beginLocalOffset + size <= beginSource.size()) { return beginSource.slice(beginLocalOffset, size); } ArrayList<DataSource> sources = new ArrayList<>(); sources.add(beginSource.slice( beginLocalOffset, beginSource.size() - beginLocalOffset)); Pair<Integer, Long> lastSource = locateDataSource(offset + size - 1); int endIndex = lastSource.getFirst(); long endLocalOffset = lastSource.getSecond(); for (int i = beginIndex + 1; i < endIndex; i++) { sources.add(mSources[i]); } sources.add(mSources[endIndex].slice(0, endLocalOffset + 1)); return new ChainedDataSource(sources.toArray(new DataSource[0])); } }
ChainedDataSource implements DataSource { @Override public DataSource slice(long offset, long size) { Pair<Integer, Long> firstSource = locateDataSource(offset); int beginIndex = firstSource.getFirst(); long beginLocalOffset = firstSource.getSecond(); DataSource beginSource = mSources[beginIndex]; if (beginLocalOffset + size <= beginSource.size()) { return beginSource.slice(beginLocalOffset, size); } ArrayList<DataSource> sources = new ArrayList<>(); sources.add(beginSource.slice( beginLocalOffset, beginSource.size() - beginLocalOffset)); Pair<Integer, Long> lastSource = locateDataSource(offset + size - 1); int endIndex = lastSource.getFirst(); long endLocalOffset = lastSource.getSecond(); for (int i = beginIndex + 1; i < endIndex; i++) { sources.add(mSources[i]); } sources.add(mSources[endIndex].slice(0, endLocalOffset + 1)); return new ChainedDataSource(sources.toArray(new DataSource[0])); } ChainedDataSource(DataSource... sources); }
ChainedDataSource implements DataSource { @Override public DataSource slice(long offset, long size) { Pair<Integer, Long> firstSource = locateDataSource(offset); int beginIndex = firstSource.getFirst(); long beginLocalOffset = firstSource.getSecond(); DataSource beginSource = mSources[beginIndex]; if (beginLocalOffset + size <= beginSource.size()) { return beginSource.slice(beginLocalOffset, size); } ArrayList<DataSource> sources = new ArrayList<>(); sources.add(beginSource.slice( beginLocalOffset, beginSource.size() - beginLocalOffset)); Pair<Integer, Long> lastSource = locateDataSource(offset + size - 1); int endIndex = lastSource.getFirst(); long endLocalOffset = lastSource.getSecond(); for (int i = beginIndex + 1; i < endIndex; i++) { sources.add(mSources[i]); } sources.add(mSources[endIndex].slice(0, endLocalOffset + 1)); return new ChainedDataSource(sources.toArray(new DataSource[0])); } ChainedDataSource(DataSource... sources); @Override long size(); @Override void feed(long offset, long size, DataSink sink); @Override ByteBuffer getByteBuffer(long offset, int size); @Override void copyTo(long offset, int size, ByteBuffer dest); @Override DataSource slice(long offset, long size); }
ChainedDataSource implements DataSource { @Override public DataSource slice(long offset, long size) { Pair<Integer, Long> firstSource = locateDataSource(offset); int beginIndex = firstSource.getFirst(); long beginLocalOffset = firstSource.getSecond(); DataSource beginSource = mSources[beginIndex]; if (beginLocalOffset + size <= beginSource.size()) { return beginSource.slice(beginLocalOffset, size); } ArrayList<DataSource> sources = new ArrayList<>(); sources.add(beginSource.slice( beginLocalOffset, beginSource.size() - beginLocalOffset)); Pair<Integer, Long> lastSource = locateDataSource(offset + size - 1); int endIndex = lastSource.getFirst(); long endLocalOffset = lastSource.getSecond(); for (int i = beginIndex + 1; i < endIndex; i++) { sources.add(mSources[i]); } sources.add(mSources[endIndex].slice(0, endLocalOffset + 1)); return new ChainedDataSource(sources.toArray(new DataSource[0])); } ChainedDataSource(DataSource... sources); @Override long size(); @Override void feed(long offset, long size, DataSink sink); @Override ByteBuffer getByteBuffer(long offset, int size); @Override void copyTo(long offset, int size, ByteBuffer dest); @Override DataSource slice(long offset, long size);  }