Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedWriter;

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

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Main {
    public static void toStream(final String str, final OutputStream stream) throws IOException {
        toStream(str, stream, Charset.defaultCharset());
    }

    public static void toStream(final String str, final OutputStream stream, final Charset charset)
            throws IOException {
        final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset));
        try {
            writer.write(str);
        } finally {
            writer.flush();
            writer.close();
        }
    }

    public static void toStream(final InputStream is, final OutputStream os) throws IOException {
        final int len = 4096;
        final byte[] buffer = new byte[len];
        int read;
        try {
            while ((read = is.read(buffer, 0, len)) != -1) {
                os.write(buffer, 0, read);
            }
        } finally {
            // close the inputstream (which is read to end) but not the output
            // stream, which can still be used
            is.close();
        }
    }
}