Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

import java.io.ObjectInput;

import java.io.OutputStream;

import java.io.Reader;
import java.io.Writer;

public class Main {
    public static void copy(InputStream in, OutputStream out) {
        final byte[] buf = new byte[1024];
        int len;
        try {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        closeQuietly(in);
        closeQuietly(out);
    }

    /**
     * Equivalent to {@link InputStream#read()} but without checked exceptions.
     */
    public static int read(InputStream inputStream) {
        try {
            return inputStream.read();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Close the specified input stream, ignoring any exceptions.
     */
    public static void closeQuietly(InputStream inputStream) {
        try {
            inputStream.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

    /**
     * Close the specified writer, ignoring any exceptions.
     */
    public static void closeQuietly(Writer writer) {
        try {
            writer.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

    /**
     * Close the specified reader, ignoring any exceptions.
     */
    public static void closeQuietly(Reader reader) {
        try {
            reader.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

    /**
     * Close the specified object input, ignoring any exceptions.
     */
    public static void closeQuietly(ObjectInput objectInput) {
        try {
            objectInput.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

    /**
     * Close the specified output stream, ignoring any exceptions.
     */
    public static void closeQuietly(OutputStream outputStream) {
        try {
            outputStream.close();
        } catch (final Exception e) {
            // Ignored
        }
    }
}