Java InputStream to Byte Array inputStreamToBytes(final InputStream ins)

Here you can find the source of inputStreamToBytes(final InputStream ins)

Description

Takes a 'InputStream' and returns a byte array.

License

Open Source License

Parameter

Parameter Description
ins a parameter

Declaration

public static byte[] inputStreamToBytes(final InputStream ins) 

Method Source Code


//package com.java2s;
/*/*www. j a  va2 s . c o m*/
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 * Portions Copyrighted 2013 ConnId
 * Portions Copyrighted 2015 ForgeRock AS.
 */

import java.io.ByteArrayOutputStream;

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

public class Main {
    /**
     * Takes a 'InputStream' and returns a byte array.
     *
     * @param ins
     * @return
     */
    public static byte[] inputStreamToBytes(final InputStream ins) {
        byte[] ret = null;
        try {
            // copy bytes from the stream to an array..
            int len = 0;
            final byte[] buf = new byte[2048];
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((len = ins.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            // get byte array..
            ret = out.toByteArray();
        } catch (IOException e) {
            // ignore and return null..
        }
        return ret;
    }
}

Related

  1. inputStreamToByteArray(InputStream is)
  2. inputStreamToByteArray(InputStream is)
  3. inputStreamToByteArray(InputStream stream)
  4. inputStreamToByteArrayOutputStream(final InputStream is)
  5. inputStreamToBytes(final InputStream aInputStream)
  6. inputStreamToBytes(InputStream in)
  7. inputStreamToBytes(InputStream in)
  8. inputStreamToBytes(InputStream in)
  9. inputStreamToBytes(InputStream is)