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.swingtech.apps.filemgmt.controller.StreamingViewRenderer.java

/**
 * Copy the given byte range of the given input to the given output.
 * // ww w.  j a  v  a2  s .c  o  m
 * @param input
 *            The input to copy the given range to the given output for.
 * @param output
 *            The output to copy the given range from the given input for.
 * @param start
 *            Start of the byte range.
 * @param length
 *            Length of the byte range.
 * @throws IOException
 *             If something fails at I/O level.
 */
private static void copy(InputStream input, OutputStream output, long inputSize, long start, long length)
        throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int read;

    if (inputSize == length) {
        // Write full range.
        while ((read = input.read(buffer)) > 0) {
            output.write(buffer, 0, read);
            output.flush();
        }
    } else {
        input.skip(start);
        long toRead = length;

        while ((read = input.read(buffer)) > 0) {
            if ((toRead -= read) > 0) {
                output.write(buffer, 0, read);
                output.flush();
            } else {
                output.write(buffer, 0, (int) toRead + read);
                output.flush();
                break;
            }
        }
    }
}

From source file:com.adaptris.util.stream.StreamUtil.java

/**
 * Copy from an InputStream to an OutputStream for expected bytes.
 *
 * @param input the input stream to read from
 * @param output the output stream to write from
 * @param expected the number of bytes to copy
 * @throws IOException if there was an error.
 *///from   w  ww  .jav a  2s  . co  m
public static void copyStream(InputStream input, OutputStream output, int expected) throws IOException {
    if (input == null || output == null) {
        return;
    }
    if (expected <= 0) {
        copyStream(input, output);
    } else {
        byte[] bytes = new byte[Math.min(expected, BUFSIZE)];

        int totalRead = 0;
        int bytesRead = 0;

        while (totalRead < expected) {
            bytesRead = input.read(bytes, 0, bytes.length);
            output.write(bytes, 0, bytesRead);
            totalRead = totalRead + bytesRead;
        }
        output.flush();
    }
}

From source file:nc.noumea.mairie.appock.util.StockSpreadsheetExporter.java

public static void createSpreadsheet(Service service, List<ArticleStock> articleStockList,
        CatalogueService catalogueService, OutputStream out)
        throws JAXBException, Docx4JException, IOException {

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet worksheet = workbook.createSheet("Inventaire");

    Integer rowNumber = 0;//from  w  w  w  .  j  av a 2  s .c om
    rowNumber = generateHeader(worksheet, workbook, rowNumber);
    generateLineOfStock(worksheet, workbook, articleStockList, catalogueService, rowNumber);
    autosize(workbook);

    workbook.write(out);
    out.flush();
    out.close();

}

From source file:com.zotoh.core.io.StreamUte.java

private static void safeFlush(OutputStream os) {
    try {/*from   w  w w.j a  va2s.  c o m*/
        if (os != null) {
            os.flush();
        }
    } catch (Exception e) {
    }
}

From source file:Main.java

public static String simplePost(String url, Bundle params, String method)
        throws MalformedURLException, IOException {
    OutputStream os;

    System.setProperty("http.keepAlive", "false");
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " agent");

    conn.setRequestMethod(method);//w  ww.j  a v a  2  s  .  c o m
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    //conn.setRequestProperty("Connection", "Keep-Alive");

    conn.connect();

    os = new BufferedOutputStream(conn.getOutputStream());
    os.write(encodePostParams(params).getBytes());
    os.flush();

    String response = "";
    try {
        response = read(conn.getInputStream());
    } catch (FileNotFoundException e) {
        // Error Stream contains JSON that we can parse to a FB error
        response = read(conn.getErrorStream());
    }
    return response;
}

From source file:com.github.horrorho.inflatabledonkey.file.FileStreamWriter.java

public static boolean copy(InputStream in, OutputStream out, Optional<XFileKey> keyCipher,
        Optional<byte[]> signature, Optional<IOFunction<InputStream, InputStream>> decompress)
        throws IOException {

    Digest digest = signature.flatMap(FileSignature::type).orElse(FileSignature.ONE).newDigest();

    DigestInputStream dis = new DigestInputStream(in, digest);

    InputStream fis = decryptStream(dis, keyCipher);

    if (decompress.isPresent()) {
        logger.info("-- copy() - decompressing");
        fis = decompress.get().apply(fis);
    }//w  w  w .j av  a 2  s.  com

    IOUtils.copyLarge(fis, out, new byte[BUFFER_SIZE]);
    out.flush();

    return testSignature(dis.getDigest(), signature);
}

From source file:org.opentides.util.FileUtil.java

/**
 * Saves multipart file into disk
 * //  ww  w.j a  va 2s  . com
 * @param multipartFile
 * @param dest
 * @return
 * @throws IOException
 */
public static boolean copyMultipartFile(final MultipartFile multipartFile, File dest) throws IOException {
    InputStream inStream = multipartFile.getInputStream();
    OutputStream outStream = new FileOutputStream(dest);

    try {
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, len);
        }
        outStream.flush();
        return true;
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        if (outStream != null) {
            outStream.close();
        }

        if (inStream != null) {
            inStream.close();
        }
    }
}

From source file:com.blade.mvc.multipart.Streams.java

/**
 * Copies the contents of the given {@link InputStream} to the given {@link OutputStream}.
 *
 * @param pIn     The input stream, which is being read. It is guaranteed, that {@link InputStream#close()} is called on
 *                the stream.//from w w  w.  j av  a 2  s .com
 * @param pOut    The output stream, to which data should be written. May be null, in which case the input streams
 *                contents are simply discarded.
 * @param pClose  True guarantees, that {@link OutputStream#close()} is called on the stream. False indicates, that
 *                only {@link OutputStream#flush()} should be called finally.
 * @param pBuffer Temporary buffer, which is to be used for copying data.
 * @return Number of bytes, which have been copied.
 * @throws IOException An I/O error occurred.
 */
public static long copy(InputStream pIn, OutputStream pOut, boolean pClose, byte[] pBuffer) throws IOException {
    OutputStream out = pOut;
    InputStream in = pIn;
    try {
        long total = 0;
        for (;;) {
            int res = in.read(pBuffer);
            if (res == -1) {
                break;
            }
            if (res > 0) {
                total += res;
                if (out != null) {
                    out.write(pBuffer, 0, res);
                }
            }
        }
        if (out != null) {
            if (pClose) {
                out.close();
            } else {
                out.flush();
            }
            out = null;
        }
        in.close();
        in = null;
        return total;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
                /* Ignore me */
            }
        }
        if (pClose && out != null) {
            try {
                out.close();
            } catch (Throwable t) {
                /* ignore me */
            }
        }
    }
}

From source file:org.esigate.http.HttpResponseUtils.java

/**
 * Copied from org.apache.http.entity.InputStreamEntity.writeTo(OutputStream) method but flushes the buffer after
 * each read in order to allow streaming and web sockets.
 * // ww w .j a v  a  2 s .  c o m
 * @param httpEntity
 *            The entity to copy to the OutputStream
 * @param outstream
 *            The OutputStream
 * @throws IOException
 *             If a problem occurs
 */
public static void writeTo(final HttpEntity httpEntity, final OutputStream outstream) throws IOException {
    Args.notNull(outstream, "Output stream");
    final InputStream instream = httpEntity.getContent();
    try {
        final byte[] buffer = new byte[OUTPUT_BUFFER_SIZE];
        int l;
        if (httpEntity.getContentLength() < 0) {
            // consume until EOF
            while ((l = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, l);
                outstream.flush();
                LOG.debug("Flushed {} bytes of data");
            }
        } else {
            // consume no more than length
            long remaining = httpEntity.getContentLength();
            while (remaining > 0) {
                l = instream.read(buffer, 0, (int) Math.min(OUTPUT_BUFFER_SIZE, remaining));
                if (l == -1) {
                    break;
                }
                outstream.write(buffer, 0, l);
                outstream.flush();
                LOG.debug("Flushed {} bytes of data");
                remaining -= l;
            }
        }
    } finally {
        instream.close();
    }
}

From source file:canreg.client.analysis.Tools.java

/**
 * Exports a JFreeChart to a SVG file./*from  w  ww  .j av a  2s  .co  m*/
 *
 * @param chart JFreeChart to export
 * @param bounds the dimensions of the viewport
 * @param svgFile the output file.
 * @throws IOException if writing the svgFile fails.
 */
public static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, bounds);

    // Write svg file
    OutputStream outputStream = new FileOutputStream(svgFile);
    Writer out = new OutputStreamWriter(outputStream, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    outputStream.flush();
    outputStream.close();
}