Java OutputStream Write copyBytes(InputStream iStream, OutputStream oStream)

Here you can find the source of copyBytes(InputStream iStream, OutputStream oStream)

Description

Write all bytes available from Input- to OutputStream in pieces of DEFAULT_BUFFER_SIZE.

License

Apache License

Parameter

Parameter Description
iStream a parameter
oStream a parameter

Exception

Parameter Description
IOException an exception

Return

total bytes copied

Declaration

public final static long copyBytes(InputStream iStream, OutputStream oStream) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w  w  w.  j  a v  a 2s .co  m
 * Copyright 2010 the original author or authors.
 *
 * 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.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * A default buffer size, could be used f.e. by copying bytes from stream to stream.
     */
    public static final int DEFAULT_BUFFER_SIZE = 4 * 1024;

    /**
     * Write all bytes available from Input- to OutputStream in pieces of DEFAULT_BUFFER_SIZE. <br/>
     * Block until it reads -1.
     * 
     * 
     * @param iStream
     * @param oStream
     * @return total bytes copied
     * @throws IOException
     */
    public final static long copyBytes(InputStream iStream, OutputStream oStream) throws IOException {
        long totalLength = 0;
        int length = -1;
        byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
        while ((length = iStream.read(bytes)) != -1) {
            oStream.write(bytes, 0, length);
            totalLength += length;
        }
        return totalLength;
    }

    /**
     * Write length bytes available from Input- to OutputStream in pieces of DEFAULT_BUFFER_SIZE.
     * 
     * @param iStream
     * @param oStream
     * @param length
     * @throws IOException
     */
    public static void copyBytes(InputStream iStream, OutputStream oStream, long length) throws IOException {
        byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
        int pieceLength = bytes.length;
        while (length > 0) {
            if (length < bytes.length) {
                pieceLength = iStream.read(bytes, 0, (int) length);
            } else {
                pieceLength = iStream.read(bytes, 0, bytes.length);
            }

            oStream.write(bytes, 0, pieceLength);
            length -= pieceLength;
        }
    }
}

Related

  1. copyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
  2. copyBytes(InputStream input, int size, OutputStream output)
  3. copyBytes(InputStream inputStream, OutputStream outputStream, int size)
  4. copyBytes(InputStream is, DataOutputStream[] os, long numBytes)
  5. copyBytes(InputStream is, OutputStream bytes)
  6. copyBytesAndClose(final InputStream is, final OutputStream os)
  7. copyBytesToFile(byte[] bytes, File outputFile)
  8. copyBytesToStream(ByteArrayOutputStream from, OutputStream to)
  9. copyBytesToStream(InputStream inputStream, OutputStream outputStream, int length)