Search in sources :

Example 21 with PrintWriter

use of java.io.PrintWriter 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 22 with PrintWriter

use of java.io.PrintWriter in project hadoop by apache.

the class ReconfigurationServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    LOG.info("POST");
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    Reconfigurable reconf = getReconfigurable(req);
    String nodeName = reconf.getClass().getCanonicalName();
    printHeader(out, nodeName);
    try {
        applyChanges(out, reconf, req);
    } catch (ReconfigurationException e) {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, StringUtils.stringifyException(e));
        return;
    }
    out.println("<p><a href=\"" + req.getServletPath() + "\">back</a></p>");
    printFooter(out);
}
Also used : PrintWriter(java.io.PrintWriter)

Example 23 with PrintWriter

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

the class JavaStubGenerator method generateClass.

public void generateClass(ClassNode classNode) throws FileNotFoundException {
    // Only attempt to render our self if our super-class is resolved, else wait for it
    if (requireSuperResolved && !classNode.getSuperClass().isResolved()) {
        return;
    }
    // owner should take care for us
    if (classNode instanceof InnerClassNode)
        return;
    // don't generate stubs for private classes, as they are only visible in the same file
    if ((classNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0)
        return;
    String fileName = classNode.getName().replace('.', '/');
    mkdirs(outputPath, fileName);
    toCompile.add(fileName);
    File file = new File(outputPath, fileName + ".java");
    FileOutputStream fos = new FileOutputStream(file);
    Charset charset = Charset.forName(encoding);
    PrintWriter out = new PrintWriter(new OutputStreamWriter(fos, charset));
    try {
        String packageName = classNode.getPackageName();
        if (packageName != null) {
            out.println("package " + packageName + ";\n");
        }
        printImports(out, classNode);
        printClassContents(out, classNode);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        // ignore
        }
        try {
            fos.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 24 with PrintWriter

use of java.io.PrintWriter 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 25 with PrintWriter

use of java.io.PrintWriter 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

PrintWriter (java.io.PrintWriter)3529 StringWriter (java.io.StringWriter)1062 IOException (java.io.IOException)653 File (java.io.File)532 Test (org.junit.Test)432 FileOutputStream (java.io.FileOutputStream)293 FileWriter (java.io.FileWriter)274 OutputStreamWriter (java.io.OutputStreamWriter)255 BufferedReader (java.io.BufferedReader)180 ArrayList (java.util.ArrayList)171 HttpServletResponse (javax.servlet.http.HttpServletResponse)141 ByteArrayOutputStream (java.io.ByteArrayOutputStream)139 FastPrintWriter (com.android.internal.util.FastPrintWriter)124 InputStreamReader (java.io.InputStreamReader)123 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 Date (java.util.Date)120 HashMap (java.util.HashMap)113 Map (java.util.Map)106 BufferedWriter (java.io.BufferedWriter)105 Writer (java.io.Writer)87