Here you can find the source of writeBytesToStream(byte[] data, OutputStream os)
Parameter | Description |
---|---|
data | the byte array to write. |
os | the output stream to write to. |
Parameter | Description |
---|---|
IOException | if an I/O error occurs. |
public static void writeBytesToStream(byte[] data, OutputStream os) throws IOException
//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); } } }