@Override public void close() throws IOException { if (!mClosed && mCurrentBlockInStream != null) { mCurrentBlockInStream.close(); } mClosed = true; }
private void checkAndAdvanceBlockInStream() throws IOException { if (mCurrentBlockLeft == 0) { if (mCurrentBlockInStream != null) { mCurrentBlockInStream.close(); } mCurrentBlockIndex = getCurrentBlockIndex(); mCurrentBlockInStream = BlockInStream.get(FILE, READ_TYPE, mCurrentBlockIndex); mCurrentBlockLeft = BLOCK_CAPACITY; } }
@Override public void seek(long pos) throws IOException { if (mCurrentPosition == pos) { return; } if (pos < 0) { throw new IOException("pos is negative: " + pos); } if ((int) (pos / BLOCK_CAPACITY) != mCurrentBlockIndex) { mCurrentBlockIndex = (int) (pos / BLOCK_CAPACITY); if (mCurrentBlockInStream != null) { mCurrentBlockInStream.close(); } mCurrentBlockInStream = BlockInStream.get(FILE, READ_TYPE, mCurrentBlockIndex); } mCurrentBlockInStream.seek(pos % BLOCK_CAPACITY); mCurrentPosition = pos; mCurrentBlockLeft = BLOCK_CAPACITY - (pos % BLOCK_CAPACITY); }
@Override public long skip(long n) throws IOException { if (n <= 0) { return 0; } long ret = n; if (mCurrentPosition + n >= FILE.length()) { ret = FILE.length() - mCurrentPosition; mCurrentPosition += ret; } else { mCurrentPosition += n; } int tBlockIndex = (int) (mCurrentPosition / BLOCK_CAPACITY); if (tBlockIndex != mCurrentBlockIndex) { if (mCurrentBlockInStream != null) { mCurrentBlockInStream.close(); } mCurrentBlockIndex = tBlockIndex; mCurrentBlockInStream = BlockInStream.get(FILE, READ_TYPE, mCurrentBlockIndex); long shouldSkip = mCurrentPosition % BLOCK_CAPACITY; long skip = mCurrentBlockInStream.skip(shouldSkip); mCurrentBlockLeft = BLOCK_CAPACITY - skip; if (skip != shouldSkip) { throw new IOException( "The underlayer BlockInStream only skip " + skip + " instead of " + shouldSkip); } } else { long skip = mCurrentBlockInStream.skip(ret); if (skip != ret) { throw new IOException( "The underlayer BlockInStream only skip " + skip + " instead of " + ret); } } return ret; }