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:org.freewheelschedule.freewheel.controlserver.ControlThread.java

private boolean workerAwaitingCommand(BufferedReader result, PrintWriter command, String hostname)
        throws IOException {
    String response = result.readLine();
    if (response.equals(HELO)) {
        command.print(HELO + " " + hostname + "\r\n");
        command.flush();
        response = result.readLine();/* w w w . j  a  v a 2 s.  c o m*/
    }
    return response.equals(COMMAND);
}

From source file:edu.lternet.pasta.portal.UploadEvaluateServlet.java

/**
 * The doPost method of the servlet. <br>
 * /*w w w . ja  v a2  s. c  om*/
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request
 *          the request send by the client to the server
 * @param response
 *          the response send by the server to the client
 * @throws ServletException
 *           if an error occurred
 * @throws IOException
 *           if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession httpSession = request.getSession();

    String uid = (String) httpSession.getAttribute("uid");

    if (uid == null || uid.isEmpty())
        uid = "public";

    String html = null;

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (isMultipart) {

        // Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        // Parse the request
        try {

            List /* FileItem */ items = upload.parseRequest(request);

            // Process the uploaded items
            Iterator iter = items.iterator();

            while (iter.hasNext()) {

                FileItem item = (FileItem) iter.next();

                if (!(item.isFormField())) {

                    File eml = processUploadedFile(item);

                    DataPackageManagerClient dpmClient = new DataPackageManagerClient(uid);
                    String xml = dpmClient.evaluateDataPackage(eml);

                    ReportUtility qrUtility = new ReportUtility(xml);
                    String htmlTable = qrUtility.xmlToHtmlTable(cwd + xslpath);

                    if (htmlTable == null) {
                        String msg = "The uploaded file could not be evaluated.";
                        throw new UserErrorException(msg);
                    } else {
                        html = HTMLHEAD + "<div class=\"qualityreport\">" + htmlTable + "</div>" + HTMLTAIL;
                    }

                }

            }

        } catch (Exception e) {
            handleDataPortalError(logger, e);
        }
    }

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.print(html);
    out.flush();
    out.close();
}

From source file:jeplus.TRNSYSWinTools.java

/**
 * Save the changes of the part INCLUDE from the DCK template to the original INCLUDE file(s)
 *
 * @param DCKTemplate The template DCK file that contains the search strings
 * @param DCKDir//from   ww w  . java  2 s . c om
 * @return state of execution
 */
public static boolean TRNSYSSaveIncludeChanges(String DCKTemplate, String DCKDir) {

    boolean success = true;

    // Load and edit the template file
    try {
        BufferedReader ins = new BufferedReader(new FileReader(DCKDir + DCKTemplate));
        String line = ins.readLine();
        while (line != null) {
            if (line.trim().startsWith("*     --> INCLUDE FILE: ")) {
                String incDir = line.substring(23).trim();
                incDir = incDir.substring(0, incDir.indexOf(" ")).trim().replaceAll("\"", "");
                line = ins.readLine();
                PrintWriter outs = new PrintWriter(new FileWriter(incDir));
                while ((line != null) && (!line.trim().startsWith("*     --> END OF INCLUDE FILE: "))) {
                    if (!line.contains(
                            "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *")) {
                        outs.println(line);
                    }
                    line = ins.readLine();
                }
                outs.flush();
                outs.close();
            }
            line = ins.readLine();
        }
        ins.close();

    } catch (Exception e) {
        logger.error("", e);
        success = false;
    }
    return success;
}

From source file:org.posterita.core.AbstractChart.java

public String getImageMap(String imageMapName) throws OperationException {
    try {//  w w w.j a  va 2 s .c o m
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        ChartUtilities.writeImageMap(printWriter, imageMapName, renderingInfo, false);
        printWriter.flush();

        String imageMap = stringWriter.getBuffer().toString();
        return imageMap;
    } catch (IOException e) {
        throw new OperationException("Problem occured while writing imagemap", e);
    }
}

From source file:com.ikon.servlet.CssServlet.java

/**
 * /*from w w  w  . j  ava2  s.com*/
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String path = request.getPathInfo();
    OutputStream os = null;

    try {
        if (path.length() > 1) {
            String[] foo = path.substring(1).split("/");

            if (foo.length > 1) {
                String context = foo[0];
                String name = foo[1];
                Css css = CssDAO.getInstance().findByContextAndName(context, name);

                if (css == null) {
                    InputStream is = null;

                    try {
                        if (Css.CONTEXT_FRONTEND.equals(context)) {

                        } else if (Css.CONTEXT_EXTENSION.equals(context)) {

                        }
                        /*
                        if (is != null) {
                           css = new Css();
                           css.setContent(IOUtils.toString(is));
                           css.setContext(context);
                           css.setName(name);
                           css.setActive(true);
                        }
                        */
                    } finally {
                        IOUtils.closeQuietly(is);
                    }
                }

                if (css != null) {
                    // Prepare file headers
                    WebUtils.prepareSendFile(request, response, css.getName() + ".css", MimeTypeConfig.MIME_CSS,
                            false);
                    PrintWriter out = new PrintWriter(
                            new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
                    out.append(css.getContent());
                    out.flush();
                }
            }
        }
    } catch (DatabaseException e) {
        log.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:com.bmwcarit.barefoot.matcher.ServerTest.java

private void sendRequest(InetAddress host, int port, JSONArray samples)
        throws InterruptedException, IOException, JSONException {
    int trials = 120;
    int timeout = 500;
    Socket client = null;/* w w  w .j a  v a 2 s .  c o m*/

    while (client == null || !client.isConnected()) {
        try {
            client = new Socket(host, port);
        } catch (IOException e) {
            Thread.sleep(timeout);

            if (trials == 0) {
                client.close();
                throw new IOException(e.getMessage());
            } else {
                trials -= 1;
            }
        }
    }

    PrintWriter writer = new PrintWriter(client.getOutputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    writer.println(samples.toString());
    writer.flush();

    String code = reader.readLine();
    assertEquals("SUCCESS", code);

    String response = reader.readLine();
    client.close();

    MatcherKState state = new MatcherKState(new JSONObject(response),
            new MatcherFactory(ServerControl.getServer().getMap()));

    OutputFormatter output = new GeoJSONOutputFormatter();
    PrintWriter out = new PrintWriter(ServerTest.class.getResource("").getPath() + "ServerTest-matching.json");
    out.println(output.format(null, state));
    out.close();

    assertEquals(samples.length(), state.sequence().size());
}

From source file:cn.vlabs.umt.ui.actions.ManageUserAction.java

public ActionForward loadUser(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    BeanFactory factory = (BeanFactory) getServlet().getServletContext()
            .getAttribute(Attributes.APPLICATION_CONTEXT_KEY);
    UserService us = (UserService) factory.getBean("UserService");
    String username = request.getParameter("q");
    User u = us.getUserByUid(Integer.valueOf(username));

    //JSON/*from  w w  w .  j  a  v  a2 s . co m*/
    StringBuffer buffer = new StringBuffer();
    buffer.append("[{");
    buffer.append("username:'" + u.getCstnetId() + "'");
    buffer.append(",truename:'" + u.getTrueName() + "'");
    buffer.append(",email:'" + u.getCstnetId() + "'");
    buffer.append("}]");

    //
    response.setCharacterEncoding("UTF-8");
    PrintWriter writer = response.getWriter();
    writer.write(buffer.toString());
    writer.flush();
    writer.close();
    return null;
}

From source file:com.openkm.servlet.CssServlet.java

/**
 * //  w  w w.ja  v  a 2  s.c  om
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String path = request.getPathInfo();
    OutputStream os = null;

    try {
        if (path.length() > 1) {
            String[] foo = path.substring(1).split("/");

            if (foo.length > 1) {
                String context = foo[0];
                String name = foo[1];
                Css css = CssDAO.getInstance().findByContextAndName(context, name);

                if (css == null) {
                    InputStream is = null;

                    try {
                        if (Css.CONTEXT_FRONTEND.equals(context)) {

                        } else if (Css.CONTEXT_EXTENSION.equals(context)) {
                            if ("htmlEditor".equals(name)) {
                                is = getServletContext().getResourceAsStream("/css/tiny_mce/content.css");
                            }
                        }

                        if (is != null) {
                            css = new Css();
                            css.setContent(IOUtils.toString(is));
                            css.setContext(context);
                            css.setName(name);
                            css.setActive(true);
                        }
                    } finally {
                        IOUtils.closeQuietly(is);
                    }
                }

                if (css != null) {
                    // Prepare file headers
                    WebUtils.prepareSendFile(request, response, css.getName() + ".css", MimeTypeConfig.MIME_CSS,
                            false);
                    PrintWriter out = new PrintWriter(
                            new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
                    out.append(css.getContent());
                    out.flush();
                }
            }
        }
    } catch (DatabaseException e) {
        log.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.clustering.EmbeddingsClutoDataWriter.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    super.collectionProcessComplete();

    // now produce output file
    // close temp file
    pwTemp.flush();//from   w ww. ja  va 2 s .co m
    pwTemp.close();

    try {
        PrintWriter pw = new PrintWriter(outputFile);

        pw.printf(Locale.ENGLISH, "%d %d%n", collectionSize, VECTOR_SIZE);

        // copy the rest
        IOUtils.copy(new FileInputStream(tmpFile), pw);

        pw.flush();
        pw.close();

        // delete tmp file
        FileUtils.deleteQuietly(tmpFile);
    } catch (IOException e) {
        throw new AnalysisEngineProcessException(e);
    }
}

From source file:grakn.core.console.GraknConsole.java

private void printHelp(PrintStream sout) {
    HelpFormatter formatter = new HelpFormatter();
    OutputStreamWriter writer = new OutputStreamWriter(sout, Charset.defaultCharset());
    PrintWriter printer = new PrintWriter(new BufferedWriter(writer));
    formatter.printHelp(printer, formatter.getWidth(), "grakn console [options]", null, options,
            formatter.getLeftPadding(), formatter.getDescPadding(), null);
    printer.flush();
}