public final class StringPrinter extends Object
Pipes strings to a Consumer<String>
through an API which unifies OutputStream
, PrintStream
, Writer
, and PrintWriter
.
See buildString(Consumer)
for some common use cases.
Constructor and Description |
---|
StringPrinter(Consumer<String> consumer)
StringPrinter will pass all the strings it receives to the given consumer.
|
Modifier and Type | Method and Description |
---|---|
static String |
buildString(Consumer<StringPrinter> printer)
Easy way to create a String using a StringPrinter.
|
static String |
buildStringFromLines(String... lines)
Utility method for creating multi-line strings (joined with
\n , never \r\n ). |
void |
print(String content)
Prints the string.
|
void |
println(String line)
Prints the string and a newline (always ‘\n’).
|
static Consumer<String> |
stringsToLines(Consumer<String> perLine)
Given a consumer of lines, creates a stateful consumer of strings which will combine its input until it finds a newline, and split its input when it contains multiple newlines.
|
static StringPrinter |
systemErr()
Creates a
StringPrinter directed at System.err . |
static StringPrinter |
systemOut()
Creates a
StringPrinter directed at System.out . |
OutputStream |
toOutputStream()
Calls
toOutputStream(Charset) with charset=UTF-8 . |
OutputStream |
toOutputStream(Charset charset)
Creates an
OutputStream which will print its content to this StringPrinter , encoding bytes according to the given Charset . |
PrintStream |
toPrintStream()
Calls
toPrintStream(Charset) with charset=UTF-8 . |
PrintStream |
toPrintStream(Charset charset)
Creates a
PrintStream of the given charset, which passes its content to this StringPrinter . |
PrintWriter |
toPrintWriter()
Creates a PrintWriter which passes its content to this StringPrinter.
|
Writer |
toWriter()
Creates a Writer which passes its content to this StringPrinter.
|
public void println(String line)
Prints the string and a newline (always ‘\n’).
public void print(String content)
Prints the string.
public static String buildString(Consumer<StringPrinter> printer)
Easy way to create a String using a StringPrinter.
String currentStackTrace = StringPrinter.buildString(printer -> {
new Throwable().printStackTrace(printer.toPrintStream());
});
public static String buildStringFromLines(String... lines)
Utility method for creating multi-line strings (joined with \n
, never \r\n
).
public OutputStream toOutputStream()
Calls toOutputStream(Charset)
with charset=UTF-8
.
public OutputStream toOutputStream(Charset charset)
Creates an OutputStream
which will print its content to this StringPrinter
, encoding bytes according to the given Charset
. Doesn’t matter if you close the stream or not, because StringPrinter doesn’t have a close().
Strings are sent to the consumer as soon as their constituent bytes are written to this OutputStream.
The implementation is lifted from Apache commons-io. Many thanks to them!
public PrintStream toPrintStream()
Calls toPrintStream(Charset)
with charset=UTF-8
.
public PrintStream toPrintStream(Charset charset)
Creates a PrintStream
of the given charset, which passes its content to this StringPrinter
.
Note that PrintStream.println
uses the system newline, while StringPrinter.println
always uses \n
.
public Writer toWriter()
Creates a Writer which passes its content to this StringPrinter.
public PrintWriter toPrintWriter()
Creates a PrintWriter which passes its content to this StringPrinter.
Note that PrintStream.println
uses the system newline, while StringPrinter.println
always uses \n
.
public static Consumer<String> stringsToLines(Consumer<String> perLine)
Given a consumer of lines, creates a stateful consumer of strings which will combine its input until it finds a newline, and split its input when it contains multiple newlines. Examples make this more clear:
"some", "\n", "simple ", "lines", "\n" -> "some", "simple lines"
"some\nsimple lines\n" -> "some", "simple lines"
"no newline\nno output" -> "no newline"
perLine
- a Consumer<String>
which will receive strings which were terminated by newlines. (the newlines are stripped when they are passed to the consumer)Consumer<String>
which accepts any strings, and will feed them to perLine.public static StringPrinter systemOut()
Creates a StringPrinter
directed at System.out
.
public static StringPrinter systemErr()
Creates a StringPrinter
directed at System.err
.