@Override public int read(ByteBuffer dst) throws IOException { requireOpen(); buffer.limit(0); wrapped.position(position); int read = wrapped.read(dst); if (read > 0) { position += read; } return read; }
private int ensureBuffer() throws IOException { if (!buffer.hasRemaining()) { buffer.clear(); wrapped.read(buffer); buffer.flip(); } return buffer.remaining(); }
public BufferedSeekableSource(SeekableSource wrapped) { super( ofNullable(wrapped) .map(SeekableSource::id) .orElseThrow( () -> { return new IllegalArgumentException( "Input decorated SeekableSource cannot be null"); })); this.wrapped = wrapped; this.size = wrapped.size(); this.buffer.limit(0); }
@Override public SeekableSource position(long newPosition) throws IOException { requireArg(newPosition >= 0, "Cannot set position to a negative value"); if (newPosition != this.position) { long newBufPosition = newPosition - position + buffer.position(); if (newBufPosition >= 0 && newBufPosition < buffer.limit()) { buffer.position((int) newBufPosition); } else { buffer.limit(0); wrapped.position(newPosition); } this.position = Math.min(newPosition, size); } return this; }
@Override public SeekableSource view(long startingPosition, long length) throws IOException { requireOpen(); return new BufferedSeekableSource(wrapped.view(startingPosition, length)); }