Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * EncFS Java Library
 * Copyright (C) 2011 Mark R. Pariente
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

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

public class Main {
    private static final int EIGHT_KILO = 8192;

    /**
     * Copy the entire content of an InputStream into an OutputStream and close
     * both streams.
     * 
     * @param in
     *            The InputStream to read data from
     * @param out
     *            The OutputStream to write data to
     * 
     * @throws IOException
     *             I/O exception from read or write
     */
    public static void copyWholeStreamAndClose(InputStream in, OutputStream out) throws IOException {
        try {
            copyWholeStreamAndCloseInput(in, out);
        } finally {
            out.close();
        }
    }

    /**
     * Copy the entire content of an InputStream into an OutputStream and close
     * only the input stream.
     * 
     * @param in
     *            The InputStream to read data from
     * @param out
     *            The OutputStream to write data to
     * 
     * @throws IOException
     *             I/O exception from read or write
     */
    public static void copyWholeStreamAndCloseInput(InputStream in, OutputStream out) throws IOException {
        try {
            readFromAndWriteTo(in, out);
        } finally {
            in.close();
        }
    }

    private static void readFromAndWriteTo(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[EIGHT_KILO];
        int bytesRead = in.read(buf);
        while (bytesRead >= 0) {
            out.write(buf, 0, bytesRead);
            bytesRead = in.read(buf);
        }
    }
}