Java OutputStream Write writeStream(InputStream in, File file)

Here you can find the source of writeStream(InputStream in, File file)

Description

Writes all data that can be read from the supplied InputStream to the specified file.

License

Open Source License

Parameter

Parameter Description
in An InputStream.
file The file to write the data to.

Exception

Parameter Description
IOException If an I/O error occurred.

Declaration

public static void writeStream(InputStream in, File file) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class Main {
    /**//  ww  w  .  j av a2  s.c  o m
     * Writes all data that can be read from the supplied InputStream to the
     * specified file.
     * 
     * @param in
     *        An InputStream.
     * @param file
     *        The file to write the data to.
     * @throws IOException
     *         If an I/O error occurred.
     */
    public static void writeStream(InputStream in, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);

        try {
            transfer(in, out);
        } finally {
            try {
                out.flush();
            } finally {
                out.close();
            }
        }
    }

    /**
     * Transfers all bytes that can be read from <tt>in</tt> to <tt>out</tt>.
     * 
     * @param in
     *        The InputStream to read data from.
     * @param out
     *        The OutputStream to write data to.
     * @return The total number of bytes transfered.
     */
    public static final long transfer(InputStream in, OutputStream out) throws IOException {
        long totalBytes = 0;
        int bytesInBuf = 0;
        byte[] buf = new byte[4096];

        while ((bytesInBuf = in.read(buf)) != -1) {
            out.write(buf, 0, bytesInBuf);
            totalBytes += bytesInBuf;
        }

        return totalBytes;
    }

    /**
     * Writes all bytes from an <tt>InputStream</tt> to a file.
     * 
     * @param in
     *        The <tt>InputStream</tt> containing the data to write to the file.
     * @param file
     *        The file to write the data to.
     * @return The total number of bytes written.
     * @throws IOException
     *         If an I/O error occured while trying to write the data to the
     *         file.
     */
    public static final long transfer(InputStream in, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);

        try {
            return transfer(in, out);
        } finally {
            out.close();
        }
    }

    /**
     * Transfers all characters that can be read from <tt>in</tt> to <tt>out</tt>
     * .
     * 
     * @param in
     *        The Reader to read characters from.
     * @param out
     *        The Writer to write characters to.
     * @return The total number of characters transfered.
     */
    public static final long transfer(Reader in, Writer out) throws IOException {
        long totalChars = 0;
        int charsInBuf = 0;
        char[] buf = new char[4096];

        while ((charsInBuf = in.read(buf)) != -1) {
            out.write(buf, 0, charsInBuf);
            totalChars += charsInBuf;
        }

        return totalChars;
    }

    /**
     * Writes all characters from a <tt>Reader</tt> to a file using the default
     * character encoding.
     * 
     * @param reader
     *        The <tt>Reader</tt> containing the data to write to the file.
     * @param file
     *        The file to write the data to.
     * @return The total number of characters written.
     * @throws IOException
     *         If an I/O error occured while trying to write the data to the
     *         file.
     * @see java.io.FileWriter
     */
    public static final long transfer(Reader reader, File file) throws IOException {
        FileWriter writer = new FileWriter(file);

        try {
            return transfer(reader, writer);
        } finally {
            writer.close();
        }
    }
}

Related

  1. serializeUUID(OutputStream out, UUID id)
  2. writeStream(byte[] data, OutputStream os)
  3. writeStream(byte[] data, OutputStream out)
  4. writeStream(final OutputStream out, final String content, final boolean close)
  5. writeStream(InputStream _from, Writer _printTo)
  6. writeStream(InputStream inputStream, OutputStream out)
  7. writeStream(OutputStream out, byte[] data)
  8. writeStream(OutputStream output, String dataStr)
  9. writeStream(OutputStream outputStream, InputStream inputStream, byte[] buffer)