Java InputStream to Byte Array getInputStreamAsByteArray(InputStream stream, int length)

Here you can find the source of getInputStreamAsByteArray(InputStream stream, int length)

Description

Returns the given input stream as a byte array

License

Open Source License

Parameter

Parameter Description
stream the stream to get as a byte array
length the length to read from the stream or -1 for unknown

Exception

Parameter Description
IOException an exception

Return

the given input stream as a byte array

Declaration

public static byte[] getInputStreamAsByteArray(InputStream stream,
        int length) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from ww w.j a v a2s .com*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * Constant representing the default size to read from an input stream
     */
    private static final int DEFAULT_READING_SIZE = 8192;

    /**
     * Returns the given input stream as a byte array
     * 
     * @param stream the stream to get as a byte array
     * @param length the length to read from the stream or -1 for unknown
     * @return the given input stream as a byte array
     * @throws IOException
     */
    public static byte[] getInputStreamAsByteArray(InputStream stream,
            int length) throws IOException {
        byte[] contents;
        if (length == -1) {
            contents = new byte[0];
            int contentsLength = 0;
            int amountRead = -1;
            do {
                // read at least 8K
                int amountRequested = Math.max(stream.available(),
                        DEFAULT_READING_SIZE);
                // resize contents if needed
                if (contentsLength + amountRequested > contents.length) {
                    System.arraycopy(contents, 0,
                            contents = new byte[contentsLength
                                    + amountRequested], 0, contentsLength);
                }
                // read as many bytes as possible
                amountRead = stream.read(contents, contentsLength,
                        amountRequested);
                if (amountRead > 0) {
                    // remember length of contents
                    contentsLength += amountRead;
                }
            } while (amountRead != -1);
            // resize contents if necessary
            if (contentsLength < contents.length) {
                System.arraycopy(contents, 0,
                        contents = new byte[contentsLength], 0,
                        contentsLength);
            }
        } else {
            contents = new byte[length];
            int len = 0;
            int readSize = 0;
            while ((readSize != -1) && (len != length)) {
                // See PR 1FMS89U
                // We record first the read size. In this case length is the actual
                // read size.
                len += readSize;
                readSize = stream.read(contents, len, length - len);
            }
        }
        return contents;
    }
}

Related

  1. getBytesFromStream(InputStream is)
  2. getBytesFromStream(InputStream is)
  3. getBytesFromStream(InputStream is)
  4. getBytesFromStream(InputStream is)
  5. getInputStreamAsByteArray(InputStream stream, int length)
  6. getStreamAsByteArray(InputStream input)
  7. getStreamAsByteArray(InputStream stream)
  8. getStreamAsBytes(final InputStream is)
  9. getStreamBytes(InputStream is)