Java InputStream to Byte Array getStreamAsByteArray(InputStream input)

Here you can find the source of getStreamAsByteArray(InputStream input)

Description

get Stream As Byte Array

License

Open Source License

Declaration

private static byte[] getStreamAsByteArray(InputStream input) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.  j av  a 2 s .  com*/
 * Copyright (C) 2014 ???????Invisible God? <373575012@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ?????????????
 */

import java.io.BufferedInputStream;

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

public class Main {
    private static byte[] getStreamAsByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream bos = null;
        BufferedInputStream bis = null;
        byte[] buffer = null;
        try {
            bos = new ByteArrayOutputStream();
            bis = new BufferedInputStream(input);
            buffer = new byte[1024];
            int length = -1;
            while ((length = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
                bos.flush();
            }
            return bos.toByteArray();
        } finally {
            if (bos != null) {
                bos.close();
                bos = null;
            }
            if (bis != null) {
                bis.close();
                bis = null;
            }
            buffer = null;
        }
    }
}

Related

  1. getBytesFromStream(InputStream is)
  2. getBytesFromStream(InputStream is)
  3. getBytesFromStream(InputStream is)
  4. getInputStreamAsByteArray(InputStream stream, int length)
  5. getInputStreamAsByteArray(InputStream stream, int length)
  6. getStreamAsByteArray(InputStream stream)
  7. getStreamAsBytes(final InputStream is)
  8. getStreamBytes(InputStream is)
  9. getStreamContent(InputStream stream)