Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import java.io.StringWriter;

public class Main {
    /**
     * Convert InputStream to String
     *
     * @param stream inputStream
     * @return string
     */
    private static String streamToString(InputStream stream) {
        StringWriter writer = null;
        if (stream != null) {
            try {
                InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
                writer = new StringWriter();
                int n;
                char[] buffer = new char[1024 * 4];
                while (-1 != (n = reader.read(buffer))) {
                    writer.write(buffer, 0, n);
                }
            } catch (IOException e) {
                // @todo better logging
                e.printStackTrace();
            }
        }
        return writer != null ? writer.toString() : "";
    }
}