Example usage for java.io Writer flush

List of usage examples for java.io Writer flush

Introduction

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

Prototype

public abstract void flush() throws IOException;

Source Link

Document

Flushes the stream.

Usage

From source file:org.tangram.components.spring.SpringViewUtilities.java

public void render(Writer writer, Map<String, Object> model, String view) throws IOException {
    ServletRequest request = (ServletRequest) model.get(Constants.ATTRIBUTE_REQUEST);
    ServletResponse response = (ServletResponse) model.get(Constants.ATTRIBUTE_RESPONSE);

    ViewContext vc = viewContextFactory.createViewContext(model, view);
    ModelAndView mav = SpringViewUtilities.createModelAndView(vc);
    View effectiveView = mav.getView();
    LOG.debug("render() effectiveView={}", effectiveView);
    try {/*ww  w  .  j  a  v a 2s  .c om*/
        if (effectiveView == null) {
            String viewName = mav.getViewName();
            if (viewName == null) {
                viewName = Constants.DEFAULT_VIEW;
            } // if

            effectiveView = viewHandler.resolveView(viewName, mav.getModel(), Locale.getDefault(), request);
        } // if

        if (writer != null) {
            writer.flush();
        } // if
        LOG.debug("render() model={}", mav.getModel());
        LOG.debug("render({}) effectiveView={}", mav.getViewName(), effectiveView);
        effectiveView.render(mav.getModel(), (HttpServletRequest) request, (HttpServletResponse) response);
    } catch (Exception e) {
        LOG.error("render() #" + view, e);
        if (writer != null) {
            writer.write(e.getLocalizedMessage());
        } // if
    } // try/catch
}

From source file:net.cit.tetrad.resource.SubResource.java

@RequestMapping("/serverDate.do")
public void serverDate(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String strDate = DateUtil.getTime();

    Writer writer = setResponse(response).getWriter();
    writer.write(strDate);//from w  w w.  ja v  a  2s.  c o  m

    writer.flush();

    log.debug("end - serverDate()");
}

From source file:net.cit.tetrad.resource.SubResource.java

@RequestMapping("/getDefaultOffsetDays.do")
public void getDefaultOffsetDays(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String sdateStr = request.getParameter("sdateStr");
    String edateStr = request.getParameter("edateStr");

    int compareMin = (DateUtil.getOffsetMins(edateStr, sdateStr)) / 2;

    String result = DateUtil.getOffsetDateMin(sdateStr, compareMin, "yyyy-MM-dd HH:mm");
    ;//from  w  w w  .j  av  a 2 s.c o m

    Writer writer = setResponse(response).getWriter();
    writer.write(result);

    writer.flush();

    log.debug("end - getDefaultOffsetDays()");
}

From source file:net.cit.tetrad.resource.SubResource.java

@RequestMapping("/compareGraphDate.do")
public void compareGraphDate(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String sdateStr = request.getParameter("sdateStr");
    String sliderMin = request.getParameter("sliderMin");

    int compareDate = DateUtil.getCompareDate(sdateStr, sliderMin, "yyyy-MM-dd HH:mm");

    String result = sdateStr;//from   w  ww.j  a  v  a 2 s.  co  m
    if (compareDate == -1) {
        result = sliderMin;
    }

    Writer writer = setResponse(response).getWriter();
    writer.write(result);

    writer.flush();

    log.debug("end - compareGraphDate()");
}

From source file:net.cit.tetrad.resource.SubResource.java

@RequestMapping("/setDefaultGraphDate.do")
public void setDefaultGraphDate(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String period = request.getParameter("period");
    String format = "yyyy-MM-dd HH:mm";

    Date stDate = defaultStartDate(period);
    Date edDate = new Date();

    String strStDate = DateUtil.getCurrentDate(stDate, format);
    String strEdDate = DateUtil.getCurrentDate(edDate, format);

    String result = strStDate + "|" + strEdDate;

    Writer writer = setResponse(response).getWriter();
    writer.write(result);/*from w w w . ja  va  2 s . c o  m*/

    writer.flush();

    log.debug("end - setDefaultGraphDate()");
}

From source file:com.bluexml.side.util.libs.velocity.VelocityGenerator.java

/**
 * @param va//from w  w  w  .  java2s  . c o m
 * @param fw
 * @throws Exception
 * @throws IOException
 */
public void generate(VelocityAction va, Writer fw) throws Exception, IOException {
    Assert.isNotNull(va.getContext(), "velocity context must be given");
    Assert.isNotNull(va.getTemplatePath(), "velocity template must be given");

    // get Template
    VelocityContext context = va.getContext();
    String templatePath = va.getTemplatePath();
    InputStream template = null;
    File templateFile = new File(templatePath);
    if (templateFile.exists()) {
        // the template is located on File System
        template = new FileInputStream(templateFile);
    } else {
        // resource may be acceded in classPath
        template = getClass().getResourceAsStream(templatePath);
    }

    Assert.isNotNull(template, "velocity template resource can't be loaded");
    // execute
    Velocity.init();
    String s = IOUtils.toString(template);
    Velocity.evaluate(context, fw, "log", s);

    // close streams
    template.close();
    fw.flush();
    fw.close();
}

From source file:com.bsb.cms.commons.template.freemarker.SpringFreemarkerGenerator.java

@Override
public void createFile(String ftlTemplateFile, Map<String, Object> dataMap, String filePath)
        throws TemplateRuntimeException {
    Writer out = null;
    try {//from   w ww. j  av  a2 s. co  m
        String htmlText = FreeMarkerTemplateUtils
                .processTemplateIntoString(configuration.getTemplate(ftlTemplateFile), dataMap);

        filePath = filePath.replace("\\", "/");
        filePath = filePath.replace("//", "/");
        String savePath = StringUtils.substringBeforeLast(filePath, "/");
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        out = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
        out.write(htmlText);
    } catch (IOException e) {
        log.error(e.getMessage());
        throw new TemplateRuntimeException(e.getMessage());
    } catch (TemplateException e) {
        log.error(e.getMessage());
        throw new TemplateRuntimeException(e.getMessage());
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:net.cit.tetrad.resource.SubResource.java

@RequestMapping("/setNowGraphDate.do")
public void setNowGraphDate(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String period = request.getParameter("period");
    String sdateStr = request.getParameter("sdateStr");
    String edateStr = request.getParameter("edateStr");
    String format = "yyyy-MM-dd HH:mm";

    int diffMins = DateUtil.getOffsetMins(edateStr, sdateStr) * -1;
    Date stDate = DateUtil.plusMinute(diffMins);
    Date edDate = new Date();
    Date fullStDate = defaultStartDate(period);

    String strStDate = DateUtil.getCurrentDate(stDate, format);
    String strEdDate = DateUtil.getCurrentDate(edDate, format);
    String strFullStDate = DateUtil.getCurrentDate(fullStDate, format);

    String result = strFullStDate + "|" + strStDate + "|" + strEdDate;

    Writer writer = setResponse(response).getWriter();
    writer.write(result);//from   ww  w  .j  a v a  2 s .  c o  m

    writer.flush();

    log.debug("end - setNowGraphDate()");
}

From source file:com.tek42.perforce.parse.AbstractPerforceTemplate.java

/**
 * Handles the IO for opening a process, writing to it, flushing, closing, and then handling any errors.
 * /*from w  w  w  .ja  v a2s.  co m*/
 * @param object   The perforce object to save
 * @param builder   The builder responsible for saving the object
 * @throws PerforceException   If there is any errors thrown from perforce
 */
@SuppressWarnings("unchecked")
protected void saveToPerforce(Object object, Builder builder) throws PerforceException {
    boolean loop = false;
    boolean attemptLogin = true;

    //StringBuilder response = new StringBuilder();
    do {
        int mesgIndex = -1;//, count = 0;
        Executor p4 = depot.getExecFactory().newExecutor();
        String debugCmd = "";
        try {
            String cmds[] = getExtraParams(builder.getSaveCmd(getP4Exe(), object));

            // for exception reporting...
            for (String cm : cmds) {
                debugCmd += cm + " ";
            }

            // back to our regularly scheduled programming...
            p4.exec(cmds);
            BufferedReader reader = p4.getReader();

            // Maintain a log of what was sent to p4 on std input
            final StringBuilder log = new StringBuilder();

            // Conditional use of std input for saving the perforce entity
            if (builder.requiresStandardInput()) {
                BufferedWriter writer = p4.getWriter();
                Writer fwriter = new FilterWriter(writer) {
                    public void write(String str) throws IOException {
                        log.append(str);
                        out.write(str);
                    }
                };
                builder.save(object, fwriter);
                fwriter.flush();
                fwriter.close();
            }

            String line;
            StringBuilder error = new StringBuilder();
            StringBuilder info = new StringBuilder();
            int exitCode = 0;

            while ((line = reader.readLine()) != null) {

                // Check for authentication errors...
                if (mesgIndex == -1)
                    mesgIndex = checkAuthnErrors(line);

                if (mesgIndex != -1) {
                    error.append(line);

                } else if (line.startsWith("error")) {
                    if (!line.trim().equals("") && (line.indexOf("up-to-date") < 0)
                            && (line.indexOf("no file(s) to resolve") < 0)) {
                        error.append(line.substring(6));
                    }

                } else if (line.startsWith("exit")) {
                    exitCode = Integer.parseInt(line.substring(line.indexOf(" ") + 1, line.length()));

                } else {
                    if (line.indexOf(":") > -1)
                        info.append(line.substring(line.indexOf(":")));
                    else
                        info.append(line);
                }
            }
            reader.close();

            loop = false;
            // If we failed to execute because of an authentication issue, try a p4 login.
            if (mesgIndex == 1 || mesgIndex == 2 || mesgIndex == 6 || mesgIndex == 9) {
                if (attemptLogin) {
                    // password is unset means that perforce isn't using the environment var P4PASSWD
                    // Instead it is using tickets. We must attempt to login via p4 login, then
                    // retry this cmd.
                    p4.close();
                    trustIfSSL();
                    login();
                    loop = true;
                    attemptLogin = false;
                    mesgIndex = -1; // cancel this error for now
                    continue;
                }
            }

            if (mesgIndex != -1 || exitCode != 0) {
                if (error.length() != 0) {
                    error.append("\nFor Command: ").append(debugCmd);
                    if (log.length() > 0) {
                        error.append("\nWith Data:\n===================\n");
                        error.append(log);
                        error.append("\n===================\n");
                    }
                    throw new PerforceException(error.toString());
                }
                throw new PerforceException(info.toString());
            }

        } catch (IOException e) {
            throw new PerforceException("Failed to open connection to perforce", e);
        } finally {
            try {
                p4.getWriter().close();
            } catch (IOException e) {
                //failed to close pipe, but we can't do much about that
            }
            try {
                p4.getReader().close();
            } catch (IOException e) {
                //failed to close pipe, but we can't do much about that
            }
            p4.close();
        }
    } while (loop);
}