Java InputStream to Byte Array getBytes(InputStream stream)

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

Description

get Bytes

License

Open Source License

Declaration

public static byte[] getBytes(InputStream stream) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 EclipseSource 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://w w w  . j a  va  2 s. com
 *    EclipseSource - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static byte[] getBytes(InputStream stream) {
        byte[] result = null;
        if (stream != null) {
            try {
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                int read;
                byte[] data = new byte[16384];
                while ((read = stream.read(data, 0, data.length)) != -1) {
                    buffer.write(data, 0, read);
                }
                buffer.flush();
                result = buffer.toByteArray();
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
        return result;
    }
}

Related

  1. getBytes(InputStream is)
  2. getBytes(InputStream is)
  3. getBytes(InputStream is)
  4. getBytes(InputStream is)
  5. getBytes(InputStream is, int max_len)
  6. getBytes(InputStream stream)
  7. getBytesFromInputStream(InputStream inputStream)
  8. getBytesFromInputStream(InputStream inputStream)
  9. getBytesFromInputStream(InputStream inputStream)