@Override public void close() throws IOException { closeBlockInStream(mBlockInStream); closeBlockInStream(mCachedPositionedReadStream); }
/** * Initializes the underlying block stream if necessary. This method must be called before * reading from mBlockInStream. */ private void updateStream() throws IOException { if (mBlockInStream != null && mBlockInStream.remaining() > 0) { // can still read from stream return; } if (mBlockInStream != null && mBlockInStream.remaining() == 0) { // current stream is done closeBlockInStream(mBlockInStream); } /* Create a new stream to read from mPosition. */ // Calculate block id. long blockId = mStatus.getBlockIds().get(Math.toIntExact(mPosition / mBlockSize)); // Create stream mBlockInStream = mBlockStore.getInStream(blockId, mOptions, mFailedWorkers); // Set the stream to the correct position. long offset = mPosition % mBlockSize; mBlockInStream.seek(offset); }
@Override public void seek(long pos) throws IOException { if (mPosition == pos) { return; } Preconditions.checkArgument(pos >= 0, PreconditionMessage.ERR_SEEK_NEGATIVE.toString(), pos); Preconditions.checkArgument(pos <= mLength, PreconditionMessage.ERR_SEEK_PAST_END_OF_FILE.toString(), pos); if (mBlockInStream == null) { // no current stream open, advance position mPosition = pos; return; } long delta = pos - mPosition; if (delta <= mBlockInStream.remaining() && delta >= -mBlockInStream.getPos()) { // within block mBlockInStream.seek(mBlockInStream.getPos() + delta); } else { // close the underlying stream as the new position is no longer in bounds closeBlockInStream(mBlockInStream); } mPosition += delta; }
mCachedPositionedReadStream = mBlockStore.getInStream(blockId, mOptions, mFailedWorkers); } else if (mCachedPositionedReadStream.getId() != blockId) { closeBlockInStream(mCachedPositionedReadStream); mCachedPositionedReadStream = mBlockStore.getInStream(blockId, mOptions, mFailedWorkers);