Java InputStream to Byte Array getBytes(InputStream fis)

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

Description

get Bytes

License

Apache License

Declaration

public static byte[] getBytes(InputStream fis) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

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

public class Main {

    public static byte[] getBytes(InputStream fis) {
        byte[] buffer = null;
        try {//  w  ww .  j a  va  2s. c  o m

            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}

Related

  1. fread(short[] data, int length, InputStream fp)
  2. getBytes(InputStream attachment)
  3. getBytes(InputStream in)
  4. getBytes(InputStream in)
  5. getBytes(InputStream in)
  6. getBytes(InputStream in)