Class RecordingInputStream

java.lang.Object
java.io.InputStream
org.archive.io.RecordingInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class RecordingInputStream extends InputStream
Stream which records all data read from it, which it acquires from a wrapped input stream. Makes use of a RecordingOutputStream for recording because of its being file backed so we can write massive amounts of data w/o worrying about overflowing memory.
Author:
gojomo
  • Field Details

    • logger

      protected static Logger logger
    • drainBuffer

      protected byte[] drainBuffer
      Reusable buffer to avoid reallocation on each readFullyUntil
  • Constructor Details

    • RecordingInputStream

      public RecordingInputStream(int bufferSize, String backingFilename)
      Create a new RecordingInputStream.
      Parameters:
      bufferSize - Size of buffer to use.
      backingFilename - Name of backing file.
  • Method Details

    • open

      public void open(InputStream wrappedStream) throws IOException
      Throws:
      IOException
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • getReplayInputStream

      public ReplayInputStream getReplayInputStream() throws IOException
      Throws:
      IOException
    • getMessageBodyReplayInputStream

      public ReplayInputStream getMessageBodyReplayInputStream() throws IOException
      Throws:
      IOException
    • readFully

      public long readFully() throws IOException
      Throws:
      IOException
    • readToEndOfContent

      public void readToEndOfContent(long contentLength) throws IOException, InterruptedException
      Throws:
      IOException
      InterruptedException
    • readFullyOrUntil

      public void readFullyOrUntil(long softMaxLength) throws IOException, RecorderLengthExceededException, RecorderTimeoutException, InterruptedException
      Deprecated.
      Read all of a stream (Or read until we timeout or have read to the max).
      Parameters:
      softMaxLength - Maximum length to read; if zero or < 0, then no limit. If met, return normally.
      Throws:
      IOException - failed read.
      RecorderLengthExceededException
      RecorderTimeoutException
      InterruptedException
    • getSize

      public long getSize()
    • markContentBegin

      public void markContentBegin()
    • getContentBegin

      public long getContentBegin()
    • startDigest

      public void startDigest()
    • setSha1Digest

      public void setSha1Digest()
      Convenience method for setting SHA1 digest.
    • setDigest

      public void setDigest(String algorithm)
      Sets a digest algorithm which may be applied to recorded data. As usually only a subset of the recorded data should be fed to the digest, you must also call startDigest() to begin digesting.
      Parameters:
      algorithm -
    • setDigest

      public void setDigest(MessageDigest md)
      Sets a digest function which may be applied to recorded data. As usually only a subset of the recorded data should be fed to the digest, you must also call startDigest() to begin digesting.
      Parameters:
      md -
    • getDigestValue

      public byte[] getDigestValue()
      Return the digest value for any recorded, digested data. Call only after all data has been recorded; otherwise, the running digest state is ruined.
      Returns:
      the digest final value
    • getResponseContentLength

      public long getResponseContentLength()
    • closeRecorder

      public void closeRecorder() throws IOException
      Throws:
      IOException
    • isOpen

      public boolean isOpen()
      Returns:
      True if we've been opened.
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • setLimits

      public void setLimits(long hardMax, long timeoutMs, long maxRateKBps)
      Set limits to be enforced by internal recording-out
    • getRecordedBufferLength

      public int getRecordedBufferLength()
      Expose the amount of in-memory buffering used by the internal recording stream.
      Returns:
      int buffer size
    • chopAtMessageBodyBegin

      public void chopAtMessageBodyBegin()
    • clearForReuse

      public void clearForReuse() throws IOException
      Throws:
      IOException
    • asOutputStream

      public RecordingOutputStream asOutputStream()
      Returns an OutputStream that can be used for recording input data. This is useful if the input comes in some form other than an InputStream. For example, if the input is provided by a callback periodically called with a chunk of data.