@Override public final int read(long pos, byte[] buf, int off, int len) throws IOException { if (pos < 0) { throw new IllegalArgumentException(); } boundsCheck(buf, off, len); return doRead(pos, buf, off, len); }
@Override public final void close() throws IOException { mIoState = null; doClose(); }
@Override public final void write(long pos, byte[] buf, int off, int len) throws IOException { if (pos < 0) { throw new IllegalArgumentException(); } boundsCheck(buf, off, len); doWrite(pos, buf, off, len); }
@Override public void write(byte[] b, int off, int len) throws IOException { boundsCheck(b, off, len); ioCheckOpen(mIoState); avail = buf.length; try { AbstractStream.this.doWrite(mPos, buf, 0, avail); } catch (Throwable e) { mEnd = avail; AbstractStream.this.doWrite(mPos, b, off, len); mPos += len;
@Override public int read(byte[] b, int off, int len) throws IOException { boundsCheck(b, off, len); ioCheckOpen(mIoState); amt = AbstractStream.this.doRead(mPos, b, off, len); if (amt <= 0) { break doRead; amt = AbstractStream.this.doRead(mPos, buf, 0, buf.length); if (amt <= 0) { break doRead;
@Override public final OutputStream newOutputStream(long pos, int bufferSize) throws IOException { if (pos < 0) { throw new IllegalArgumentException(); } checkOpen(); return new Out(mIoState, pos, new byte[selectBufferSize(bufferSize)]); }
@Override public void write(int b) throws IOException { ioCheckOpen(mIoState); byte[] buf = mBuffer; int end = mEnd; if (end >= buf.length) { flush(); end = 0; } buf[end++] = (byte) b; try { if (end >= buf.length) { AbstractStream.this.doWrite(mPos, buf, 0, end); mPos += end; end = 0; } } finally { mEnd = end; } }
@Override public int read() throws IOException { ioCheckOpen(mIoState); byte[] buf = mBuffer; int start = mStart; if (start < mEnd) { mPos++; int b = buf[start] & 0xff; mStart = start + 1; return b; } long pos = mPos; int amt = AbstractStream.this.doRead(pos, buf, 0, buf.length); if (amt <= 0) { if (amt < 0) { throw new NoSuchValueException(); } return -1; } mEnd = amt; mPos = pos + 1; mStart = 1; return buf[0] & 0xff; }
@Override public long skip(long n) throws IOException { ioCheckOpen(mIoState); if (n <= 0) { return 0; } int start = mStart; int amt = mEnd - start; if (amt > 0) { if (n >= amt) { // Skip the entire buffer. mEnd = start; } else { amt = (int) n; mStart = start + amt; } mPos += amt; return amt; } long pos = mPos; long newPos = Math.min(pos + n, length()); if (newPos > pos) { mPos = newPos; return newPos - pos; } else { return 0; } }
@Override public void close() throws IOException { AbstractStream.this.ioClose(mIoState); } }
@Override public void flush() throws IOException { ioCheckOpen(mIoState); doFlush(); }
private void doFlush() throws IOException { int end = mEnd; if (end > 0) { AbstractStream.this.doWrite(mPos, mBuffer, 0, end); mPos += end; mEnd = 0; } } }
@Override public final InputStream newInputStream(long pos) throws IOException { return newInputStream(pos, -1); }
@Override public final OutputStream newOutputStream(long pos) throws IOException { return newOutputStream(pos, -1); }
@Override public void write(byte[] b, int off, int len) throws IOException { boundsCheck(b, off, len); ioCheckOpen(mIoState); avail = buf.length; try { AbstractStream.this.doWrite(mPos, buf, 0, avail); } catch (Throwable e) { mEnd = avail; AbstractStream.this.doWrite(mPos, b, off, len); mPos += len;
@Override public int read(byte[] b, int off, int len) throws IOException { boundsCheck(b, off, len); ioCheckOpen(mIoState); amt = AbstractStream.this.doRead(mPos, b, off, len); if (amt <= 0) { break doRead; amt = AbstractStream.this.doRead(mPos, buf, 0, buf.length); if (amt <= 0) { break doRead;
@Override public final InputStream newInputStream(long pos, int bufferSize) throws IOException { if (pos < 0) { throw new IllegalArgumentException(); } checkOpen(); return new In(mIoState, pos, new byte[selectBufferSize(bufferSize)]); }
@Override public void write(int b) throws IOException { ioCheckOpen(mIoState); byte[] buf = mBuffer; int end = mEnd; if (end >= buf.length) { flush(); end = 0; } buf[end++] = (byte) b; try { if (end >= buf.length) { AbstractStream.this.doWrite(mPos, buf, 0, end); mPos += end; end = 0; } } finally { mEnd = end; } }
@Override public int read() throws IOException { ioCheckOpen(mIoState); byte[] buf = mBuffer; int start = mStart; if (start < mEnd) { mPos++; int b = buf[start] & 0xff; mStart = start + 1; return b; } long pos = mPos; int amt = AbstractStream.this.doRead(pos, buf, 0, buf.length); if (amt <= 0) { if (amt < 0) { throw new NoSuchValueException(); } return -1; } mEnd = amt; mPos = pos + 1; mStart = 1; return buf[0] & 0xff; }
@Override public long skip(long n) throws IOException { ioCheckOpen(mIoState); if (n <= 0) { return 0; } int start = mStart; int amt = mEnd - start; if (amt > 0) { if (n >= amt) { // Skip the entire buffer. mEnd = start; } else { amt = (int) n; mStart = start + amt; } mPos += amt; return amt; } long pos = mPos; long newPos = Math.min(pos + n, length()); if (newPos > pos) { mPos = newPos; return newPos - pos; } else { return 0; } }