Java InputStream Read All readAll(InputStream in)

Here you can find the source of readAll(InputStream in)

Description

Reads all data from the given input stream.

License

Apache License

Parameter

Parameter Description
in the stream to read from.

Exception

Parameter Description
IOException if there was an error reading from the stream.

Return

the data.

Declaration

public static byte[] readAll(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w w  w . ja v a2 s.c  om
 * $Id: Util.java 169 2011-03-03 18:45:00Z ahto.truu $
 *
 *
 *
 * Copyright 2008-2011 GuardTime AS
 *
 * This file is part of the GuardTime client SDK.
 *
 * 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;

public class Main {
    /**
     * The default buffer size for the data read/copy operations in this class.
     */
    public static final int DEFAULT_BUFFER_SIZE = 8192;

    /**
     * Reads all data from the given input stream.
     *
     * @param in
     *            the stream to read from.
     * @return the data.
     * @throws IOException
     *             if there was an error reading from the stream.
     */
    public static byte[] readAll(InputStream in) throws IOException {
        return readAll(in, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Reads all data from the given input stream using a buffer of the given size.
     *
     * @param in
     *            the stream to read from.
     * @param bufSize
     *            size of the buffer to use.
     * @return the data.
     * @throws IOException
     *             if there was an error reading from the stream.
     */
    public static byte[] readAll(InputStream in, int bufSize) throws IOException {
        if (bufSize < 1) {
            throw new IllegalArgumentException("Invalid buffer size: " + bufSize);
        }

        byte[] res = new byte[0];
        byte[] buf = new byte[bufSize];

        int bytesRead;
        while ((bytesRead = in.read(buf)) != -1) {
            byte[] tmp = new byte[res.length + bytesRead];
            System.arraycopy(res, 0, tmp, 0, res.length);
            System.arraycopy(buf, 0, tmp, res.length, bytesRead);
            res = tmp;
        }
        in.close();
        return res;
    }
}

Related

  1. readAll(final InputStream is)
  2. readAll(InputStream i, byte b[])
  3. readAll(InputStream in)
  4. readall(InputStream in)
  5. readAll(InputStream in)
  6. readAll(InputStream in)
  7. readAll(InputStream in)
  8. readAll(InputStream in, byte[] buffer, int off, int len)
  9. readall(InputStream in, byte[] buffer, int offset, int len)