Java InputStream to Byte Array getBytesFromStream(InputStream input)

Here you can find the source of getBytesFromStream(InputStream input)

Description

Read a stream (usually small) completely in to a byte array.

License

Open Source License

Declaration

public static byte[] getBytesFromStream(InputStream input) throws IOException 

Method Source Code


//package com.java2s;
/*/*w ww.ja  v  a2 s . c  om*/
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2008-2012 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 * This library is distributed in the hope that 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. You should have received
 * a copy of the GNU Lesser General Public License along with this library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.ByteArrayOutputStream;

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

public class Main {
    /**
     * Read a stream (usually small) completely in to a byte array. Used to get all of the
     * bytes out of one or more content objects for decoding or other processing, where the
     * content needs to be handed to something else as a unit.
     */
    public static byte[] getBytesFromStream(InputStream input) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int byteCount = 0;
        byteCount = input.read(buf);
        while (byteCount > 0) {
            baos.write(buf, 0, byteCount);
            byteCount = input.read(buf);
        }
        return baos.toByteArray();
    }
}

Related

  1. getBytesFromInputStream(InputStream inputStream)
  2. getBytesFromInputStream(InputStream inputStream)
  3. getBytesFromInputStream(InputStream inputStream)
  4. getBytesFromInputStream(InputStream is)
  5. getBytesFromInputStream(InputStream is)
  6. getBytesFromStream(InputStream inputStream)
  7. getBytesFromStream(InputStream is)
  8. getBytesFromStream(InputStream is)
  9. getBytesFromStream(InputStream is)