Java OutputStream Write Byte Array writeBytesToStream(byte[] data, OutputStream os)

Here you can find the source of writeBytesToStream(byte[] data, OutputStream os)

Description

Write a byte array into an output stream.

License

Apache License

Parameter

Parameter Description
data the byte array to write.
os the output stream to write to.

Exception

Parameter Description
IOException if an I/O error occurs.

Declaration

public static void writeBytesToStream(byte[] data, OutputStream os)
        throws IOException 

Method Source Code

//package com.java2s;
/*//from w  w w. j a v  a  2  s .  c  o  m
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

public class Main {
    /**
     * Maximum buffer size for reading class files.
     */
    private static final int TEMP_BUFFER_SIZE = 32 * 1024;

    /**
     * Write a byte array into an output stream.
     * @param data the byte array to write.
     * @param os the output stream to write to.
     * @throws IOException if an I/O error occurs.
     */
    public static void writeBytesToStream(byte[] data, OutputStream os)
            throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        copyStream(bais, os);
        bais.close();
    }

    /**
     * Copy the data read from the specified input stream to the specified output stream. 
     * @param is the input stream to read from.
     * @param os the output stream to write to.
     * @throws IOException if an I/O error occurs.
     */
    public static void copyStream(InputStream is, OutputStream os)
            throws IOException {
        byte[] bytes = new byte[TEMP_BUFFER_SIZE];
        while (true) {
            int n = is.read(bytes);
            if (n <= 0)
                break;
            os.write(bytes, 0, n);
        }
    }
}

Related

  1. writeBytes(OutputStream out, byte[] data)
  2. writeBytes(OutputStream output, Object value)
  3. writeBytes(OutputStream outputStream, byte[] data)
  4. writeBytesToStream(byte[] bytes, OutputStream os, boolean printStackTraceOnError)
  5. writeBytesToStream(byte[] bytes, OutputStream outputStream)