@Override public long tell() { if (writer.buffered()) { return writer.tell(); } return reader.tell(); }
@Override public int write(ByteBuffer bytes) { if (reader.buffered()) { reader.clear(); } return writer.write(bytes); }
@Override public long seek(long pos, int whence) { flush(); // First do the raw seek, then empty the read buffer, so that // if the raw seek fails, we don't lose buffered data forever. pos = writer.seek(pos, whence); reader.clear(); return pos; }
/** * Read a chunk of data of size CHUNK_SIZE into the readahead buffer. Returns the amount of data * read. * * @return the amount of data read */ protected int readChunk() { // Prepare the readahead for reading readahead.clear(); if (readahead.remaining() > CHUNK_SIZE) { // Limit potential full reads on a resized readahead to CHUNK_SIZE readahead.limit(readahead.position() + CHUNK_SIZE); } bufferedIO.read1(readahead); readahead.flip(); return readahead.remaining(); }
@Override public long truncate(long pos) { long initialPos = tell(); flush(); pos = bufferedIO.truncate(pos); // FileChannel resets the position to the truncated size if // the position was larger, whereas Python expects the // original position if (initialPos > pos) { seek(initialPos); } return pos; }
/** * Read a chunk of data of the given size into the readahead buffer. Returns the amount of data * read. * * <p>Enforces a minimum size of CHUNK_SIZE. * * @param size the desired size of the chunk * @return the amount of data read */ protected int readChunk(int size) { // Prepare the readahead for reading if (size > CHUNK_SIZE) { // More than we can hold; reallocate a larger readahead readahead = ByteBuffer.allocate(size); } else { size = CHUNK_SIZE; readahead.clear().limit(size); } bufferedIO.readinto(readahead); readahead.flip(); return readahead.remaining(); }
@Override public long seek(long pos, int whence) { pos = bufferedIO.seek(pos, whence); clearReadahead(); return pos; }
@Override public OutputStream asOutputStream() { return bufferedIO.asOutputStream(); }
@Override public void flush() { writer.flush(); }
@Override public boolean closed() { return bufferedIO.closed(); }
@Override public InputStream asInputStream() { return bufferedIO.asInputStream(); }
@Override public boolean readable() { return bufferedIO.readable(); }
@Override public boolean writable() { return bufferedIO.writable(); }
@Override public RawIOBase fileno() { return bufferedIO.fileno(); }
@Override public boolean isatty() { return bufferedIO.isatty(); }
@Override public ByteBuffer peek(int size) { flush(); return reader.peek(size); }
@Override public long tell() { return bufferedIO.tell() - readahead.remaining(); }
@Override public void close() { bufferedIO.close(); }
@Override public void flush() { bufferedIO.flush(); }
@Override public ByteBuffer read(int size) { flush(); return reader.read(size); }
@Override public int readinto(ByteBuffer bytes) { flush(); return reader.readinto(bytes); }
@Override public ByteBuffer readall() { flush(); return reader.readall(); }