/** * Wraps the raw table stream. This is used to create the charPos and * filePos streams. The streams that this method returns are "safe", * meaning that the charPos and filePos position() fields never clobber * each other. They are buffered, meaning that up to <i>n</i> elements * can be read before the disk is accessed again. And they are "origined", * meaning result.position(0) actually positions the stream at the * beginning of the piece table array, not the beginning of the file. * * @param input the stream to wrap * @param pos the origin for the returned stream * @param cache the number of bytes for the returned stream to buffer * @return the wrapped stream * @throws IOException if an IO error occurs */ private SeekInputStream wrap(SeekInputStream input, long pos, int cache) throws IOException { input.position(pos); SeekInputStream r = new SafeSeekInputStream(input); r = new OriginSeekInputStream(r, pos); r = new BufferedSeekInputStream(r, cache); return r; }
@Override public long skip(long c) throws IOException { ensureBuffer(); if (maxOffset == 0) { return 0; } int count = (c > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)c; int skip = Math.min(count, remaining()); offset += skip; return skip; }
/** * Ensures that the buffer is valid. * * @throws IOException if an IO error occurs */ private void ensureBuffer() throws IOException { if (offset >= maxOffset) { buffer(); } }
@Override public int read(byte[] buf) throws IOException { return read(buf, 0, buf.length); }
public void close() throws IOException { super.close(); if(diskStream != null) { diskStream.close(); } }
@Override public int read() throws IOException { ensureBuffer(); if (maxOffset == 0) { return -1; } int ch = buffer[offset] & 0xFF; offset++; return ch; }
/** * Seeks to the given position. This method avoids re-filling the buffer * if at all possible. * * @param p the position to set * @throws IOException if an IO error occurs */ public void position(long p) throws IOException { long blockStart = (input.position() - maxOffset) / buffer.length * buffer.length; long blockEnd = blockStart + maxOffset; if ((p >= blockStart) && (p < blockEnd)) { // Desired position is somewhere inside current buffer long adj = p - blockStart; offset = (int)adj; return; } positionDirect(p); }
/** * Reposition the stream. * * @param p the new position for this stream * @throws IOException if an IO error occurs */ public void position(long p) throws IOException { if (p < 0) { throw new IOException("Negative seek offset."); } if (p > size) { throw new IOException("Desired position exceeds size."); } if (p < buffer.length) { // Only seek file if necessary if (position > buffer.length) { diskStream.position(0); } } else { diskStream.position(p - buffer.length); } this.position = p; }
@Override public long skip(long c) throws IOException { ensureBuffer(); if (maxOffset == 0) { return 0; } int count = (c > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)c; int skip = Math.min(count, remaining()); offset += skip; return skip; }
/** * Ensures that the buffer is valid. * * @throws IOException if an IO error occurs */ private void ensureBuffer() throws IOException { if (offset >= maxOffset) { buffer(); } }
@Override public int read(byte[] buf) throws IOException { return read(buf, 0, buf.length); }
public void close() throws IOException { super.close(); if(diskStream != null) { diskStream.close(); } }
@Override public int read() throws IOException { ensureBuffer(); if (maxOffset == 0) { return -1; } int ch = buffer[offset] & 0xFF; offset++; return ch; }
/** * Seeks to the given position. This method avoids re-filling the buffer * if at all possible. * * @param p the position to set * @throws IOException if an IO error occurs */ public void position(long p) throws IOException { long blockStart = (input.position() - maxOffset) / buffer.length * buffer.length; long blockEnd = blockStart + maxOffset; if ((p >= blockStart) && (p < blockEnd)) { // Desired position is somewhere inside current buffer long adj = p - blockStart; offset = (int)adj; return; } positionDirect(p); }
/** * Reposition the stream. * * @param p the new position for this stream * @throws IOException if an IO error occurs */ public void position(long p) throws IOException { if (p < 0) { throw new IOException("Negative seek offset."); } if (p > size) { throw new IOException("Desired position exceeds size."); } if (p < buffer.length) { // Only seek file if necessary if (position > buffer.length) { diskStream.position(0); } } else { diskStream.position(p - buffer.length); } this.position = p; }
@Override public long skip(long c) throws IOException { ensureBuffer(); if (maxOffset == 0) { return 0; } int count = (c > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)c; int skip = Math.min(count, remaining()); offset += skip; return skip; }
protected void setupDiskStream(File backingFile) throws IOException { RandomAccessInputStream rais = new RandomAccessInputStream(backingFile); diskStream = new BufferedSeekInputStream(rais, 4096); }
/** * Ensures that the buffer is valid. * * @throws IOException if an IO error occurs */ private void ensureBuffer() throws IOException { if (offset >= maxOffset) { buffer(); } }
@Override public int read(byte[] buf) throws IOException { return read(buf, 0, buf.length); }
public void close() throws IOException { super.close(); if(diskStream != null) { diskStream.close(); } }