Class OutputSlice

java.lang.Object
cs1302.tracer.trace.OutputSlice

public final class OutputSlice extends Object
Represents an immutable slice of captured output bytes without requiring premature copying.
  • Method Summary

    Modifier and Type
    Method
    Description
    asString(Charset charset)
    Returns the contents of this slice decoded using the specified charset.
    Returns the contents of this slice decoded as a UTF-8 string.
    byte
    byteAt(int index)
    Returns the byte at the specified index within this slice.
    boolean
    Compares the byte contents of this slice with another slice without allocating byte arrays.
    Returns an empty output slice.
    boolean
     
    void
    Streams all bytes in this slice to the given consumer without allocating a byte array.
    from(byte[] bytes)
    Wraps an entire byte array as an OutputSlice.
    from(byte[] bytes, int offset, int length)
    Creates an OutputSlice representing a subrange of a byte array.
    from(StreamDrainer drainer, int offset, int length)
    Creates an OutputSlice backed by an active StreamDrainer sink.
    int
     
    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.
    boolean
    Returns true if this slice contains zero bytes.
    int
    Returns the number of bytes contained in this slice.
    Materializes this slice into a self-contained slice detached from any drainer.
    boolean
    startsWith(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.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • empty

      public static OutputSlice empty()
      Returns an empty output slice.
      Returns:
      An empty OutputSlice instance.
    • from

      public static OutputSlice from(byte[] bytes)
      Wraps an entire byte array as an OutputSlice.
      Parameters:
      bytes - Direct byte array, or null.
      Returns:
      OutputSlice instance.
    • from

      public static OutputSlice from(byte[] bytes, int offset, int length)
      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

      public static OutputSlice from(StreamDrainer drainer, int offset, int length)
      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

      public OutputSlice 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

      public String asUtf8String()
      Returns the contents of this slice decoded as a UTF-8 string.
      Returns:
      Decoded string.
    • asString

      public String asString(Charset charset)
      Returns the contents of this slice decoded using the specified charset.
      Parameters:
      charset - Character encoding to use.
      Returns:
      Decoded string.
    • subSlice

      public OutputSlice subSlice(int relativeOffset, int subLength)
      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

      public OutputSlice subSlice(int relativeOffset)
      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

      public void forEachByte(StreamDrainer.ByteConsumer consumer) throws IOException
      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

      public boolean contentEquals(OutputSlice other)
      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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object