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
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.
-
Field Summary
Fields inherited from class FilterOutputStream
out -
Constructor Summary
ConstructorsConstructorDescriptionThreadLocalPrintStream(PrintStream fallback) Create a thread-local delegating PrintStream. -
Method Summary
Modifier and TypeMethodDescriptionClear the thread-local output and return the captured content.voidflush()Get the fallback stream (original System.out or System.err).voidsetThreadOutput(ByteArrayOutputStream output) Set the output capture for the current thread.voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class PrintStream
append, append, append, charset, checkError, clearError, close, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, writeBytesMethods inherited from class OutputStream
nullOutputStream
-
Constructor Details
-
ThreadLocalPrintStream
Create a thread-local delegating PrintStream.- Parameters:
fallback- The stream to use when no thread-local output is registered
-
-
Method Details
-
setThreadOutput
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
Clear the thread-local output and return the captured content.- Returns:
- The captured output, or empty string if nothing was captured
-
getFallback
Get the fallback stream (original System.out or System.err). -
write
public void write(int b) - Overrides:
writein classPrintStream
-
write
public void write(@Nonnull byte[] b, int off, int len) - Overrides:
writein classPrintStream
-
flush
public void flush()- Specified by:
flushin interfaceFlushable- Overrides:
flushin classPrintStream
-