Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayOutputStream;

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

public class Main {
    public static byte[] inputStream2ByteArray(InputStream is) throws IOException {
        return inputStream2ByteArray(is, 1024);
    }

    public static byte[] inputStream2ByteArray(InputStream is, int bufferSize) throws IOException {
        if (null == is) {
            return null;
        }
        if (bufferSize < 1) {
            bufferSize = 1;
        }
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        // this is storage overwritten on each iteration with bytes
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the
        // byteBuffer
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }

        byteBuffer.close();
        is.close();

        // and then we can return your byte array.
        return byteBuffer.toByteArray();
    }
}