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).

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

Used by REPL and bytecode tests to capture program output.

  • 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 static 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 static 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).