    001/*
    002@license.text@
    003 */
    004
    005package com.hammurapi.common;
    006
    007import java.io.IOException;
    008import java.io.InputStream;
    009import java.io.OutputStream;
    010import java.io.Reader;
    011import java.io.Writer;
    012import java.util.concurrent.Callable;
    013
    014/**
    015 *
    016 * Copies all data from an input stream to an output stream.
    017 * @author Pavel Vlasov
    018 * @version $Revision: 1.3 $
    019 */
    020public class Pumper implements Callable<Long> {
    021
    022    private final static int SIZE = 1024;
    023    private InputStream is;
    024    private OutputStream os;
    025
    026    private boolean closeStreams=false;
    027        private Reader reader;
    028        private Writer writer;
    029
    030    /**
    031     * Create a new pumper.
    032     *
    033     * @param is input stream to read data from
    034     * @param os output stream to write data to.
    035     */
    036    public Pumper(InputStream is, OutputStream os, boolean closeStreams) {
    037        this.is = is;
    038        this.os = os;
    039        this.closeStreams=closeStreams;
    040    }
    041
    042    /**
    043     * Create a new pumper.
    044     *
    045     * @param reader Reader to read data from
    046     * @param writer Writer to write data to.
    047     */
    048    public Pumper(Reader reader, Writer writer, boolean closeStreams) {
    049        this.reader = reader;
    050        this.writer = writer;
    051        this.closeStreams=closeStreams;
    052    }
    053
    054    /**
    055     * Copies data from the input stream to the output stream.
    056     * Terminates as soon as the input stream is closed or an error occurs.
    057     */
    058        public Long call() throws Exception {
    059                if (is==null) {
    060                        return pump(reader, writer, closeStreams);
    061                }
    062
    063                return pump(is, os, closeStreams);
    064    }
    065
    066    /**
    067     * Copies data from the input stream to the output stream.
    068     */
    069    public static long pump(InputStream in, OutputStream out, boolean closeStreams) throws IOException {
    070        try {
    071                try {
    072                    final byte[] buf = new byte[SIZE];
    073                    int length;
    074                        long ret = 0;
    075                    while ((length = in.read(buf)) != -1) {
    076                        out.write(buf, 0, length);
    077                        ret+=length;
    078                    }
    079                    return ret;
    080                } finally {
    081                    if (closeStreams) {
    082                       in.close();
    083                    }
    084                }
    085        } finally {
    086            if (closeStreams) {
    087               out.close();
    088            }
    089        }
    090    }
    091
    092    /**
    093     * Copies data from the reader to the writer.
    094     */
    095    public static long pump(Reader in, Writer out, boolean closeStreams) throws IOException {
    096        try {
    097                try {
    098                    final char[] buf = new char[SIZE];
    099                    int length;
    100                        long ret = 0;
    101                    while ((length = in.read(buf)) != -1) {
    102                        out.write(buf, 0, length);
    103                        ret+=length;
    104                    }
    105                    return ret;
    106                } finally {
    107                    if (closeStreams) {
    108                       in.close();
    109                    }
    110                }
    111        } finally {
    112            if (closeStreams) {
    113               out.close();
    114            }
    115        }
    116    }
    117
    118}