/** * Calls {@link #toOutputStream(Charset)} with `charset=UTF-8`. */ public OutputStream toOutputStream() { return toOutputStream(StandardCharsets.UTF_8); }
/** Creates a PrintStream of the given charset, which passes its content to this StringPrinter. */ public PrintStream toPrintStream(Charset charset) { return Errors.rethrow().get(() -> { return new PrintStream(toOutputStream(charset), true, charset.name()); }); }
/** Creates a PrintStream of the given charset, which passes its content to this StringPrinter. */ public PrintStream toPrintStream(Charset charset) { return Errors.rethrow().get(() -> { return new PrintStream(toOutputStream(charset), true, charset.name()); }); }
/** * 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 PrintStream toPrintStream(Charset charset) { return Errors.rethrow().get(() -> { return new PrintStream(toOutputStream(charset), true, charset.name()); }); }
private static String getManifestContent(Jar jarTask, BndManifestExtension extension) throws Throwable { // if we don't want to merge, then delete the existing manifest so that bnd doesn't merge with it if (!extension.mergeWithExisting) { Files.deleteIfExists(outputManifest(jarTask)); } // take the bnd action return BndManifestPlugin.takeBndAction(jarTask.getProject(), jarTask, jar -> { return StringPrinter.buildString(printer -> { try (OutputStream output = printer.toOutputStream(StandardCharsets.UTF_8)) { aQute.bnd.osgi.Jar.writeManifest(jar.getManifest(), printer.toOutputStream(StandardCharsets.UTF_8)); } catch (Exception e) { throw Errors.asRuntime(e); } }); }); }
@Test public void testToOutputStream() { Assert.assertEquals(TEST_STR, StringPrinter.buildString(printer -> { Errors.rethrow().run(() -> { printer.toOutputStream(StandardCharsets.UTF_8).write(TEST_STR.getBytes(StandardCharsets.UTF_8)); }); })); }
/** * Reads the given entry from the zip. * * @param input a zip file * @param toRead a path within that zip file * @return the given path within the zip file decoded as a UTF8 string, with only unix newlines. */ public static String read(File input, String toRead) throws IOException { String raw = StringPrinter.buildString(Errors.rethrow().wrap(printer -> { read(input, toRead, inputStream -> { copy(inputStream, printer.toOutputStream(StandardCharsets.UTF_8)); }); })); return FileMisc.toUnixNewline(raw); }
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); }