Java InputStream Read Bytes readBytes(InputStream in, byte[] buffer, int maxBytesAtOnce)

Here you can find the source of readBytes(InputStream in, byte[] buffer, int maxBytesAtOnce)

Description

Read a number of bytes from an InputStream into the given byte array.

License

Open Source License

Parameter

Parameter Description
in the InputStream to read from
buffer the byte array to write into (must not be null )
maxBytesAtOnce the maximum number of bytes that is read per invocation of InputStream#read(byte[],int,int)

Exception

Parameter Description
IOException If the end of the InputStream is reached before buffer.length bytes are reador if the first byte cannot be read for any reason other than end of file, or if the input stream has beenclosed, or if some other I/O error occurs
NullPointerException if buffer is null

Declaration

public static void readBytes(InputStream in, byte[] buffer,
        int maxBytesAtOnce) throws IOException 

Method Source Code

//package com.java2s;
/*/*w w  w.j  a  va  2  s  . c  o  m*/
 * Copyright (c) 2003 Objectix Pty Ltd  All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL OBJECTIX PTY LTD BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

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

public class Main {
    /**
     * Shorthand method for {@link #readBytes(InputStream, byte[], int)}.
     * Calls {@link #readBytes(InputStream, byte[], int)} with an unlimited number of bytes per invocation of
     * {@link InputStream#read(byte[], int, int)}.
     * @param in the {@link InputStream} to read from
     * @param buffer the byte array to write into
     * @throws IOException If the end of the {@link InputStream} is reached before {@code buffer.length} bytes are read
     *     or if the first byte cannot be read for any reason other than end of file, or if the input stream has been
     *     closed, or if some other I/O error occurs
     * @throws NullPointerException if {@code buffer} is null
     */
    public static void readBytes(InputStream in, byte[] buffer)
            throws IOException {
        readBytes(in, buffer, Integer.MAX_VALUE);
    }

    /**
     * Read a number of bytes from an InputStream into the given byte array.
     * The number of read bytes is determined by the length of the given byte array.
     * The number of bytes that are read per invocation of {@link InputStream#read(byte[], int, int)} can be limited by
     * the parameter {@code maxBytesAtOnce}.
     * @param in the {@link InputStream} to read from
     * @param buffer the byte array to write into (must not be {@code null})
     * @param maxBytesAtOnce
     *     the maximum number of bytes that is read per invocation of {@link InputStream#read(byte[], int, int)}
     * @throws IOException If the end of the {@link InputStream} is reached before {@code buffer.length} bytes are read
     *     or if the first byte cannot be read for any reason other than end of file, or if the input stream has been
     *     closed, or if some other I/O error occurs
     * @throws NullPointerException if {@code buffer} is null
     */
    public static void readBytes(InputStream in, byte[] buffer,
            int maxBytesAtOnce) throws IOException {
        if (buffer == null) {
            throw new NullPointerException(
                    "The byte buffer for reading from InputStream must not be null!");
        }

        int pos = 0;
        while (pos < buffer.length) {
            int diff = in.read(
                    buffer,
                    pos,
                    Math.min(buffer.length - pos,
                            Math.max(1, maxBytesAtOnce)));
            if (diff < 0) {
                throw new IOException(
                        String.format(
                                "Could only read %d of %d bytes, because the InputStream ended unexpectedly!",
                                pos + diff, buffer.length));
            }
            pos += diff;
        }
    }
}

Related

  1. readBytes(InputStream in)
  2. readBytes(InputStream in, boolean forceClose)
  3. readBytes(InputStream in, byte[] b)
  4. readBytes(InputStream in, byte[] buf, int length)
  5. readBytes(InputStream in, byte[] buffer)
  6. readBytes(InputStream in, byte[] data, int offset, int length)
  7. readBytes(InputStream in, int bufferSize, boolean forceClose)
  8. readBytes(InputStream in, int bytesToRead, byte[] buffer, int bufferOffset)
  9. readBytes(InputStream in, int expectedBytes, long timeoutMs)