Class ThreadLocalPrintStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
org.ek9lang.compiler.backend.jvm.ThreadLocalPrintStream
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public final class ThreadLocalPrintStream extends PrintStream
A PrintStream that delegates to thread-local outputs. This enables parallel execution where each thread captures its own output without interference from other threads.

When a thread has registered an output stream (via setThreadOutput(ByteArrayOutputStream)), all writes from that thread go to its specific stream. Otherwise, writes go to the fallback stream (typically the original System.out or System.err).

This class is thread-safe. Each thread's output is completely isolated.

Instance-based design allows multiple instances (e.g., one for stdout, one for stderr) each with independent thread-local state.

Used by REPL, bytecode tests, and the EK9 test runner for output capture.

  • Constructor Details

    • ThreadLocalPrintStream

      public ThreadLocalPrintStream(PrintStream fallback)
      Create a thread-local delegating PrintStream.
      Parameters:
      fallback - The stream to use when no thread-local output is registered
  • Method Details

    • setThreadOutput

      public void setThreadOutput(ByteArrayOutputStream output)
      Set the output capture for the current thread. All subsequent writes from this thread will go to this stream.
      Parameters:
      output - The stream to capture output to
    • clearAndGet

      public String clearAndGet()
      Clear the thread-local output and return the captured content.
      Returns:
      The captured output, or empty string if nothing was captured
    • getFallback

      public PrintStream getFallback()
      Get the fallback stream (original System.out or System.err).
    • write

      public void write(int b)
      Overrides:
      write in class PrintStream
    • write

      public void write(@Nonnull byte[] b, int off, int len)
      Overrides:
      write in class PrintStream
    • flush

      public void flush()
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class PrintStream