Java OutputStream Write Byte Array writeBytes(byte[] data, OutputStream out)

Here you can find the source of writeBytes(byte[] data, OutputStream out)

Description

write Bytes

License

Open Source License

Declaration

public static void writeBytes(byte[] data, OutputStream out) throws IOException 

Method Source Code


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

import java.io.ByteArrayInputStream;

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 {
    public static void writeBytes(byte[] data, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        try {/*from ww  w . j  a  v  a  2s .c  om*/
            writeBytes(data, out);
        } finally {
            out.close();
        }
    }

    public static void writeBytes(byte[] data, OutputStream out) throws IOException {
        transfer(new ByteArrayInputStream(data), out);
    }

    /**
     * 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. writeBytes(byte[] data, OutputStream out)
  2. writeBytes(byte[] data, OutputStream stream)
  3. writeBytes(int[] data, OutputStream out)
  4. writeBytes(OutputStream os, byte[] b)