public final class IOStreams extends Object implements IO
It is not good practice to clutter production code with calls to
System.out, System.err, and System.in. But on the
other hand most applications must do I/O to the user. This aggregate object
contains I/O streams to pass around as injected dependency. It is only
necessary to the systems IO only at the main applications entry point:
public void main(final String[] args) {
final IOStreams io = new IOStreams(System.in, System.out, System.err);
// Pass around the io object to your client code.
System.exit(0);
}
In the test code it will be easy to replace the I/O with mocks:
@Test public void someTestWithIO() {
final IOStreams io = new IOStreams(mock(InputStream.class),
mock(PrintStream.class),
mock(PrintStream.class));
// Pass around the io object to your client code and assert something.
}
As a convenience method for creating an I/O streams object with the default I/O streams
of System you can use newDefault().
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_ENCODING
Default character encoding for out put print streams.
|
private PrintStream |
stderr
Standard error stream.
|
private InputStream |
stdin
Standard input stream.
|
private PrintStream |
stdout
Standard output stream.
|
| Constructor and Description |
|---|
IOStreams(InputStream stdin,
PrintStream stdout,
PrintStream stderr)
Initializes the streams.
|
| Modifier and Type | Method and Description |
|---|---|
void |
error(String str)
Prints error.
|
void |
errorln(String str)
Prints error line.
|
PrintStream |
getStderr()
Get standard errorln output stream.
|
InputStream |
getStdin()
Get standard input stream.
|
PrintStream |
getStdout()
Get standard output stream.
|
static IOStreams |
newDefault()
Creates same as
newDefault(java.lang.String) but with DEFAULT_ENCODING as encoding. |
static IOStreams |
newDefault(String encoding)
|
void |
print(String str)
Prints string.
|
void |
println(String str)
Prints string line.
|
void |
printStackTrace(Throwable ex)
Prints exception stack trace.
|
public static final String DEFAULT_ENCODING
private final InputStream stdin
private final PrintStream stdout
private final PrintStream stderr
public IOStreams(InputStream stdin, PrintStream stdout, PrintStream stderr)
stdin - must not be nullstdout - must not be nullstderr - must not be nullpublic PrintStream getStderr()
public InputStream getStdin()
public PrintStream getStdout()
public static IOStreams newDefault() throws UnsupportedEncodingException
newDefault(java.lang.String) but with DEFAULT_ENCODING as encoding.UnsupportedEncodingException - If system does not support encoding.public static IOStreams newDefault(String encoding) throws UnsupportedEncodingException
System.in, System.out, and System.err.
The output "print streams" get DEFAULT_ENCODING as encoding set.
Also the System.out, and System.err are changed with a copy of the original
print stream with the encoding.encoding - must not be null or emptyUnsupportedEncodingException - if system does not support passed encodingpublic void printStackTrace(Throwable ex)
printStackTrace in interface IOex - must not be nullpublic void errorln(String str)
public void error(String str)
public void println(String str)
Copyright © 2012 Sven Strittmatter. All Rights Reserved.