Example usage for java.io PrintWriter flush

List of usage examples for java.io PrintWriter flush

Introduction

In this page you can find the example usage for java.io PrintWriter flush.

Prototype

public void flush() 

Source Link

Document

Flushes the stream.

Usage

From source file:Transform.java

/**
 * convert a Throwable into an array of Strings
 * @param throwable/*  w  w  w.j  a  v  a 2 s . c o  m*/
 * @return string representation of the throwable
 */
public static String[] getThrowableStrRep(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
    ArrayList<String> lines = new ArrayList<String>();
    try {
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
    } catch (IOException ex) {
        lines.add(ex.toString());
    }
    String[] rep = new String[lines.size()];
    lines.toArray(rep);
    return rep;
}

From source file:com.splunk.shuttl.testutil.TUtilsTestNG.java

private static String getStackTrace(Exception exception) {
    StringWriter stackTraceStringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stackTraceStringWriter);
    exception.printStackTrace(printWriter);
    printWriter.flush();
    return stackTraceStringWriter.toString();
}

From source file:com.twosigma.beaker.mimetype.MIMEContainer.java

protected static String exceptionToString(Exception e) {
    StringWriter w = new StringWriter();
    PrintWriter printWriter = new PrintWriter(w);
    e.printStackTrace(printWriter);//  ww  w . ja v a2s . c o m
    printWriter.flush();
    return w.toString();
}

From source file:net.openhft.chronicle.queue.ChronicleReaderMain.java

private static void printUsageAndExit(final Options options) {
    final PrintWriter writer = new PrintWriter(System.out);
    new HelpFormatter().printUsage(writer, 180, ChronicleReaderMain.class.getSimpleName(), options);
    writer.flush();
    System.exit(1);/*  w ww.  j a  va2s  .  c o  m*/
}

From source file:com.progressiveaccess.cmlspeech.base.FileHandler.java

/**
 * Writes a document to a CML file./*ww  w.  java 2 s.  c  om*/
 *
 * @param doc
 *          The output document.
 * @param fileName
 *          The base filename.
 * @param extension
 *          The additional extension.
 *
 * @throws IOException
 *           Problems with opening output file.
 * @throws CDKException
 *           Problems with writing the CML XOM.
 */
public static void writeFile(final Document doc, final String fileName, final String extension)
        throws IOException, CDKException {
    final String basename = FilenameUtils.getBaseName(fileName);
    final OutputStream outFile = new BufferedOutputStream(
            new FileOutputStream(basename + "-" + extension + ".cml"));
    final PrintWriter output = new PrintWriter(outFile);
    output.write(XOMUtil.toPrettyXML(doc));
    output.flush();
    output.close();
}

From source file:io.proleap.vb6.TestGenerator.java

public static void generateTreeFile(final File vb6InputFile, final File outputDirectory) throws IOException {
    final File outputFile = new File(outputDirectory + "/" + vb6InputFile.getName() + TREE_EXTENSION);

    final boolean createdNewFile = outputFile.createNewFile();

    if (createdNewFile) {
        LOG.info("Creating tree file {}.", outputFile);

        final InputStream inputStream = new FileInputStream(vb6InputFile);
        final VisualBasic6Lexer lexer = new VisualBasic6Lexer(new ANTLRInputStream(inputStream));
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
        final StartRuleContext startRule = parser.startRule();
        final String inputFileTree = TreeUtils.toStringTree(startRule, parser);

        final PrintWriter pWriter = new PrintWriter(new FileWriter(outputFile));

        pWriter.write(inputFileTree);/*from w ww.j  av  a  2s .c o m*/
        pWriter.flush();
        pWriter.close();
    }
}

From source file:gov.nist.appvet.tool.sigverifier.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *///  w w  w .j av a  2  s  .c o  m
public static boolean sendInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setContentType("text/html");
        response.setHeader("toolrisk", reportStatus.name());
        PrintWriter out = response.getWriter();
        out.println(reportText);
        out.flush();
        out.close();
        log.debug("Returned report");
        return true;
    } catch (IOException e) {
        log.error(e.toString());
        return false;
    }
}

From source file:it.biztech.btable.util.Utils.java

public static void buildJsonResult(final OutputStream out, final Boolean success, final Object result) {
    final JSONObject jsonResult = new JSONObject();
    jsonResult.put("status", Boolean.toString(success));
    if (result != null) {
        jsonResult.put("result", result);
    }//  w w  w. j a  va2s .c om
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(out);
        pw.print(jsonResult.toString(2));
        pw.flush();
    } finally {
        IOUtils.closeQuietly(pw);
    }
}

From source file:com.momab.dstool.DSTool.java

static void printErrorMessage(String message, OutputStream out) {
    PrintWriter writer = new PrintWriter(out);
    writer.println(String.format("Error: %s", message));
    writer.flush();
}

From source file:com.google.api.GoogleAPI.java

/**
 * Forms an HTTP request, sends it using POST method and returns the result of the request as a JSONObject.
 * //from  w ww. jav a 2s. co  m
 * @param url The URL to query for a JSONObject.
 * @param parameters Additional POST parameters
 * @return The translated String.
 * @throws Exception on error.
 */
protected static JSONObject retrieveJSON(final URL url, final String parameters) throws Exception {
    try {
        final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setRequestProperty("referer", referrer);
        uc.setRequestMethod("POST");
        uc.setDoOutput(true);

        final PrintWriter pw = new PrintWriter(uc.getOutputStream());
        pw.write(parameters);
        pw.flush();

        try {
            final String result = inputStreamToString(uc.getInputStream());

            return new JSONObject(result);
        } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
            uc.getInputStream().close();
            if (uc.getErrorStream() != null) {
                uc.getErrorStream().close();
            }
            pw.close();
        }
    } catch (Exception ex) {
        throw new Exception("[google-api-translate-java] Error retrieving translation.", ex);
    }
}