Java InputStream Read Bytes readBytes(InputStream is)

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

Description

read Bytes

License

BSD License

Declaration

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

Method Source Code

//package com.java2s;
//The contents of this file are subject to the "Simplified BSD License" (the "License");

import java.io.ByteArrayOutputStream;

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

public class Main {
    public static byte[] readBytes(InputStream is) throws IOException {

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        @SuppressWarnings("unused")
        long size = copyStream(is, bs);
        return bs.toByteArray();

    }//from  w  ww  .j  a  v a2s  . c o  m

    public static long copyStream(InputStream is, OutputStream os) throws IOException {
        byte[] buf = new byte[1024];
        int len;
        long size = 0;
        while ((len = is.read(buf)) > 0) {
            os.write(buf, 0, len);
            size += len;
        }

        return size;

    }
}

Related

  1. readBytes(InputStream ins, byte[] buf, int size)
  2. readBytes(InputStream is)
  3. readBytes(InputStream is)
  4. readBytes(InputStream is)
  5. readBytes(InputStream is)