@Override public void seek(long pos) throws IOException { if (mPos == pos) { return; } Preconditions.checkArgument(pos >= 0, ERR_SEEK_NEGATIVE, pos); Preconditions.checkArgument(pos < mFileLength, ERR_SEEK_PAST_END_OF_FILE, pos); seekBlockInStream(pos); checkAndAdvanceBlockInStream(); mCurrentBlockInStream.seek(mPos % mBlockSize); }
@Override public long skip(long n) throws IOException { if (n <= 0) { return 0; } long toSkip = Math.min(n, mFileLength - mPos); long newPos = mPos + toSkip; long toSkipInBlock = ((newPos / mBlockSize) > mPos / mBlockSize) ? newPos % mBlockSize : toSkip; seekBlockInStream(newPos); checkAndAdvanceBlockInStream(); if (toSkipInBlock != mCurrentBlockInStream.skip(toSkipInBlock)) { throw new IOException("The underlying BlockInStream could not skip " + toSkip); } return toSkip; }
@Override public int read() throws IOException { if (mPos >= mFileLength) { return -1; } checkAndAdvanceBlockInStream(); int data = mCurrentBlockInStream.read(); mPos ++; if (mShouldCacheCurrentBlock) { try { mCurrentCacheStream.write(data); } catch (IOException ioe) { LOG.warn("Block of ID " + getCurrentBlockId() + " could not be cached into Tachyon. Exception:" + ioe.getMessage()); mShouldCacheCurrentBlock = false; } } return data; }