Java InputStream Copy copyBytes(final byte[] bytes, final OutputStream outputStream)

Here you can find the source of copyBytes(final byte[] bytes, final OutputStream outputStream)

Description

copy Bytes

License

Open Source License

Declaration

public static void copyBytes(final byte[] bytes, final OutputStream outputStream) throws IOException 

Method Source Code

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

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.Reader;

import java.io.StringWriter;
import java.io.Writer;

public class Main {
    private static final int IoBufferSize = 1024;
    private static final String Encoding = "UTF-8";

    public static void copyBytes(final byte[] bytes, final OutputStream outputStream) throws IOException {
        outputStream.write(bytes, 0, bytes.length);
        outputStream.flush();/*from w ww. j a  v  a2s .c o m*/
    }

    /**
     * Writes bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
     * 
     * @param data
     *            the byte array to write, do not modify during output, null
     *            ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(byte[] data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    /**
     * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
     * using the default character Encoding of the platform.
     * <p>
     * This method uses {@link String#String(byte[])}.
     * 
     * @param data
     *            the byte array to write, do not modify during output, null
     *            ignored
     * @param output
     *            the <code>Writer</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(byte[] data, Writer output) throws IOException {
        if (data != null) {
            output.write(new String(data));
        }
    }

    /**
     * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
     * using the specified character Encoding.
     * <p>
     * Character Encoding names can be found at <a
     * href="http://www.iana.org/assignments/character-sets">IANA</a>.
     * <p>
     * This method uses {@link String#String(byte[], String)}.
     * 
     * @param data
     *            the byte array to write, do not modify during output, null
     *            ignored
     * @param output
     *            the <code>Writer</code> to write to
     * @param encoding
     *            the Encoding to use, null means platform default
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(byte[] data, Writer output, String encoding) throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(data, output);
            } else {
                output.write(new String(data, encoding));
            }
        }
    }

    /**
     * Writes chars from a <code>char[]</code> to bytes on an
     * <code>OutputStream</code>.
     * <p>
     * This method uses {@link String#String(char[])} and
     * {@link String#getBytes()}.
     * 
     * @param data
     *            the char array to write, do not modify during output, null
     *            ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(char[] data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(new String(data).getBytes());
        }
    }

    /**
     * Writes chars from a <code>char[]</code> to bytes on an
     * <code>OutputStream</code> using the specified character Encoding.
     * <p>
     * Character Encoding names can be found at <a
     * href="http://www.iana.org/assignments/character-sets">IANA</a>.
     * <p>
     * This method uses {@link String#String(char[])} and
     * {@link String#getBytes(String)}.
     * 
     * @param data
     *            the char array to write, do not modify during output, null
     *            ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @param encoding
     *            the Encoding to use, null means platform default
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(char[] data, OutputStream output, String encoding) throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(data, output);
            } else {
                output.write(new String(data).getBytes(encoding));
            }
        }
    }

    /**
     * Writes chars from a <code>char[]</code> to a <code>Writer</code> using
     * the default character Encoding of the platform.
     * 
     * @param data
     *            the char array to write, do not modify during output, null
     *            ignored
     * @param output
     *            the <code>Writer</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(char[] data, Writer output) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    /**
     * Writes chars from a <code>String</code> to bytes on an
     * <code>OutputStream</code> using the default character Encoding of the
     * platform.
     * <p>
     * This method uses {@link String#getBytes()}.
     * 
     * @param data
     *            the <code>String</code> to write, null ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(String data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(data.getBytes());
        }
    }

    /**
     * Writes chars from a <code>String</code> to bytes on an
     * <code>OutputStream</code> using the specified character Encoding.
     * <p>
     * Character Encoding names can be found at <a
     * href="http://www.iana.org/assignments/character-sets">IANA</a>.
     * <p>
     * This method uses {@link String#getBytes(String)}.
     * 
     * @param data
     *            the <code>String</code> to write, null ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @param encoding
     *            the Encoding to use, null means platform default
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(String data, OutputStream output, String encoding) throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(data, output);
            } else {
                output.write(data.getBytes(encoding));
            }
        }
    }

    /**
     * Writes chars from a <code>String</code> to a <code>Writer</code>.
     * 
     * @param data
     *            the <code>String</code> to write, null ignored
     * @param output
     *            the <code>Writer</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(String data, Writer output) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    /**
     * Writes chars from a <code>AppendingStringBuffer</code> to bytes on an
     * <code>OutputStream</code> using the default character Encoding of the
     * platform.
     * <p>
     * This method uses {@link String#getBytes()}.
     * 
     * @param data
     *            the <code>AppendingStringBuffer</code> to write, null ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(StringBuffer data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(data.toString().getBytes());
        }
    }

    /**
     * Writes chars from a <code>AppendingStringBuffer</code> to bytes on an
     * <code>OutputStream</code> using the specified character Encoding.
     * <p>
     * Character Encoding names can be found at <a
     * href="http://www.iana.org/assignments/character-sets">IANA</a>.
     * <p>
     * This method uses {@link String#getBytes(String)}.
     * 
     * @param data
     *            the <code>AppendingStringBuffer</code> to write, null ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @param encoding
     *            the Encoding to use, null means platform default
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(data, output);
            } else {
                output.write(data.toString().getBytes(encoding));
            }
        }
    }

    /**
     * Writes chars from a <code>AppendingStringBuffer</code> to a
     * <code>Writer</code>.
     * 
     * @param data
     *            the <code>AppendingStringBuffer</code> to write, null ignored
     * @param output
     *            the <code>Writer</code> to write to
     * @throws NullPointerException
     *             if output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since 1.1
     */
    public static void write(StringBuffer data, Writer output) throws IOException {
        if (data != null) {
            output.write(data.toString());
        }
    }

    /**
     * Get the contents of an <code>InputStream</code> as a String using the
     * default character Encoding of the platform.
     * <p>
     * This method buffers the input internally, so there is no need to use a
     * <code>BufferedInputStream</code>.
     * 
     * @param input
     *            the <code>InputStream</code> to read from
     * @return the requested String
     * @throws NullPointerException
     *             if the input is null
     * @throws IOException
     *             if an I/O error occurs
     */
    public static String toString(InputStream input) throws IOException {
        StringWriter sw = new StringWriter();
        copy(input, sw);
        return sw.toString();
    }

    /**
     * Get the contents of an <code>InputStream</code> as a String using the
     * specified character Encoding.
     * <p>
     * Character Encoding names can be found at <a
     * href="http://www.iana.org/assignments/character-sets">IANA</a>.
     * <p>
     * This method buffers the input internally, so there is no need to use a
     * <code>BufferedInputStream</code>.
     * 
     * @param input
     *            the <code>InputStream</code> to read from
     * @param encoding
     *            the Encoding to use, null means platform default
     * @return the requested String
     * @throws NullPointerException
     *             if the input is null
     * @throws IOException
     *             if an I/O error occurs
     */
    public static String toString(InputStream input, String encoding) throws IOException {
        StringWriter sw = new StringWriter();
        copy(input, sw, encoding);
        return sw.toString();
    }

    /**
     * Get the contents of a <code>Reader</code> as a String.
     * <p>
     * This method buffers the input internally, so there is no need to use a
     * <code>BufferedReader</code>.
     * 
     * @param input
     *            the <code>Reader</code> to read from
     * @return the requested String
     * @throws NullPointerException
     *             if the input is null
     * @throws IOException
     *             if an I/O error occurs
     */
    public static String toString(Reader input) throws IOException {
        StringWriter sw = new StringWriter();
        copy(input, sw);
        return sw.toString();
    }

    /**
     * Copies input stream to output stream using buffer. Streams dont have to
     * be wrapped to buffered, since copying is already optimizied.
     */
    public static int copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[IoBufferSize];
        int count = 0;
        int read;
        while (true) {
            read = input.read(buffer, 0, IoBufferSize);
            if (read == -1) {
                break;
            }
            output.write(buffer, 0, read);
            count += read;
        }
        return count;
    }

    /**
     * Copies specified number of bytes from input stream to output stream using
     * buffer.
     */
    public static int copy(InputStream input, OutputStream output, int byteCount) throws IOException {
        byte buffer[] = new byte[IoBufferSize];
        int count = 0;
        int read;
        while (byteCount > 0) {
            if (byteCount < IoBufferSize) {
                read = input.read(buffer, 0, byteCount);
            } else {
                read = input.read(buffer, 0, IoBufferSize);
            }
            if (read == -1) {
                break;
            }
            byteCount -= read;
            count += read;
            output.write(buffer, 0, read);
        }
        return count;
    }

    /**
     * Copies input stream to writer using buffer.
     */
    public static void copy(InputStream input, Writer output) throws IOException {
        copy(input, output, Encoding);
    }

    /**
     * Copies specified number of bytes from input stream to writer using
     * buffer.
     */
    public static void copy(InputStream input, Writer output, int byteCount) throws IOException {
        copy(input, output, Encoding, byteCount);
    }

    /**
     * Copies input stream to writer using buffer and specified Encoding.
     */
    public static void copy(InputStream input, Writer output, String encoding) throws IOException {
        copy(new InputStreamReader(input, encoding), output);
    }

    /**
     * Copies specified number of bytes from input stream to writer using buffer
     * and specified Encoding.
     */
    public static void copy(InputStream input, Writer output, String encoding, int byteCount) throws IOException {
        copy(new InputStreamReader(input, encoding), output, byteCount);
    }

    /**
     * Copies reader to output stream using buffer.
     */
    public static void copy(Reader input, OutputStream output) throws IOException {
        copy(input, output, Encoding);
    }

    /**
     * Copies specified number of characters from reader to output stream using
     * buffer.
     */
    public static void copy(Reader input, OutputStream output, int charCount) throws IOException {
        copy(input, output, Encoding, charCount);
    }

    /**
     * Copies reader to output stream using buffer and specified Encoding.
     */
    public static void copy(Reader input, OutputStream output, String encoding) throws IOException {
        Writer out = new OutputStreamWriter(output, encoding);
        copy(input, out);
        out.flush();
    }

    /**
     * Copies specified number of characters from reader to output stream using
     * buffer and specified Encoding.
     */
    public static void copy(Reader input, OutputStream output, String encoding, int charCount) throws IOException {
        Writer out = new OutputStreamWriter(output, encoding);
        copy(input, out, charCount);
        out.flush();
    }

    /**
     * Copies reader to writer using buffer. Streams dont have to be wrapped to
     * buffered, since copying is already optimized.
     */
    public static int copy(Reader input, Writer output) throws IOException {
        char[] buffer = new char[IoBufferSize];
        int count = 0;
        int read;
        while ((read = input.read(buffer, 0, IoBufferSize)) >= 0) {
            output.write(buffer, 0, read);
            count += read;
        }
        output.flush();
        return count;
    }

    /**
     * Copies specified number of characters from reader to writer using buffer.
     */
    public static int copy(Reader input, Writer output, int charCount) throws IOException {
        char buffer[] = new char[IoBufferSize];
        int count = 0;
        int read;
        while (charCount > 0) {
            if (charCount < IoBufferSize) {
                read = input.read(buffer, 0, charCount);
            } else {
                read = input.read(buffer, 0, IoBufferSize);
            }
            if (read == -1) {
                break;
            }
            charCount -= read;
            count += read;
            output.write(buffer, 0, read);
        }
        return count;
    }
}

Related

  1. copyByte(InputStream input, OutputStream output)
  2. copyBytes(DataInput in, DataOutput out, int length, byte[] buf)
  3. copyBytes(final InputStream in, final OutputStream out, final int buffSize, final boolean close)
  4. copyBytes(final InputStream is, final OutputStream os)
  5. copyBytes(InputStream in, OutputStream out)
  6. copyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)