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 String stream2StringQuiet(InputStream inStream) {
        return stream2StringQuiet("UTF-8", inStream);
    }

    public static String stream2StringQuiet(String charset, InputStream inStream) {
        String result = null;
        try {
            result = stream2String(charset, inStream);
        } catch (IOException e) {
            // nothing to do
        } finally {
            try {
                inStream.close();
            } catch (IOException e) {
                // nothing to do
            } finally {
                inStream = null;
            }
        }
        return result;
    }

    public static String stream2String(InputStream inStream) throws IOException {
        return stream2String("UTF-8", inStream);
    }

    public static String stream2String(String charset, InputStream inStream) throws IOException {
        return new String(stream2ByteArray(inStream), charset);
    }

    public static byte[] stream2ByteArray(InputStream inStream) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024 * 10];
        while ((len = inStream.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        byte[] result = baos.toByteArray();
        baos.close();
        inStream.close();
        return result;
    }
}