Search in sources :

Example 11 with Writer

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);
    }
}
Also used : StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 12 with 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());
}
Also used : Binding(groovy.lang.Binding) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) GroovyShell(groovy.lang.GroovyShell)

Example 13 with Writer

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);
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with Writer

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);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 15 with 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);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Aggregations

Writer (java.io.Writer)1259 OutputStreamWriter (java.io.OutputStreamWriter)512 IOException (java.io.IOException)414 StringWriter (java.io.StringWriter)300 File (java.io.File)269 FileOutputStream (java.io.FileOutputStream)196 BufferedWriter (java.io.BufferedWriter)178 FileWriter (java.io.FileWriter)174 PrintWriter (java.io.PrintWriter)159 OutputStream (java.io.OutputStream)120 Test (org.junit.Test)109 InputStreamReader (java.io.InputStreamReader)71 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 BufferedReader (java.io.BufferedReader)62 Reader (java.io.Reader)62 HashMap (java.util.HashMap)59 Map (java.util.Map)59 ArrayList (java.util.ArrayList)58 InputStream (java.io.InputStream)54 Properties (java.util.Properties)39