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

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

Description

get Input Stream As Byte Array

License

Mozilla Public License

Declaration

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

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/*from   ww w.  j  a  v  a  2 s  .  c om*/
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Spindle, an Eclipse Plugin for Tapestry.
 *
 * The Initial Developer of the Original Code is
 * Intelligent Works Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * 
 *  glongman@intelligentworks.com 
 *
 * ***** END LICENSE BLOCK ***** */

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

public class Main {
    public static byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException {
        byte[] contents;
        if (length == -1) {
            contents = new byte[0];
            int contentsLength = 0;
            int bytesRead = -1;
            do {
                int available = stream.available();

                // resize contents if needed
                if (contentsLength + available > contents.length) {
                    System.arraycopy(contents, 0, contents = new byte[contentsLength + available], 0,
                            contentsLength);
                }

                // read as many bytes as possible
                bytesRead = stream.read(contents, contentsLength, available);

                if (bytesRead > 0) {
                    // remember length of contents
                    contentsLength += bytesRead;
                }
            } while (bytesRead > 0);

            // 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 len 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. getBytesFromStream(InputStream is)
  6. getInputStreamAsByteArray(InputStream stream, int length)
  7. getStreamAsByteArray(InputStream input)
  8. getStreamAsByteArray(InputStream stream)
  9. getStreamAsBytes(final InputStream is)