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:Main.java

public static String getStackTrace(@Nullable Throwable e) {
    if (e == null) {
        return "";
    }/*  ww  w.  ja v  a2s .  c o  m*/
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}

From source file:gov.nist.appvet.tool.synchtest.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  a va  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("apprisk", reportStatus.name());
        PrintWriter out = response.getWriter();
        out.println(reportText);
        out.flush();
        out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}

From source file:com.delmar.core.web.filter.ExportDelegate.java

/**
 * Actually writes exported data. Extracts content from the Map stored in request with the
 * <code>TableTag.FILTER_CONTENT_OVERRIDE_BODY</code> key.
 * @param wrapper BufferedResponseWrapper implementation
 * @param response HttpServletResponse// ww w . j  a va 2s.  co m
 * @param request ServletRequest
 * @throws java.io.IOException exception thrown by response writer/outputStream
 */
public static void writeExport(HttpServletResponse response, ServletRequest request,
        BufferedResponseWrapper wrapper) throws IOException {

    if (wrapper.isOutRequested()) {
        // data already written
        log.debug("Filter operating in unbuffered mode. Everything done, exiting");
        return;
    }

    // if you reach this point the PARAMETER_EXPORTING has been found, but the special header has never been set in
    // response (this is the signal from table tag that it is going to write exported data)
    log.debug("Filter operating in buffered mode. ");

    Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);

    if (log.isDebugEnabled()) {
        log.debug(bean);
    }

    Object pageContent = bean.get(TableTagParameters.BEAN_BODY);

    if (pageContent == null) {
        if (log.isDebugEnabled()) {
            log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
        }

        response.setContentType(wrapper.getContentType());
        PrintWriter out = response.getWriter();

        out.write(wrapper.getContentAsString());
        out.flush();
        return;
    }

    // clear headers
    if (!response.isCommitted()) {
        response.reset();
    }

    String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
    String contentType = (String) bean.get(TableTagParameters.BEAN_CONTENTTYPE);

    if (StringUtils.isNotBlank(filename)) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    }

    String characterEncoding = wrapper.getCharacterEncoding();
    String wrappedContentType = wrapper.getContentType();

    if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1) {
        // charset is already specified (see #921811)
        characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
    }

    if (characterEncoding != null && contentType.indexOf("charset") == -1) //$NON-NLS-1$
    {
        contentType += "; charset=" + characterEncoding; //$NON-NLS-1$
    }

    response.setContentType(contentType);

    if (pageContent instanceof String) {
        // text content
        if (characterEncoding != null) {
            response.setContentLength(((String) pageContent).getBytes(characterEncoding).length);
        } else {
            //FIXME  Reliance on default encoding
            // Found a call to a method which will perform a byte to String (or String to byte) conversion,
            // and will assume that the default platform encoding is suitable.
            // This will cause the application behaviour to vary between platforms.
            // Use an alternative API and specify a charset name or Charset object explicitly.
            response.setContentLength(((String) pageContent).getBytes().length);
        }

        PrintWriter out = response.getWriter();
        out.write((String) pageContent);
        out.flush();
    } else {
        // dealing with binary content
        byte[] content = (byte[]) pageContent;
        response.setContentLength(content.length);
        OutputStream out = response.getOutputStream();
        out.write(content);
        out.flush();
    }
}

From source file:io.github.azige.mages.Cli.java

static void printHelp(PrintStream out, Options options) {
    HelpFormatter hf = new HelpFormatter();
    PrintWriter pw = new PrintWriter(out);
    hf.printHelp(pw, hf.getWidth(),/*from w w  w . jav  a2  s.  com*/
            "mages [-r <bundle> [-l <locale>]] [-t <template>] [-p <plugin dir>] <input files>",
            "Convert input files.", options, hf.getLeftPadding(), hf.getDescPadding(), null);
    pw.flush();
}

From source file:Main.java

/**
 * Recursively walks the DOM tree starting at the given <CODE>Node</CODE>
 * printing the gory details of its construction to the indicated
 * <CODE>PrintWriter</CODE>.
 * /*www  .j  a  v  a2s  . c om*/
 * @param   out            The <CODE>PrintWriter</CODE> for output.
 * @param    node         The <CODE>Node</CODE> to start the dump at.
 * @since   TFP 1.0
 */
public static void dump(PrintWriter out, final Node node) {
    doDump(out, node, 0);
    out.flush();
}

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

static void printUsage(Options options, OutputStream out) {

    PrintWriter writer = new PrintWriter(out);
    HelpFormatter formatter = new HelpFormatter();
    formatter.printUsage(writer, 80, CMDLINE_SYNTAX, options);
    writer.flush();
}

From source file:sample.server.NativeGemFireServer.java

private static void writeStringTo(File file, String fileContents) {
    PrintWriter fileWriter = null;

    try {/*from w w  w. j av a 2 s .c  om*/
        fileWriter = new PrintWriter(new BufferedWriter(new FileWriter(file, true)), true);
        fileWriter.println(fileContents);
        fileWriter.flush();
    } catch (IOException e) {
        throw new RuntimeException(String.format("Failed to write [%s] to file [%s]", fileContents, file), e);
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }
}

From source file:com.dianping.avatar.cache.util.CacheMonitorUtil.java

private static String getErrorString(Throwable throwable) {
    OutputStream out = null;/*from  www .j  ava  2 s.co m*/
    PrintWriter writer = null;
    try {
        out = new ByteArrayOutputStream(3000);
        writer = new PrintWriter(out);
        throwable.printStackTrace(writer);
        writer.flush();
        return out.toString();
    } catch (Exception e2) {
        return throwable.getMessage();
    } finally {
        if (writer != null) {
            writer.close();
        }
        out = null;
        writer = null;
    }
}

From source file:com.congiu.load.csv.Main.java

static void printUsage(Options options, PrintWriter pw) {
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(pw, 80, "load-csv [options] file [,file...]",
            "loads one or more csv files to HDFS, converting them to " + "a more appropriate format", options,
            2, 2, "");
    pw.flush();

}

From source file:edu.brown.benchmark.voteresper.EPRuntimeUtil.java

public static void writeToFile(String toWrite) {
    try {//from  w ww. j a v a 2 s. c o  m
        if (!VoterConstants.PRINT_TO_CONSOLE) {
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new FileWriter(VoterConstants.OUT_FILE, true)));
            out.println(toWrite);
            out.flush();
            out.close();
        } else {
            System.out.println(toWrite);
        }
    }

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}