Java InputStream Copy copyLarge(InputStream in, OutputStream out, byte[] buffer)

Here you can find the source of copyLarge(InputStream in, OutputStream out, byte[] buffer)

Description

Copies bytes from a large (over 2GB) InputStream to an OutputStream via a byte buffer.

License

Apache License

Parameter

Parameter Description
in the source
out the destination
buffer the buffer

Exception

Parameter Description
IOException if one occurs while reading or writing

Return

the number of bytes copied

Declaration

public static long copyLarge(InputStream in, OutputStream out, byte[] buffer) throws IOException 

Method Source Code

//package com.java2s;
/*// w ww  .  ja v a 2  s  . com
   Copyright 2011, Lightbox Technologies, Inc
    
   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.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    /**
     * Copies bytes from a large (over 2GB) <code>InputStream</code> to an
     * <code>OutputStream</code> via a <code>byte</code> buffer. This
     * method buffers input internally, so the input stream should not
     * be a <code>BufferedInputStream</code>.
     *
     * @param in the source
     * @param out the destination
     * @param buffer the buffer
     * @return the number of bytes copied
     * @throws IOException if one occurs while reading or writing
     */
    public static long copyLarge(InputStream in, OutputStream out, byte[] buffer) throws IOException {
        long count = 0;
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    /**
     * Reads from an {@link InputStream} to a byte array. This will always
     * completely fill the byte array, unless there are no more bytes to
     * read from the stream.
     *
     * @param in the input stream from which to read
     * @param buf the byte array to fill
     * @return the number of bytes read, of <code>-1</code> if at the end of
     * the stream
     *
     * @throws IOException if one occurs while reading
     */
    public static int read(InputStream in, byte[] buf) throws IOException {
        int num;
        int off = 0;
        while (off < buf.length && (num = in.read(buf, off, buf.length - off)) != -1) {
            off += num;
        }

        // This will read at least one byte if there are any to be read,
        // so bytes read cannot be zero.
        return off == 0 ? -1 : off;
    }
}

Related

  1. copyBytes(InputStream in, OutputStream out)
  2. copyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)
  3. copyBytes(InputStream in, OutputStream out, int buffSize)
  4. copyLarge(final InputStream input, final OutputStream output)
  5. copyLarge(final InputStream input, final OutputStream output)
  6. copyLarge(InputStream input, File outputFile)
  7. copyLarge(InputStream input, OutputStream output)
  8. copyLarge(InputStream input, OutputStream output)
  9. copyLarge(InputStream input, OutputStream output)