@Override public DataSource slice(long offset, long size) { checkChunkValid(offset, size); // checkChunkValid ensures that it's OK to cast offset and size to int. return new SliceDataSource((int) offset, (int) size); }
/** * Returns a new in-memory {@link DataSink} which exposes all data consumed so far via the * {@link DataSource} interface. * * @param initialCapacity initial capacity in bytes */ public static ReadableDataSink newInMemoryDataSink(int initialCapacity) { return new ByteArrayDataSink(initialCapacity); } }
@Override public RandomAccessFileDataSource slice(long offset, long size) { long sourceSize = size(); checkChunkValid(offset, size, sourceSize); if ((offset == 0) && (size == sourceSize)) { return this; } return new RandomAccessFileDataSource(mFile, mOffset + offset, size); }
@Override public DataSource slice(long offset, long size) { checkChunkValid(offset, size); // checkChunkValid combined with the way instances of this class are constructed ensures // that mSliceOffset + offset does not overflow and that it's fine to cast size to int. return new SliceDataSource((int) (mSliceOffset + offset), (int) size); }
public static InclusiveIntRange fromTo(int min, int max) { return new InclusiveIntRange(min, max); }
@Override public ByteBuffer getByteBuffer(long offset, int size) { checkChunkValid(offset, size); // checkChunkValid ensures that it's OK to cast offset to int. return ByteBuffer.wrap(mArray, (int) offset, size); }
@Override public void copyTo(long offset, int size, ByteBuffer dest) throws IOException { checkChunkValid(offset, size); // checkChunkValid combined with the way instances of this class are constructed ensures // that mSliceOffset + offset does not overflow. dest.put(mArray, (int) (mSliceOffset + offset), size); }
/** * Returns a {@link DataSink} which outputs received data into the provided file, sequentially, * starting at the beginning of the file. */ public static DataSink asDataSink(RandomAccessFile file) { return new RandomAccessFileDataSink(file); }
/** * Returns a {@link DataSink} which outputs received data into the provided * {@link OutputStream}. */ public static DataSink asDataSink(OutputStream out) { return new OutputStreamDataSink(out); }
public static <A, B> Pair<A, B> of(A first, B second) { return new Pair<A, B>(first, second); }
@Override public void consume(byte[] buf, int offset, int length) throws IOException { if (length == 0) { return; } ensureAvailable(length); System.arraycopy(buf, offset, mArray, mSize, length); mSize += length; }
@Override public void copyTo(long offset, int size, ByteBuffer dest) { dest.put(getByteBuffer(offset, size)); }
/** * Returns a {@link DataSource} backed by the provided {@link RandomAccessFile}. Changes to the * file, including changes to size of file, will be visible in the data source. */ public static DataSource asDataSource(RandomAccessFile file) { if (file == null) { throw new NullPointerException(); } return new RandomAccessFileDataSource(file); }
@Override public ByteBuffer getByteBuffer(long offset, int size) throws IOException { ByteBuffer result = ByteBuffer.allocate(size); copyTo(offset, size, result); result.flip(); return result; }
@Override public DataSink getDataSink() { synchronized (mLock) { checkNotDone(); if (mDataSink == null) { mDataSink = new MessageDigestSink(new MessageDigest[] {getMessageDigest()}); } return mDataSink; } }
/** * Returns a new in-memory {@link DataSink} which exposes all data consumed so far via the * {@link DataSource} interface. */ public static ReadableDataSink newInMemoryDataSink() { return new ByteArrayDataSink(); }
public static InclusiveIntRange from(int min) { return new InclusiveIntRange(min, Integer.MAX_VALUE); }
@Override public void feed(long offset, long size, DataSink sink) throws IOException { checkChunkValid(offset, size); // checkChunkValid ensures that it's OK to cast offset and size to int. sink.consume(mArray, (int) offset, (int) size); }
@Override public ByteBuffer getByteBuffer(long offset, int size) throws IOException { checkChunkValid(offset, size); // checkChunkValid combined with the way instances of this class are constructed ensures // that mSliceOffset + offset does not overflow. return ByteBuffer.wrap(mArray, (int) (mSliceOffset + offset), size); }