/** * Calls {@link #toPrintStream(Charset)} with `charset=UTF-8`. */ public PrintStream toPrintStream() { return toPrintStream(StandardCharsets.UTF_8); }
/** Creates a UTF-8 PrintStream which passes its content to this StringPrinter. */ public PrintStream toPrintStream() { return toPrintStream(StandardCharsets.UTF_8); }
/** Creates a UTF-8 PrintStream which passes its content to this StringPrinter. */ public PrintStream toPrintStream() { return toPrintStream(StandardCharsets.UTF_8); }
/** * Returns a PrintStream which will redirect all of its output to the source PrintStream. If * the trigger string is passed through the wrapped PrintStream, then it will dump the * stack trace of the call that printed the trigger. * * @param source * the returned PrintStream will delegate to this stream * @param trigger * the string which triggers a stack dump * @return a PrintStream with the above properties */ public static PrintStream wrapAndDumpWhenContains(PrintStream source, String trigger) { StringPrinter wrapped = new StringPrinter(StringPrinter.stringsToLines(perLine -> { source.println(perLine); if (perLine.contains(trigger)) { dump("Triggered by " + trigger); } })); return wrapped.toPrintStream(); }
/** * Returns a PrintStream which will redirect all of its output to the source PrintStream. If * the trigger string is passed through the wrapped PrintStream, then it will dump the * stack trace of the call that printed the trigger. * * @param source * the returned PrintStream will delegate to this stream * @param trigger * the string which triggers a stack dump * @return a PrintStream with the above properties */ public static PrintStream wrapAndDumpWhenContains(PrintStream source, String trigger) { StringPrinter wrapped = new StringPrinter(StringPrinter.stringsToLines(perLine -> { source.println(perLine); if (perLine.contains(trigger)) { dump("Triggered by " + trigger); } })); return wrapped.toPrintStream(); }
@Before public void setupStreams() { testCaseOut.setLength(0); testCaseErr.setLength(0); cleanOut = System.out; cleanErr = System.err; System.setOut(new StringPrinter(str -> { testCaseOut.append(str.replace("\r", "")); }).toPrintStream()); System.setErr(new StringPrinter(str -> { testCaseErr.append(str.replace("\r", "")); }).toPrintStream()); StackDumper.pristineSysErr = new StringPrinter(System.err::print); }
@Test public void testToPrintStream() { Assert.assertEquals(TEST_STR, StringPrinter.buildString(printer -> { Errors.rethrow().run(() -> { printer.toPrintStream().print(TEST_STR); }); })); }
private void testCharset(Charset charset) { // get the bytes for this string byte[] bytes = TEST_UTF.getBytes(charset); // write the complicated UTF string one byte at a time to an OutputStream String byteByByteOutputStream = StringPrinter.buildString(printer -> { Errors.rethrow().run(() -> { OutputStream output = printer.toOutputStream(charset); for (byte b : bytes) { output.write(b); } }); }); Assert.assertEquals(TEST_UTF, byteByByteOutputStream); // write the complicated UTF string one byte at a time to a PrintStream String byteByBytePrintStream = StringPrinter.buildString(printer -> { Errors.rethrow().run(() -> { OutputStream output = printer.toPrintStream(charset); for (byte b : bytes) { output.write(b); } }); }); Assert.assertEquals(TEST_UTF, byteByBytePrintStream); }