Java InputStream to Byte Array getStreamContent(InputStream stream)

Here you can find the source of getStreamContent(InputStream stream)

Description

get Stream Content

License

Open Source License

Declaration

public static byte[] getStreamContent(InputStream stream) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2014 Xored Software Inc 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  w ww  .j  a va  2 s. c  om
 *     Xored Software Inc - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.io.ByteArrayOutputStream;
import java.io.Closeable;

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

public class Main {
    public static byte[] getStreamContent(InputStream stream) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[4096];
            int len = 0;
            while ((len = stream.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            safeClose(stream);
        }
        return output.toByteArray();
    }

    public static void safeClose(Closeable closeable) {
        try {
            closeable.close();
        } catch (Exception e) {
        }
    }
}

Related

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