Package cs1302.tracer.trace
Class StreamDrainer
java.lang.Object
cs1302.tracer.trace.StreamDrainer
- All Implemented Interfaces:
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 ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for streaming individual bytes with IO exceptions. -
Constructor Summary
ConstructorsConstructorDescriptionStreamDrainer(InputStream source) Constructs a new StreamDrainer for the specified source stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidattachSession(TraceSession session) Attaches an active trace session so this drainer enforces its output limit.bytebyteAt(int index) Returns the byte at the specified index in the captured buffer.voidclose()voidDetaches the active trace session so this drainer does not retain it.voidforEachByte(int offset, int length, StreamDrainer.ByteConsumer consumer) Streams a subrange of bytes under a single lock acquisition to the specified consumer.byte[]getBytes()Returns a copy of the accumulated bytes.byte[]getBytes(int offset, int length) Returns a copy of the specified subrange of accumulated bytes.Decodes the specified subrange of accumulated bytes into a string.intindexOf(int offset, int length, byte b, int fromIndex) Searches for the first occurrence of a byte in a subrange under a single lock acquisition.booleanisEof()Returns true if EOF has been encountered on the source stream.voidreset()Resets the accumulated byte buffer in the sink.intsize()Returns the number of bytes currently accumulated in the sink.Returns an OutputSlice referencing the current accumulated bytes in this drainer.booleanstartsWith(int offset, int length, byte[] prefix) Checks if a subrange of bytes starts with the given prefix under a single lock acquisition.voidsync()Synchronizes the stream using default wait and quiet-period thresholds.voidsync(long maxWaitMillis, long quietPeriodMillis) Synchronizes the stream with adaptive quiet-period polling without contending on the underlying input stream lock.voidsyncUntil(long expectedTotalBytes, long maxWaitMillis) Synchronizes the stream until at least the expected total bytes are read or timeout elapses.voidwaitForEof(long timeoutMillis) Waits up totimeoutMillisfor the stream reader to reach EOF.
-
Constructor Details
-
StreamDrainer
Constructs a new StreamDrainer for the specified source stream.- Parameters:
source- The input stream to drain.
-
-
Method Details
-
attachSession
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 untilmaxWaitMilliselapses. 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
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
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 totimeoutMillisfor the stream reader to reach EOF.- Parameters:
timeoutMillis- Maximum milliseconds to wait.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-