Java ByteArrayOutputStream Write readAll(InputStream bytes)

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

Description

Read all of the bytes in an input stream.

License

Open Source License

Parameter

Parameter Description
bytes the InputStream of bytes to read

Exception

Parameter Description
IOException if the stream fails

Return

an array of all bytes retrieved from the stream

Declaration

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

Method Source Code


//package com.java2s;
/*/*w  w w.  ja  v  a 2  s . c om*/
 * jndn-utils
 * Copyright (c) 2016, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 3, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 */

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

public class Main {
    /**
     * 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 {
        ByteArrayOutputStream builder = new ByteArrayOutputStream();
        int read = bytes.read();
        while (read != -1) {
            builder.write(read);
            read = bytes.read();
        }
        builder.flush();
        bytes.close();
        return builder.toByteArray();
    }
}

Related

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