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:com.jsonstore.util.JSONStoreUtil.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);

    ZipEntry entry;//from  ww w.  j a v  a 2  s.  c o  m
    while ((entry = zin.getNextEntry()) != null) {
        String extractFilePath = entry.getName();
        if (extractFilePath.startsWith("/") || extractFilePath.startsWith("\\")) {
            extractFilePath = extractFilePath.substring(1);
        }
        File extractFile = new File(targetDir.getPath() + File.separator + extractFilePath);
        if (entry.isDirectory()) {
            if (!extractFile.exists()) {
                extractFile.mkdirs();
            }
            continue;
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
        }

        // if not directory instead of the previous check and continue
        OutputStream os = new BufferedOutputStream(new FileOutputStream(extractFile));
        copyFile(zin, os);
        os.flush();
        os.close();
    }
}

From source file:brainleg.app.util.AppWeb.java

/**
 * Copied from ITNProxy/*  w  w  w.j a v a 2s.c  o  m*/
 */
private static HttpURLConnection doPost(String url, byte[] bytes) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) HttpConfigurable.getInstance().openConnection(url);

    connection.setReadTimeout(60 * 1000);
    connection.setConnectTimeout(10 * 1000);
    connection.setRequestMethod(HTTP_POST);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestProperty(HTTP_CONTENT_TYPE, String.format("%s; charset=%s", HTTP_WWW_FORM, ENCODING));
    connection.setRequestProperty(HTTP_CONTENT_LENGTH, Integer.toString(bytes.length));

    OutputStream out = new BufferedOutputStream(connection.getOutputStream());
    try {
        out.write(bytes);
        out.flush();
    } finally {
        out.close();
    }

    return connection;
}

From source file:com.wisdombud.right.client.common.HttpKit.java

/**
 * Send POST request/*w  ww .ja  v  a 2  s . c  o m*/
 */
public static String post(String url, Map<String, String> queryParas, String data,
        Map<String, String> headers) {
    HttpURLConnection conn = null;
    try {
        conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
        conn.connect();

        final OutputStream out = conn.getOutputStream();
        out.write(data != null ? data.getBytes(CHARSET) : null);
        out.flush();
        out.close();

        return readResponseString(conn);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:Main.java

public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

    try {//from   ww  w.j av  a 2 s .com
        InputStream in = context.getAssets().open(filename);
        int max = in.available();
        if (pd != null) {
            pd.setMax(max);
        }

        File file = new File(destfilename);
        OutputStream out = new FileOutputStream(file);
        byte[] byt = new byte[1024];
        int len = 0;
        int total = 0;
        while ((len = in.read(byt)) != -1) {
            out.write(byt, 0, len);
            total += len;
            if (pd != null) {
                pd.setProgress(total);
            }
        }
        out.flush();
        out.close();
        in.close();

        return file;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:in.goahead.apps.util.URLUtils.java

public static void DownloadStream(InputStream inputStream, String outputFile, long skipLength)
        throws IOException {
    File outputFileObj = new File(outputFile);
    boolean appendStreamData = false;
    if (skipLength > 0) {
        appendStreamData = true;/*from www . ja  v a2s  . c  om*/
        skipLength = outputFileObj.length();
    }
    OutputStream outputStream = new FileOutputStream(outputFile, appendStreamData);
    //   if(inputStream.skip(skipLength) == skipLength) {
    DownloadStream(inputStream, outputStream);
    //   }
    outputStream.flush();
    outputStream.close();
}

From source file:Main.java

public static void decompress(byte[] data, int off, int len, OutputStream out) {
    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  w w  w .  j a v a  2s  .com*/
    decompresser.setInput(data, off, len);
    byte[] buf = new byte[1024];

    try {
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            out.write(buf, 0, i);
            out.flush();
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        decompresser.end();
    }
}

From source file:gridool.db.partitioning.phihash.csv.distmm.InMemoryIndexHelper.java

/**
 * Synchronization is required.//from  ww  w.j a v  a2 s . c om
 */
public static void writeToFile(final InputStream in) throws IOException {
    final byte[] recordBuf = new byte[2048]; // big buffer enough for a record
    final Map<String, OutputStream> outputMap = new HashMap<String, OutputStream>(12);
    while (in.available() > 0) {
        String fkIdxName = IOUtils.readString(in);
        int bucket = IOUtils.readInt(in);
        int recordlen = IOUtils.readInt(in);
        IOUtils.readFully(in, recordBuf, 0, recordlen);

        OutputStream out = prepareFkOutputStream(fkIdxName, bucket, outputMap);
        out.write(recordBuf, 0, recordlen);
    }
    for (OutputStream out : outputMap.values()) {
        out.flush();
        out.close();
    }
}

From source file:Main.java

public static File writeToSDFromInput(String path, String fileName, InputStream input) {

    File file = null;/*from   w w  w. j  a  v a2s.  co m*/
    OutputStream output = null;
    try {
        file = createFileInSDCard(fileName, path);
        output = new FileOutputStream(file, false);
        byte buffer[] = new byte[4 * 1024];
        int temp;
        while ((temp = input.read(buffer)) != -1) {
            output.write(buffer, 0, temp);
        }
        output.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return file;
}

From source file:com.chicm.cmraft.rpc.PacketUtils.java

private static int writeRpc_backup(SocketChannel channel, Message header, Message body, int totalSize)
        throws IOException {
    // writing total size so that server can read all request data in one read
    LOG.debug("total size:" + totalSize);
    long t = System.currentTimeMillis();
    OutputStream os = Channels.newOutputStream(channel);
    writeIntToStream(totalSize, os);/*from   ww  w .  j  a v a 2s  . c o m*/
    header.writeDelimitedTo(os);
    if (body != null)
        body.writeDelimitedTo(os);
    os.flush();
    LOG.debug("" + (System.currentTimeMillis() - t) + " ms");
    LOG.debug("flushed:" + totalSize);
    return totalSize;
}

From source file:Main.java

/**
 * Copy a stream from source to destination
 *///  w  w  w.j  a  v a 2  s.  c o m
private static void copyStream(InputStream source, OutputStream dest) throws IOException {
    int bytes;
    byte[] buffer;
    int BUFFER_SIZE = 1024;
    buffer = new byte[BUFFER_SIZE];
    while ((bytes = source.read(buffer)) != -1) {
        if (bytes == 0) {
            bytes = source.read();
            if (bytes < 0) {
                break;
            }
            dest.write(bytes);
            dest.flush();
            continue;
        }

        dest.write(buffer, 0, bytes);
        dest.flush();
    }
}