Class StreamDrainer

java.lang.Object
cs1302.tracer.trace.StreamDrainer
All Implemented Interfaces:
AutoCloseable

public class StreamDrainer extends Object implements AutoCloseable
Asynchronously drains an InputStream into an in-memory byte buffer and provides synchronization barriers to ensure all emitted bytes are captured before inspection.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Functional interface for streaming individual bytes with IO exceptions.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new StreamDrainer for the specified source stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Attaches an active trace session so this drainer enforces its output limit.
    byte
    byteAt(int index)
    Returns the byte at the specified index in the captured buffer.
    void
     
    void
    Detaches the active trace session so this drainer does not retain it.
    void
    forEachByte(int offset, int length, StreamDrainer.ByteConsumer consumer)
    Streams a subrange of bytes under a single lock acquisition to the specified consumer.
    byte[]
    Returns a copy of the accumulated bytes.
    byte[]
    getBytes(int offset, int length)
    Returns a copy of the specified subrange of accumulated bytes.
    getString(int offset, int length, Charset charset)
    Decodes the specified subrange of accumulated bytes into a string.
    int
    indexOf(int offset, int length, byte b, int fromIndex)
    Searches for the first occurrence of a byte in a subrange under a single lock acquisition.
    boolean
    Returns true if EOF has been encountered on the source stream.
    void
    Resets the accumulated byte buffer in the sink.
    int
    Returns the number of bytes currently accumulated in the sink.
    Returns an OutputSlice referencing the current accumulated bytes in this drainer.
    boolean
    startsWith(int offset, int length, byte[] prefix)
    Checks if a subrange of bytes starts with the given prefix under a single lock acquisition.
    void
    Synchronizes the stream using default wait and quiet-period thresholds.
    void
    sync(long maxWaitMillis, long quietPeriodMillis)
    Synchronizes the stream with adaptive quiet-period polling without contending on the underlying input stream lock.
    void
    syncUntil(long expectedTotalBytes, long maxWaitMillis)
    Synchronizes the stream until at least the expected total bytes are read or timeout elapses.
    void
    waitForEof(long timeoutMillis)
    Waits up to timeoutMillis for the stream reader to reach EOF.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StreamDrainer

      public StreamDrainer(InputStream source)
      Constructs a new StreamDrainer for the specified source stream.
      Parameters:
      source - The input stream to drain.
  • Method Details

    • attachSession

      public void attachSession(TraceSession session)
      Attaches an active trace session so this drainer enforces its output limit.
      Parameters:
      session - Active trace session.
    • detachSession

      public void detachSession()
      Detaches the active trace session so this drainer does not retain it.
    • reset

      public void reset()
      Resets the accumulated byte buffer in the sink.
    • sync

      public void sync()
      Synchronizes the stream using default wait and quiet-period thresholds.
    • sync

      public void sync(long maxWaitMillis, long quietPeriodMillis)
      Synchronizes the stream with adaptive quiet-period polling without contending on the underlying input stream lock.

      If bytes have recently been read or arrive after the flush, this method waits until no new bytes arrive for quietPeriodMillis, or until maxWaitMillis elapses. If no bytes arrive after a micro-wait, it returns immediately.

      Parameters:
      maxWaitMillis - Maximum milliseconds to wait.
      quietPeriodMillis - Milliseconds of quiet time required after reading bytes.
    • syncUntil

      public void syncUntil(long expectedTotalBytes, long maxWaitMillis)
      Synchronizes the stream until at least the expected total bytes are read or timeout elapses.
      Parameters:
      expectedTotalBytes - Minimum total bytes expected to be read.
      maxWaitMillis - Maximum milliseconds to wait.
    • getBytes

      public byte[] getBytes()
      Returns a copy of the accumulated bytes.
      Returns:
      Byte array containing all captured output.
    • snapshotOutput

      public OutputSlice snapshotOutput()
      Returns an OutputSlice referencing the current accumulated bytes in this drainer.
      Returns:
      Captured OutputSlice.
    • getBytes

      public byte[] getBytes(int offset, int length)
      Returns a copy of the specified subrange of accumulated bytes.
      Parameters:
      offset - Starting byte offset.
      length - Number of bytes to copy.
      Returns:
      Subrange byte array.
    • byteAt

      public byte byteAt(int index)
      Returns the byte at the specified index in the captured buffer.
      Parameters:
      index - Byte index to inspect.
      Returns:
      Byte at specified index.
    • forEachByte

      public void forEachByte(int offset, int length, StreamDrainer.ByteConsumer consumer) throws IOException
      Streams a subrange of bytes under a single lock acquisition to the specified consumer.
      Parameters:
      offset - Starting byte offset.
      length - Number of bytes to consume.
      consumer - Consumer invoked for each byte.
      Throws:
      IOException - If the consumer throws an IOException.
    • startsWith

      public boolean startsWith(int offset, int length, byte[] prefix)
      Checks if a subrange of bytes starts with the given prefix under a single lock acquisition.
      Parameters:
      offset - Starting byte offset.
      length - Available byte length.
      prefix - Prefix to check.
      Returns:
      True if the subrange starts with the prefix.
    • indexOf

      public int indexOf(int offset, int length, byte b, int fromIndex)
      Searches for the first occurrence of a byte in a subrange under a single lock acquisition.
      Parameters:
      offset - Starting byte offset.
      length - Available byte length.
      b - Byte to find.
      fromIndex - Relative index within the subrange to start searching.
      Returns:
      Relative index of first match within the subrange, or -1 if not found.
    • getString

      public String getString(int offset, int length, Charset charset)
      Decodes the specified subrange of accumulated bytes into a string.
      Parameters:
      offset - Starting byte offset.
      length - Number of bytes to decode.
      charset - Character encoding to use.
      Returns:
      Decoded string.
    • size

      public int size()
      Returns the number of bytes currently accumulated in the sink.
      Returns:
      Accumulated byte count.
    • isEof

      public boolean isEof()
      Returns true if EOF has been encountered on the source stream.
      Returns:
      True if EOF reached.
    • waitForEof

      public void waitForEof(long timeoutMillis)
      Waits up to timeoutMillis for the stream reader to reach EOF.
      Parameters:
      timeoutMillis - Maximum milliseconds to wait.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable