Package cs1302.tracer.trace
Class OutputSlice
java.lang.Object
cs1302.tracer.trace.OutputSlice
Represents an immutable slice of captured output bytes without requiring premature copying.
-
Method Summary
Modifier and TypeMethodDescriptionReturns the contents of this slice decoded using the specified charset.Returns the contents of this slice decoded as a UTF-8 string.bytebyteAt(int index) Returns the byte at the specified index within this slice.booleancontentEquals(OutputSlice other) Compares the byte contents of this slice with another slice without allocating byte arrays.static OutputSliceempty()Returns an empty output slice.booleanvoidforEachByte(StreamDrainer.ByteConsumer consumer) Streams all bytes in this slice to the given consumer without allocating a byte array.static OutputSlicefrom(byte[] bytes) Wraps an entire byte array as an OutputSlice.static OutputSlicefrom(byte[] bytes, int offset, int length) Creates an OutputSlice representing a subrange of a byte array.static OutputSlicefrom(StreamDrainer drainer, int offset, int length) Creates an OutputSlice backed by an active StreamDrainer sink.inthashCode()intindexOf(byte b, int fromIndex) Returns the index within this slice of the first occurrence of the specified byte, starting at the specified index.booleanisEmpty()Returns true if this slice contains zero bytes.intlength()Returns the number of bytes contained in this slice.Materializes this slice into a self-contained slice detached from any drainer.booleanstartsWith(byte[] prefix) Checks if this slice starts with the specified byte prefix without allocating memory.subSlice(int relativeOffset) Creates a new sub-slice starting at the specified relative offset.subSlice(int relativeOffset, int subLength) Creates a new sub-slice with relative offset and length.byte[]Materializes and returns the slice contents as a newly allocated byte array.toString()
-
Method Details
-
empty
Returns an empty output slice.- Returns:
- An empty OutputSlice instance.
-
from
Wraps an entire byte array as an OutputSlice.- Parameters:
bytes- Direct byte array, or null.- Returns:
- OutputSlice instance.
-
from
Creates an OutputSlice representing a subrange of a byte array.- Parameters:
bytes- Direct byte array, or null.offset- Starting byte index.length- Number of bytes in slice.- Returns:
- OutputSlice instance.
-
from
Creates an OutputSlice backed by an active StreamDrainer sink.- Parameters:
drainer- The source StreamDrainer.offset- Starting byte offset in drainer buffer.length- Captured byte length.- Returns:
- OutputSlice instance.
-
materialize
Materializes this slice into a self-contained slice detached from any drainer.- Returns:
- Materialized OutputSlice instance.
-
length
public int length()Returns the number of bytes contained in this slice.- Returns:
- Byte length.
-
isEmpty
public boolean isEmpty()Returns true if this slice contains zero bytes.- Returns:
- True if empty.
-
toByteArray
public byte[] toByteArray()Materializes and returns the slice contents as a newly allocated byte array.- Returns:
- Copied byte array.
-
asUtf8String
Returns the contents of this slice decoded as a UTF-8 string.- Returns:
- Decoded string.
-
asString
Returns the contents of this slice decoded using the specified charset.- Parameters:
charset- Character encoding to use.- Returns:
- Decoded string.
-
subSlice
Creates a new sub-slice with relative offset and length.- Parameters:
relativeOffset- Relative byte offset from the start of this slice.subLength- Desired byte length of the sub-slice.- Returns:
- New sub-slice.
-
subSlice
Creates a new sub-slice starting at the specified relative offset.- Parameters:
relativeOffset- Relative byte offset from the start of this slice.- Returns:
- New sub-slice.
-
byteAt
public byte byteAt(int index) Returns the byte at the specified index within this slice.- Parameters:
index- The 0-based index within this slice.- Returns:
- The byte value at the index.
-
forEachByte
Streams all bytes in this slice to the given consumer without allocating a byte array.- Parameters:
consumer- Consumer called for each byte.- Throws:
IOException- If the consumer throws an IOException.
-
startsWith
public boolean startsWith(byte[] prefix) Checks if this slice starts with the specified byte prefix without allocating memory.- Parameters:
prefix- The byte sequence to look for.- Returns:
- True if this slice starts with prefix.
-
indexOf
public int indexOf(byte b, int fromIndex) Returns the index within this slice of the first occurrence of the specified byte, starting at the specified index.- Parameters:
b- The byte to search for.fromIndex- The index to start the search from.- Returns:
- The index of the byte within this slice, or -1 if not found.
-
contentEquals
Compares the byte contents of this slice with another slice without allocating byte arrays.- Parameters:
other- The other slice to compare against.- Returns:
- True if byte contents match.
-
equals
-
hashCode
public int hashCode() -
toString
-