Class TiedOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
Usage example:
PrintStream debugPrintStream = new PrintStream(new OutputStream() {@Override
public void write(int b) { debugTextArea.append(String.valueOf((char)b)); debugTextArea.setCaretPosition(debugTextArea.getDocument().getLength()); } }); TiedOutputStream tiedOutputStream = new TiedOutputStream(debugPrintStream); tiedOutputStream.tieOutputStreams(); debugFrame.addWindowListener(new java.awt.event.WindowAdapter() {@Override
public void windowClosed(java.awt.event.WindowEvent windowEvent) { tiedOutputStream.resetOutputStreams(); } });
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the original standard error stream (System.err) before it was tied.Retrieves the original standard output stream (System.out) before it was tied.boolean
Checks whether the tied output stream is set to copy output to the original output stream.boolean
Checks whether the tied output stream is currently enabled.void
void
void
Resets System.out and System.err to their original streams.void
setCopyEnabled
(boolean copy) Sets whether the tied output stream should copy output to the original output stream.void
setEnabled
(boolean enabled) Enables or disables the tied output stream.void
Ties the current output stream to both System.out and System.err.void
tieOutputStreams
(boolean copy) Ties the current output stream to both System.out and System.err.void
write
(byte[] b, int off, int len) void
write
(int b) Methods inherited from class java.io.PrintStream
append, append, append, charset, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, printf, println, println, println, println, println, println, println, println, println, setError, write, writeBytes
Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
TiedOutputStream
Creates a new TiedOutputStream.- Parameters:
outputStream
- the output stream to tie toNote:
This needs to call tieOutputStreams to start and then resetOutputStreams when finished.
- See Also:
-
-
Method Details
-
getOriginalOutputStream
Retrieves the original standard output stream (System.out) before it was tied.- Returns:
- the original PrintStream for standard output
-
getOriginalErrorStream
Retrieves the original standard error stream (System.err) before it was tied.- Returns:
- the original PrintStream for standard error
-
tieOutputStreams
public void tieOutputStreams()Ties the current output stream to both System.out and System.err.Overload method that enables copy by default.
This method replaces the standard output and error streams with this instance, allowing all output to be redirected to the tied output stream.
- See Also:
-
tieOutputStreams
public void tieOutputStreams(boolean copy) Ties the current output stream to both System.out and System.err.This method replaces the standard output and error streams with this instance, allowing all output to be redirected to the tied output stream.
- Parameters:
copy
- true to copy the output to the original output stream, false to disable copying- See Also:
-
resetOutputStreams
public void resetOutputStreams()Resets System.out and System.err to their original streams.This method should be called when the tied output stream is no longer needed, to restore the original behavior of the standard output and error streams.
- See Also:
-
setEnabled
public void setEnabled(boolean enabled) Enables or disables the tied output stream.When disabled, the tied output stream will not output anything, and the original output stream will handle all output alone.
- Parameters:
enabled
- true to enable the tied output stream, false to disable it
-
setCopyEnabled
public void setCopyEnabled(boolean copy) Sets whether the tied output stream should copy output to the original output stream.Note: Tied output stream needs to be enabled to copy
- Parameters:
copy
- true to enable copying, false to disable it
-
isEnabled
public boolean isEnabled()Checks whether the tied output stream is currently enabled.- Returns:
- true if the tied output stream is enabled, false otherwise
-
isCopyEnabled
public boolean isCopyEnabled()Checks whether the tied output stream is set to copy output to the original output stream.- Returns:
- true if copying is enabled, false otherwise
-
print
- Overrides:
print
in classPrintStream
-
printf
- Overrides:
printf
in classPrintStream
-
println
- Overrides:
println
in classPrintStream
-
write
public void write(int b) - Overrides:
write
in classPrintStream
-
write
public void write(byte[] b, int off, int len) - Overrides:
write
in classPrintStream
-