public class StringPrinter extends Object
Consumer<String>
through an API similar to PrintWriter and PrintStream.
Can present itself as an OutputStream
, PrintStream
, Writer
, or PrintWriter
.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)
Easy way to create a String from a bunch of lines.
|
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()
Returns a StringPrinter for
System.err . |
static StringPrinter |
systemOut()
Returns a StringPrinter for
System.out . |
OutputStream |
toOutputStream(Charset charset)
Creates an OutputStream which will print its content to the given StringPrinter, encoding bytes according to the given Charset.
|
PrintStream |
toPrintStream()
Creates a UTF-8 PrintStream which passes its content to this StringPrinter.
|
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)
public void print(String content)
public static String buildString(Consumer<StringPrinter> printer)
public static String buildStringFromLines(String... lines)
public OutputStream toOutputStream(Charset charset)
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()
public PrintStream toPrintStream(Charset charset)
public Writer toWriter()
public PrintWriter toPrintWriter()
public static Consumer<String> stringsToLines(Consumer<String> perLine)
"some", "\n", "simple ", "lines", "\n" -> "some", "simple lines" "some\nsimple lines\n" -> "some", "simple lines" "no newline\nno output" -> "no newline"
perLine
- a Consumerpublic static StringPrinter systemOut()
System.out
.public static StringPrinter systemErr()
System.err
.