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

public static void writeWString(OutputStream out, String s) throws IOException {
    byte[] bytes = s.getBytes();
    writeShort(out, bytes.length);/*from  ww  w  . j  a  v a2s . co  m*/
    out.write(bytes);
    out.flush();
}

From source file:Main.java

/**
 * Copy the content of the input stream into the output stream, using a
 * temporary byte array buffer whose size is defined by
 * {@link #FILE_BUFFER_SIZE}./*  w ww . j ava 2  s .com*/
 * 
 * @param in
 *          The input stream to copy from.
 * @param out
 *          The output stream to copy to.
 * 
 * @throws java.io.IOException
 *           If any error occurs during the copy.
 */
public static void copyFile(InputStream in, OutputStream out, long length) throws IOException {
    long totalRead = 0;
    byte[] b = new byte[FILE_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) > 0) {
        out.write(b, 0, read);
        out.flush();

        totalRead += read;
        if (totalRead >= length)
            break;
    }
}

From source file:Main.java

/**
 * Writes the contents from the given input stream to the given file.
 *
 * @param in the InputStream to read from
 * @param outputFileName the name of the file to write to
 * @return the URL for the local file/*  w  ww  .ja  v a  2s  . c  o m*/
 */
public static String writeStreamToFile(InputStream in, String outputFileName) throws IOException {
    File file = new File(outputFileName);

    // Create the parent directory.
    file.getParentFile().mkdirs();

    OutputStream out = new FileOutputStream(file);
    try {
        copy(in, out);

        // Return the URL to the output file.
        return file.toURI().toString();
    } finally {
        out.flush();
        out.close();
    }
}

From source file:Main.java

public static void streamToStream(InputStream is, OutputStream os) throws IOException {
    final byte[] buffer = new byte[256];
    try {//  ww w .ja va2s.  c om
        int n;
        while ((n = is.read(buffer)) != -1) {
            os.write(buffer, 0, n);
        }
    } finally {
        os.flush();
        os.close();
        is.close();
    }
}

From source file:Main.java

public static void writeFanout(List<String> childCrcs, long actualContentLength, OutputStream bout)
        throws IOException {
    bout.write((actualContentLength + "").getBytes());
    for (String l : childCrcs) {
        bout.write("\n".getBytes());
        bout.write(l.getBytes());/* ww  w  .  j a v  a2s .  c o  m*/
    }
    bout.flush();
}

From source file:Main.java

public static void copyFile(InputStream in, OutputStream out) {
    try {//from w  w w.ja v  a 2  s . c  o  m
        byte[] b = new byte[2 * 1024 * 1024]; //2M memory
        int len = -1;
        while ((len = in.read(b)) > 0) {
            out.write(b, 0, len);
            out.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(in, out);
    }
}

From source file:Main.java

public static void writeInt(OutputStream out, long s) throws IOException {
    byte[] buffer = new byte[] { (byte) ((s >> 24) & 0xff), (byte) ((s >> 16) & 0xff), (byte) ((s >> 8) & 0xff),
            (byte) (s & 0xff) };

    out.write(buffer);// w  w  w. jav  a2s . co m
    out.flush();
    buffer = null;
}

From source file:com.usefullc.platform.common.utils.IOUtils.java

/**
 * /*  w w w .  j av  a  2 s  . c  o m*/
 * 
 * @param path
 * @param fileName
 * @param response
 * @return
 */
public static void download(String path, String fileName, HttpServletResponse response) {
    try {

        if (StringUtils.isEmpty(path)) {
            throw new IllegalArgumentException("?");
        } else {
            File file = new File(path);
            if (!file.exists()) {
                throw new IllegalArgumentException("??");
            }
        }
        if (StringUtils.isEmpty(fileName)) {
            throw new IllegalArgumentException("???");
        }
        if (response == null) {
            throw new IllegalArgumentException("response ?");
        }

        // path
        File file = new File(path);

        // ??
        InputStream fis = new BufferedInputStream(new FileInputStream(path));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // response
        response.reset();

        // ??linux ?  linux utf-8,windows  GBK)
        String defaultEncoding = System.getProperty("file.encoding");
        if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) {

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1"));
        } else {
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
        }

        // responseHeader
        response.addHeader("Content-Length", "" + file.length());
        response.setContentType("application/octet-stream");
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:fitnesse.http.ResponseParser.java

public static ResponseParser performHttpRequest(String hostname, int hostPort, RequestBuilder builder)
        throws IOException {
    Socket socket = new Socket(hostname, hostPort);
    OutputStream socketOut = socket.getOutputStream();
    InputStream socketIn = socket.getInputStream();
    builder.send(socketOut);/*from  www  .j a  va 2 s.  c  o m*/
    socketOut.flush();
    try {
        return new ResponseParser(socketIn);
    } finally {
        socketOut.close();
        socket.close();
    }
}

From source file:Main.java

/**
 * Dump the database file to external storage
 *
 * @param packageName//from   ww w . jav  a2s.  c o  m
 * @param fileName
 * @throws IOException
 */
public static void dumpDatabase(String packageName, String fileName) throws IOException {
    File dbFile = new File("/data/data/" + packageName + "/databases/" + fileName);
    if (dbFile.exists()) {
        FileInputStream fis = new FileInputStream(dbFile);
        String outFileName = Environment.getExternalStorageDirectory() + "/" + fileName;
        OutputStream output = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
        output.close();
        fis.close();
    }
}