use of java.io.Writer in project groovy by apache.
the class IOGroovyMethods method withWriter.
/**
* Allows this writer to be used within the closure, ensuring that it
* is flushed and closed before this method returns.
*
* @param writer the writer which is used and then closed
* @param closure the closure that the writer is passed into
* @return the value returned by the closure
* @throws IOException if an IOException occurs.
* @since 1.5.2
*/
public static <T> T withWriter(Writer writer, @ClosureParams(FirstParam.class) Closure<T> closure) throws IOException {
try {
T result = closure.call(writer);
try {
writer.flush();
} catch (IOException e) {
// try to continue even in case of error
}
Writer temp = writer;
writer = null;
temp.close();
return result;
} finally {
closeWithWarning(writer);
}
}
use of java.io.Writer in project groovy by apache.
the class PlatformLineWriterTest method testPlatformLineWriter.
public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
String LS = System.getProperty("line.separator");
Binding binding = new Binding();
binding.setVariable("first", "Tom");
binding.setVariable("last", "Adams");
StringWriter stringWriter = new StringWriter();
Writer platformWriter = new PlatformLineWriter(stringWriter);
GroovyShell shell = new GroovyShell(binding);
platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
stringWriter = new StringWriter();
platformWriter = new PlatformLineWriter(stringWriter);
platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
use of java.io.Writer in project hadoop by apache.
the class KMSClientProvider method writeJson.
private static void writeJson(Map map, OutputStream os) throws IOException {
Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.writerWithDefaultPrettyPrinter().writeValue(writer, map);
}
use of java.io.Writer in project groovy by apache.
the class NioGroovyMethods method append.
/**
* Append the text at the end of the Path, using a specified encoding. If
* the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
* <code>writeBom</code> is <code>true</code>, and the file doesn't already
* exist, the requisite byte order mark is written to the file before the
* text is appended.
*
* @param self a Path
* @param text the text to append at the end of the Path
* @param charset the charset used
* @param writeBom whether to write the BOM
* @throws java.io.IOException if an IOException occurs.
* @since 2.5.0
*/
public static void append(Path self, Object text, String charset, boolean writeBom) throws IOException {
Writer writer = null;
try {
Charset resolvedCharset = Charset.forName(charset);
boolean shouldWriteBom = writeBom && !self.toFile().exists();
OutputStream out = Files.newOutputStream(self, CREATE, APPEND);
if (shouldWriteBom) {
IOGroovyMethods.writeUTF16BomIfRequired(out, resolvedCharset);
}
writer = new OutputStreamWriter(out, resolvedCharset);
InvokerHelper.write(writer, text);
writer.flush();
Writer temp = writer;
writer = null;
temp.close();
} finally {
closeWithWarning(writer);
}
}
use of java.io.Writer in project groovy by apache.
the class NioGroovyMethods method write.
/**
* Write the text to the Path, using the specified encoding. If the given
* charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and
* <code>writeBom</code> is <code>true</code>, the requisite byte order
* mark is written to the file before the text.
*
* @param self a Path
* @param text the text to write to the Path
* @param charset the charset used
* @param writeBom whether to write a BOM
* @throws java.io.IOException if an IOException occurs.
* @since 2.5.0
*/
public static void write(Path self, String text, String charset, boolean writeBom) throws IOException {
Writer writer = null;
try {
OutputStream out = Files.newOutputStream(self);
if (writeBom) {
IOGroovyMethods.writeUTF16BomIfRequired(out, charset);
}
writer = new OutputStreamWriter(out, Charset.forName(charset));
writer.write(text);
writer.flush();
Writer temp = writer;
writer = null;
temp.close();
} finally {
closeWithWarning(writer);
}
}
Aggregations