Java ByteArrayOutputStream Write readAll(InputStream bytes, int bufferSize)

Here you can find the source of readAll(InputStream bytes, int bufferSize)

Description

Read all of the bytes in an input stream.

License

Apache License

Parameter

Parameter Description
bytes the InputStream of bytes to read
bufferSize the number of bytes to buffer at each read

Exception

Parameter Description
IOException if the stream fails

Return

an array of all bytes retrieved from the stream

Declaration

private static byte[] readAll(InputStream bytes, int bufferSize) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w  w  .  j  a v a2 s  .co  m
 * Copyright (c) 2017 Intel Corporation 
 *
 * 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 4096;

    /**
     * Read all of the bytes in an input stream.
     *
     * @param bytes the {@link InputStream} of bytes to read
     * @return an array of all bytes retrieved from the stream
     * @throws IOException if the stream fails
     */
    public static byte[] readAll(InputStream bytes) throws IOException {
        return readAll(bytes, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Read all of the bytes in an input stream. Note that this will close the passed stream after all bytes have been
     * read.
     *
     * @param bytes the {@link InputStream} of bytes to read
     * @param bufferSize the number of bytes to buffer at each read
     * @return an array of all bytes retrieved from the stream
     * @throws IOException if the stream fails
     */
    private static byte[] readAll(InputStream bytes, int bufferSize) throws IOException {
        try {
            return readAllAndKeepOpen(bytes, bufferSize);
        } finally {
            bytes.close();
        }
    }

    /**
     * Read all of the bytes in an input stream. Note that this will close the passed stream after all bytes have been
     * read.
     *
     * @param bytes the {@link InputStream} of bytes to read
     * @param bufferSize the number of bytes to buffer at each read
     * @return an array of all bytes retrieved from the stream
     * @throws IOException if the stream fails
     */
    public static byte[] readAllAndKeepOpen(InputStream bytes, int bufferSize) throws IOException {
        try (ByteArrayOutputStream builder = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[bufferSize];
            int read;
            while ((read = bytes.read(buffer)) != -1) {
                builder.write(buffer, 0, read);
            }
            return builder.toByteArray();
        }
    }
}

Related

  1. loadProbe(InputStream in, int buffSize)
  2. loadStream(InputStream in, String encoding)
  3. readAll(File file)
  4. readAll(InputStream aInputStream)
  5. readAll(InputStream bytes)
  6. readAll(InputStream in)
  7. readAll(InputStream in)
  8. readAll(InputStream in)
  9. readAll(InputStream in)