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.smartsheet.utils.HttpUtils.java

/**
 * Copies an input stream to an output stream, closing the output stream before returning.
 *//* w w w  .ja v a  2s. com*/
private static void copyAndClose(InputStream inStream, OutputStream outStream) throws IOException {
    try {
        byte[] bytes = new byte[ATTACHMENT_BUFFER_SIZE];
        int actualRead;
        while ((actualRead = inStream.read(bytes, 0, ATTACHMENT_BUFFER_SIZE)) != -1) {
            outStream.write(bytes, 0, actualRead);
        }

    } finally {
        outStream.flush();
        outStream.close();
    }
}

From source file:annis.gui.requesthandler.BinaryRequestHandler.java

private static long copy(InputStream from, OutputStream to) throws IOException {
    checkNotNull(from);//ww w  .  j  a v a 2s .c om
    checkNotNull(to);
    byte[] buf = new byte[BUFFER_SIZE];
    long total = 0;
    while (true) {
        int r = from.read(buf);
        if (r == -1) {
            break;
        }
        to.write(buf, 0, r);
        to.flush();
        total += r;
    }
    return total;
}

From source file:com.android.build.gradle.internal.transforms.ExtractJarsTransform.java

private static void extractJar(@NonNull File outJarFolder, @NonNull File jarFile, boolean extractCode)
        throws IOException {
    mkdirs(outJarFolder);/*  www .  j a va2  s  .  c  om*/
    HashSet<String> lowerCaseNames = new HashSet<>();
    boolean foundCaseInsensitiveIssue = false;

    try (Closer closer = Closer.create()) {
        FileInputStream fis = closer.register(new FileInputStream(jarFile));
        ZipInputStream zis = closer.register(new ZipInputStream(fis));
        // loop on the entries of the intermediary package and put them in the final package.
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            try {
                String name = entry.getName();

                // do not take directories
                if (entry.isDirectory()) {
                    continue;
                }

                foundCaseInsensitiveIssue = foundCaseInsensitiveIssue
                        || !lowerCaseNames.add(name.toLowerCase(Locale.US));

                Action action = getAction(name, extractCode);
                if (action == Action.COPY) {
                    File outputFile = new File(outJarFolder, name.replace('/', File.separatorChar));
                    mkdirs(outputFile.getParentFile());

                    try (Closer closer2 = Closer.create()) {
                        java.io.OutputStream outputStream = closer2
                                .register(new BufferedOutputStream(new FileOutputStream(outputFile)));
                        ByteStreams.copy(zis, outputStream);
                        outputStream.flush();
                    }
                }
            } finally {
                zis.closeEntry();
            }
        }

    }

    if (foundCaseInsensitiveIssue) {
        LOGGER.error(
                "Jar '{}' contains multiple entries which will map to "
                        + "the same file on case insensitive file systems.\n"
                        + "This can be caused by obfuscation with useMixedCaseClassNames.\n"
                        + "This build will be incorrect on case insensitive " + "file systems.",
                jarFile.getAbsolutePath());
    }
}

From source file:com.linkedin.d2.quorum.ZKPeer.java

/**
 * Send the 4letterword//from www. j ava 2  s. c  o m
 *
 * @param host- destination host
 * @param port- destination port
 * @param cmd- the 4letterword (stat, srvr,etc. - see
 *          http://zookeeper.apache.org/doc/r3.3.3/zookeeperAdmin.html#sc_zkCommands)
 * @return
 * @throws IOException
 */
public static String sendCommand(String host, int port, String cmd) throws IOException {
    // NOTE: ignore CancelledKeyException in logs caused by
    // https://issues.apache.org/jira/browse/ZOOKEEPER-1237
    Socket sock = new Socket(host, port);
    BufferedReader reader = null;
    try {
        OutputStream outstream = sock.getOutputStream();
        outstream.write(cmd.getBytes());
        outstream.flush();
        sock.shutdownOutput();

        reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        return sb.toString();
    } finally {
        sock.close();
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:JarMaker.java

/**
 * writes the content of the InputStream into the OutputStream
 * @param _in/*from   w w w. ja va2s .  co m*/
 * @param _out
 * @throws IOException
 */
private static synchronized void write(InputStream _in, OutputStream _out) throws IOException {

    byte[] buffer = new byte[1024 * 512];
    int read;
    while (true) {
        read = _in.read(buffer);
        if (read == -1)
            break;

        _out.write(buffer, 0, read);
    }
    _out.flush();
}

From source file:com.android.ide.common.process.MtlProcessExecutor.java

private static ListenableFuture<Integer> grabProcessOutput(@NonNull final Process process,
        @NonNull final ProcessOutput output) {
    final SettableFuture<Integer> result = SettableFuture.create();
    final AtomicReference<Exception> exceptionHolder = new AtomicReference<Exception>();

    /*//from  w w w. j  av  a2s  .c om
     * It looks like on windows process#waitFor() can return before the thread have filled the
     * arrays, so we wait for both threads and the process itself.
     *
     * To make sure everything is complete before setting the future, the thread handling
     * "out" will wait for all its input to be read, will wait for the "err" thread to finish
     * and will wait for the process to finish. Only after all three are done will it set
     * the future and terminate.
     *
     * This means that the future will be set while the "out" thread is still running, but
     * no output is pending and the process has already finished.
     */
    final Thread threadErr = new Thread("stderr") {
        @Override
        public void run() {
            InputStream stderr = process.getErrorStream();
            OutputStream stream = output.getErrorOutput();

            try {
                ByteStreams.copy(stderr, stream);
                stream.flush();
            } catch (IOException e) {
                exceptionHolder.compareAndSet(null, e);
            }
        }
    };

    Thread threadOut = new Thread("stdout") {
        @Override
        public void run() {
            InputStream stdout = process.getInputStream();
            OutputStream stream = output.getStandardOutput();

            try {
                ByteStreams.copy(stdout, stream);
                stream.flush();
            } catch (Exception e) {
                exceptionHolder.compareAndSet(null, e);
            }

            try {
                threadErr.join();
                int processResult = process.waitFor();
                if (exceptionHolder.get() != null) {
                    result.setException(exceptionHolder.get());
                }

                result.set(processResult);
                output.close();
            } catch (Exception e) {
                result.setException(e);
            }
        }
    };

    threadErr.start();
    threadOut.start();

    return result;
}

From source file:FileCopyUtils.java

/**
 * Copy the contents of the given InputStream to the given OutputStream.
 * Closes both streams when done./*  w  w w  .  j a  v  a  2 s.c o m*/
 * @param in the stream to copy from
 * @param out the stream to copy to
 * @return the number of bytes copied
 * @throws IOException in case of I/O errors
 */
public static int copy(InputStream in, OutputStream out) throws IOException {

    try {
        int byteCount = 0;
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        return byteCount;
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException ex) {
            System.out.println("Could not close OutputStream:" + ex);
        }
    }
}

From source file:com.adguard.commons.io.IoUtils.java

/**
 * Copies from input to output stream/*from   ww  w . ja v  a2  s  . c o m*/
 *
 * @param inputStream  Input stream
 * @param outputStream Output stream
 * @param bufferSize   Buffer size
 * @throws IOException
 */
public static void copy(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException {
    byte[] buffer;
    ByteArrayPool.ByteArray array = null;
    if (bufferSize > BUFFER_SIZE) {
        buffer = new byte[bufferSize];
    } else {
        array = BUFFER_POOL.getByteArray();
        buffer = array.getBytes();
    }

    IOUtils.copyLarge(inputStream, outputStream, buffer);
    outputStream.flush();
    if (array != null) {
        array.release();
    }
}

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

public static void readFullyWriteToOutputStream(InputStream in, OutputStream out) throws IOException {
    try {//  www .  j a  va  2s. c o m
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        out.close();
    }
}

From source file:Main.java

public static void writeCString(OutputStream out, String s, int fixedLen) throws IOException {
    byte[] bytes = s.getBytes();
    out.write(bytes.length);//from   w  ww .  j a  v  a2 s.  c o m
    fixedLen -= 1;

    if (fixedLen <= 0)
        return;

    if (fixedLen <= bytes.length) {
        out.write(bytes, 0, fixedLen);
    } else {
        out.write(bytes);
        byte[] fillBytes = new byte[fixedLen - bytes.length];
        Arrays.fill(fillBytes, (byte) 0);
        out.write(fillBytes);
    }

    out.flush();
}