Example usage for java.io OutputStream flush

List of usage examples for java.io OutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:edu.toronto.cs.phenotips.hpoa.ontology.HPO.java

public File getInputFileHandler(String inputLocation, boolean forceUpdate) {
    try {//from w ww  .ja v  a  2s.  c o m
        File result = new File(inputLocation);
        if (!result.exists() || result.length() == 0) {
            String name = inputLocation.substring(inputLocation.lastIndexOf('/') + 1);
            result = getTemporaryFile(name);
            if (!result.exists() || result.length() == 0) {
                BufferedInputStream in = new BufferedInputStream((new URL(inputLocation)).openStream());
                result.createNewFile();
                OutputStream out = new FileOutputStream(result);
                IOUtils.copy(in, out);
                out.flush();
                out.close();
            }
        }
        return result;
    } catch (IOException ex) {
        this.logger.error("Mapping file [{}] not found", inputLocation);
        return null;
    }
}

From source file:com.nominanuda.springmvc.StaticViewResolver.java

public View resolveViewName(String viewName, Locale locale) throws Exception {
    URL url = resolve(viewName, locale);
    if (url == null) {
        return null;
    } else {/*  w  w w . j a  v  a  2 s . c  o m*/
        if (cache) {
            View cv = findCachedView(url);
            if (cv != null) {
                return cv;
            }
        }
        final byte[] barr = io.readAndClose(url.openStream());
        View v = new View() {
            public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
                response.setContentLength(barr.length);
                response.setContentType(contentType);
                OutputStream os = response.getOutputStream();
                os.write(barr);
                os.flush();
            }

            public String getContentType() {
                return contentType;
            }
        };
        if (cache) {
            storeCachedView(url, v);
        }
        return v;
    }
}

From source file:echopoint.tucana.InputStreamDownloadProvider.java

public void writeFile(final OutputStream out) throws IOException {
    status = Status.inprogress;//from   w  w  w . jav a2s . co m

    try {
        IOUtils.copy(stream, out);
        out.flush();
    } catch (IOException e) {
        status = Status.failed;
        throw e;
    }

    status = Status.completed;
}

From source file:io.undertow.server.handlers.ChunkedRequestNotConsumedTestCase.java

@Test
public void testChunkedRequestNotConsumed() throws IOException {
    HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
    TestHttpClient client = new TestHttpClient();
    try {/*from ww  w . j  av a2 s.c  o  m*/
        final Random random = new Random();
        final int seed = random.nextInt();
        System.out.print("Using Seed " + seed);
        random.setSeed(seed);

        for (int i = 0; i < 3; ++i) {
            post.setEntity(new StringEntity("") {
                @Override
                public long getContentLength() {
                    return -1;
                }

                @Override
                public boolean isChunked() {
                    return true;
                }

                @Override
                public void writeTo(OutputStream outstream) throws IOException {
                    outstream.flush();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {

                    }
                    outstream.write(MESSAGE.getBytes(StandardCharsets.US_ASCII));
                }
            });
            HttpResponse result = client.execute(post);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            HttpClientUtils.readResponse(result);
        }
    } finally {

        client.getConnectionManager().shutdown();
    }
}

From source file:com.zb.app.common.interceptor.ExportFileAnnotationInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    if (handler == null || response == null) {
        return;//from w  w w .  ja v a  2  s. c  om
    }

    HandlerMethod hm = (HandlerMethod) handler;

    ExportWordFile exportWordFile = hm.getMethodAnnotation(ExportWordFile.class);
    // word,
    if (exportWordFile != null) {
        String wordName = exportWordFile.value();
        if (StringUtils.isEmpty(wordName)) {
            return;
        }
        wordName = new String(wordName.getBytes(), "ISO8859-1");

        String contentDis = "attachment;filename=" + wordName + ".doc";
        response.setHeader("content-disposition", contentDis);
        response.setContentType("application/msword;");
        response.setCharacterEncoding("UTF-8");
    }

    ExportExcelFile exportExcelFile = hm.getMethodAnnotation(ExportExcelFile.class);
    // excel,
    if (exportExcelFile != null) {
        String xlsName = exportExcelFile.value();
        if (StringUtils.isEmpty(xlsName)) {
            return;
        }
        xlsName = new String(xlsName.getBytes(), "UTF-8");

        List<?> list = (List<?>) modelAndView.getModel().get("list");
        String[] head = (String[]) modelAndView.getModel().get("head");
        modelAndView.clear();

        HSSFWorkbook workbook = ExcelUtils.defBuildExcel(list, xlsName, head);

        if (workbook == null) {
            try {
                response.getOutputStream().print("Not conform to the requirements of data");
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            return;
        }

        response.setHeader("content-disposition", "attachment;filename=" + xlsName + ".xls");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        //  response
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
    }
}

From source file:com.acmemotors.integration.OBD2Serializer.java

/**
 * Convert a CustomOrder object into a byte-stream
 *
 * @param outputStream/*from  w ww . j  ava  2 s  . c o  m*/
 * @throws IOException
 */
@Override
public void serialize(String command, OutputStream outputStream) throws IOException {
    outputStream.write(command.getBytes());
    outputStream.write("\r\n".getBytes());
    outputStream.flush();
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * //from   www. j ava2  s.  c om
 * 
 * @param filePath 
 * @param fileName ??
 * @param inline ??
 * @throws Exception
 */
public static void downloadFile(HttpServletResponse response, File file, String fileName, boolean inline)
        throws Exception {

    OutputStream outp = null;
    FileInputStream br = null;
    int len = 0;
    try {
        br = new FileInputStream(file);
        response.reset();
        outp = response.getOutputStream();
        outp.flush();
        response.setContentType("application/octet-stream");
        response.setContentLength((int) file.length());
        String header = (inline ? "inline" : "attachment") + ";filename="
                + new String(fileName.getBytes(), "ISO8859-1");
        response.addHeader("Content-Disposition", header);
        byte[] buf = new byte[1024];
        while ((len = br.read(buf)) != -1) {
            outp.write(buf, 0, len);
        }
        outp.flush();
        outp.close();
    } finally {
        if (br != null) {
            if (0 == br.available()) {
                br.close();
            }
        }
    }

}

From source file:io.neba.core.mvc.SlingMultipartFile.java

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
    if (dest.exists() && !dest.delete()) {
        throw new IOException(
                "Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted");
    }//  ww  w .  j  a  va 2  s. c om
    final InputStream in = getInputStream();
    final OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
    try {
        copy(in, out);
        out.flush();
    } finally {
        closeQuietly(out);
        closeQuietly(in);
    }
}

From source file:com.simplenoteapp.test.Test.java

/**
 * Base 64 encode the given data and post it to uri.
 * @param uri to which the data is to be posted
 * @param postData string to be converted to UTF-8, then encoded
 * as base 64 and posted to uri//from  w w  w .  j av  a 2 s.  co  m
 * @return content returned from the request
 * @throws IOException
 */
public String postBase64(String uriString, String postData) throws IOException {
    Test.log("postBase64 uri=" + uriString + " data=" + postData);
    byte[] base64 = Base64.encodeBase64(postData.getBytes(), false, false);
    Test.log("base64=" + new String(base64));

    URL url = new URL(uriString);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStream out = conn.getOutputStream();
    out.write(base64);
    out.flush();
    String response = readResponse(conn.getInputStream());
    out.close();
    return response;
}

From source file:org.apache.sshd.TestSpringConfig.java

@Test
public void testSpringConfig() throws Exception {
    JSch sch = new JSch();
    sch.setLogger(new Logger() {
        public boolean isEnabled(int i) {
            return true;
        }/*from   ww w .  j  av  a  2  s .  c  o m*/

        public void log(int i, String s) {
            System.out.println("Log(jsch," + i + "): " + s);
        }
    });
    com.jcraft.jsch.Session s = sch.getSession("smx", "localhost", 8000);
    s.setUserInfo(new UserInfo() {
        public String getPassphrase() {
            return null; //To change body of implemented methods use File | Settings | File Templates.
        }

        public String getPassword() {
            return "smx";
        }

        public boolean promptPassword(String message) {
            return true;
        }

        public boolean promptPassphrase(String message) {
            return false; //To change body of implemented methods use File | Settings | File Templates.
        }

        public boolean promptYesNo(String message) {
            return true;
        }

        public void showMessage(String message) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    });
    s.connect();
    Channel c = s.openChannel("shell");
    c.connect();
    OutputStream os = c.getOutputStream();
    os.write("this is my command".getBytes());
    os.flush();
    Thread.sleep(100);
    c.disconnect();
    s.disconnect();
}