Java InputStream to Byte Array getBytesFromStream(InputStream is)

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

Description

get Bytes From Stream

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 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 w ww  .  jav  a  2 s . c o  m
 *     Matthew Hatem, IBM Corporation - initial API and implementation
 *     Boris Bokowski, IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayOutputStream;

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

public class Main {
    public static byte[] getBytesFromStream(InputStream is)
            throws IOException {
        int BUFFER_SIZE = 8192;
        byte[] buffer = new byte[BUFFER_SIZE];
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int numRead = 0;
        while ((numRead = is.read(buffer)) > 0) {
            os.write(buffer, 0, numRead);
        }
        return os.toByteArray();
    }
}

Related

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