Search in sources :

Example 41 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class TestLogger method exceptionToString.

private static String exceptionToString(Throwable t) {
    if (t == null) {
        return "(null)";
    }
    try {
        StringWriter stm = new StringWriter();
        PrintWriter wrt = new PrintWriter(stm);
        t.printStackTrace(wrt);
        wrt.close();
        return stm.toString();
    } catch (Throwable ignored) {
        return t.getClass().getName() + " (error while printing stack trace)";
    }
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 42 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class EncodingGroovyMethods method encodeHex.

/**
     * Produces a Writable that writes the hex encoding of the byte[]. Calling
     * toString() on this Writable returns the hex encoding as a String. The hex
     * encoding includes two characters for each byte and all letters are lower case.
     *
     * @param data byte array to be encoded
     * @return object which will write the hex encoding of the byte array
     * @see Integer#toHexString(int)
     */
public static Writable encodeHex(final byte[] data) {
    return new Writable() {

        public Writer writeTo(Writer out) throws IOException {
            for (int i = 0; i < data.length; i++) {
                // convert byte into unsigned hex string
                String hexString = Integer.toHexString(data[i] & 0xFF);
                // add leading zero if the length of the string is one
                if (hexString.length() < 2) {
                    out.write("0");
                }
                // write hex string to writer
                out.write(hexString);
            }
            return out;
        }

        public String toString() {
            StringWriter buffer = new StringWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
Also used : StringWriter(java.io.StringWriter) StringWriterIOException(groovy.lang.StringWriterIOException) Writable(groovy.lang.Writable) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 43 with StringWriter

use of java.io.StringWriter 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 44 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class JavacJavaCompiler method compile.

public void compile(List<String> files, CompilationUnit cu) {
    String[] javacParameters = makeParameters(files, cu.getClassLoader());
    StringWriter javacOutput = null;
    int javacReturnValue = 0;
    try {
        Class javac = findJavac(cu);
        Method method = null;
        try {
            method = javac.getMethod("compile", new Class[] { String[].class, PrintWriter.class });
            javacOutput = new StringWriter();
            PrintWriter writer = new PrintWriter(javacOutput);
            Object ret = method.invoke(null, javacParameters, writer);
            javacReturnValue = (Integer) ret;
        } catch (NoSuchMethodException e) {
        }
        if (method == null) {
            method = javac.getMethod("compile", new Class[] { String[].class });
            Object ret = method.invoke(null, new Object[] { javacParameters });
            javacReturnValue = (Integer) ret;
        }
    } catch (InvocationTargetException ite) {
        cu.getErrorCollector().addFatalError(new ExceptionMessage((Exception) ite.getCause(), true, cu));
    } catch (Exception e) {
        cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
    }
    if (javacReturnValue != 0) {
        switch(javacReturnValue) {
            case 1:
                addJavacError("Compile error during compilation with javac.", cu, javacOutput);
                break;
            case 2:
                addJavacError("Invalid commandline usage for javac.", cu, javacOutput);
                break;
            case 3:
                addJavacError("System error during compilation with javac.", cu, javacOutput);
                break;
            case 4:
                addJavacError("Abnormal termination of javac.", cu, javacOutput);
                break;
            default:
                addJavacError("unexpected return value by javac.", cu, javacOutput);
                break;
        }
    } else {
        // print warnings if any
        System.out.print(javacOutput);
    }
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) StringWriter(java.io.StringWriter) GroovyObject(groovy.lang.GroovyObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrintWriter(java.io.PrintWriter)

Example 45 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class CSTNode method toString.

//---------------------------------------------------------------------------
// STRING CONVERSION
/**
    *  Formats the node as a <code>String</code> and returns it.
    */
public String toString() {
    StringWriter string = new StringWriter();
    write(new PrintWriter(string));
    string.flush();
    return string.toString();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Aggregations

StringWriter (java.io.StringWriter)3175 PrintWriter (java.io.PrintWriter)1057 Test (org.junit.Test)612 IOException (java.io.IOException)516 StringReader (java.io.StringReader)232 Writer (java.io.Writer)211 StreamResult (javax.xml.transform.stream.StreamResult)207 File (java.io.File)194 InputStreamReader (java.io.InputStreamReader)140 HashMap (java.util.HashMap)136 Transformer (javax.xml.transform.Transformer)125 InputStream (java.io.InputStream)119 Map (java.util.Map)116 ArrayList (java.util.ArrayList)106 DOMSource (javax.xml.transform.dom.DOMSource)99 BufferedReader (java.io.BufferedReader)96 ByteArrayInputStream (java.io.ByteArrayInputStream)84 Reader (java.io.Reader)77 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)75 HttpServletResponse (javax.servlet.http.HttpServletResponse)73