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[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[4096];
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096);

        // Do the first byte via a blocking read
        outputStream.write(inputStream.read());

        // Slurp the rest
        int available = 0;// inputStream.available();
        boolean run = true;
        while (run && (available = inputStream.available()) > 0) {
            // Log.d(TAG, "slurp " + available);
            while (available > 0) {
                int cbToRead = Math.min(buffer.length, available);
                int cbRead = inputStream.read(buffer, 0, cbToRead);
                if (cbRead <= 0) {
                    run = false;
                    break;
                }
                outputStream.write(buffer, 0, cbRead);
                available -= cbRead;
            }
        }
        return outputStream.toByteArray();
    }
}