protected SeekableInputStream makeInputStream(final InputStream pStream) { try { return new FileCacheSeekableStream(pStream); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public int read() throws IOException { checkOpen(); int read; if (position == streamPosition) { // Read ahead into buffer, for performance read = readAhead(buffer, 0, buffer.length); if (read >= 0) { read = buffer[0] & 0xff; } //System.out.println("Read 1 byte from stream: " + Integer.toHexString(read & 0xff)); } else { // ..or read byte from the cache syncPosition(); read = getCache().read(); //System.out.println("Read 1 byte from cache: " + Integer.toHexString(read & 0xff)); } // TODO: This field is not REALLY considered accessible.. :-P if (read != -1) { position++; } return read; }
/** * Creates a {@code FileCacheSeekableStream} reading from the given * {@code InputStream}. Data will be cached in a temporary file, with * the given base name, in the given directory * * @param pStream the {@code InputStream} to read from * @param pTempBaseName optional base name for the temporary file * @param pTempDir optional temp directory * * @throws IOException if the temporary file cannot be created, * or cannot be opened for random access. */ public FileCacheSeekableStream(final InputStream pStream, final String pTempBaseName, final File pTempDir) throws IOException { // NOTE: We do validation BEFORE we create temp file, to avoid orphan files this(Validate.notNull(pStream, "stream"), createTempFile(pTempBaseName, pTempDir)); }
private int readAhead(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { int length; length = stream.read(pBytes, pOffset, pLength); if (length > 0) { streamPosition += length; getCache().write(pBytes, pOffset, length); } return length; }
private int readAhead(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { int length; length = mStream.read(pBytes, pOffset, pLength); if (length > 0) { mStreamPosition += length; getCache().write(pBytes, pOffset, length); } return length; }
@Override public int read(byte[] pBytes, int pOffset, int pLength) throws IOException { checkOpen(); int length; if (position == streamPosition) { // Read bytes from the stream length = readAhead(pBytes, pOffset, pLength); //System.out.println("Read " + length + " byte from stream"); } else { // ...or read bytes from the cache syncPosition(); length = getCache().read(pBytes, pOffset, (int) Math.min(pLength, streamPosition - position)); //System.out.println("Read " + length + " byte from cache"); } // TODO: This field is not REALLY considered accessible.. :-P if (length > 0) { position += length; } return length; }
/** * Creates a read only {@code CompoundDocument}. * * @param pInput the input to read from * * @throws IOException if an I/O exception occurs while reading the header */ public CompoundDocument(final InputStream pInput) throws IOException { this(new FileCacheSeekableStream(pInput)); }
private int readAhead(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { int length; length = stream.read(pBytes, pOffset, pLength); if (length > 0) { streamPosition += length; getCache().write(pBytes, pOffset, length); } return length; }
/** * Creates a {@code FileCacheSeekableStream} reading from the given * {@code InputStream}. Data will be cached in a temporary file, with * the given base name, in the given directory * * @param pStream the {@code InputStream} to read from * @param pTempBaseName optional base name for the temporary file * @param pTempDir optional temp directory * * @throws IOException if the temporary file cannot be created, * or cannot be opened for random access. */ public FileCacheSeekableStream(final InputStream pStream, final String pTempBaseName, final File pTempDir) throws IOException { // NOTE: We do validation BEFORE we create temp file, to avoid orphan files this(Validate.notNull(pStream, "stream"), createTempFile(pTempBaseName, pTempDir)); }
@Override public int read() throws IOException { checkOpen(); int read; if (position == streamPosition) { // Read ahead into buffer, for performance read = readAhead(buffer, 0, buffer.length); if (read >= 0) { read = buffer[0] & 0xff; } //System.out.println("Read 1 byte from stream: " + Integer.toHexString(read & 0xff)); } else { // ..or read byte from the cache syncPosition(); read = getCache().read(); //System.out.println("Read 1 byte from cache: " + Integer.toHexString(read & 0xff)); } // TODO: This field is not REALLY considered accessible.. :-P if (read != -1) { position++; } return read; }
/** * Creates a read only {@code CompoundDocument}. * * @param pInput the input to read from * * @throws IOException if an I/O exception occurs while reading the header */ public CompoundDocument(final InputStream pInput) throws IOException { this(new FileCacheSeekableStream(pInput)); }
/** * Creates a {@code FileCacheSeekableStream} reading from the given * {@code InputStream}. Data will be cached in a temporary file, with * the given base name, in the given directory * * @param pStream the {@code InputStream} to read from * @param pTempBaseName optional base name for the temporary file * @param pTempDir optional temp directory * * @throws IOException if the temporary file cannot be created, * or cannot be opened for random access. */ public FileCacheSeekableStream(final InputStream pStream, final String pTempBaseName, final File pTempDir) throws IOException { // NOTE: We do validation BEFORE we create temp file, to avoid orphan files this(Validate.notNull(pStream, "stream"), createTempFile(pTempBaseName, pTempDir)); }
@Override public int read(byte[] pBytes, int pOffset, int pLength) throws IOException { checkOpen(); int length; if (position == streamPosition) { // Read bytes from the stream length = readAhead(pBytes, pOffset, pLength); //System.out.println("Read " + length + " byte from stream"); } else { // ...or read bytes from the cache syncPosition(); length = getCache().read(pBytes, pOffset, (int) Math.min(pLength, streamPosition - position)); //System.out.println("Read " + length + " byte from cache"); } // TODO: This field is not REALLY considered accessible.. :-P if (length > 0) { position += length; } return length; }
protected SeekableInputStream makeInputStream(final InputStream pStream) { try { return new FileCacheSeekableStream(pStream); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public int read() throws IOException { checkOpen(); int read; if (mPosition == mStreamPosition) { // Read ahead into buffer, for performance read = readAhead(mBuffer, 0, mBuffer.length); if (read >= 0) { read = mBuffer[0] & 0xff; } //System.out.println("Read 1 byte from stream: " + Integer.toHexString(read & 0xff)); } else { // ..or read byte from the cache syncPosition(); read = getCache().read(); //System.out.println("Read 1 byte from cache: " + Integer.toHexString(read & 0xff)); } // TODO: This field is not REALLY considered accessible.. :-P if (read != -1) { mPosition++; } return read; }
protected SeekableInputStream makeInputStream(final InputStream pStream) { try { return new FileCacheSeekableStream(pStream); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public int read(byte[] pBytes, int pOffset, int pLength) throws IOException { checkOpen(); int length; if (mPosition == mStreamPosition) { // Read bytes from the stream length = readAhead(pBytes, pOffset, pLength); //System.out.println("Read " + length + " byte from stream"); } else { // ...or read bytes from the cache syncPosition(); length = getCache().read(pBytes, pOffset, (int) Math.min(pLength, mStreamPosition - mPosition)); //System.out.println("Read " + length + " byte from cache"); } // TODO: This field is not REALLY considered accessible.. :-P if (length > 0) { mPosition += length; } return length; }