| 1 | /* |
| 2 | @license.text@ |
| 3 | */ |
| 4 | |
| 5 | package com.hammurapi.common; |
| 6 | |
| 7 | import java.io.IOException; |
| 8 | import java.io.InputStream; |
| 9 | import java.io.OutputStream; |
| 10 | import java.io.Reader; |
| 11 | import java.io.Writer; |
| 12 | import java.util.concurrent.Callable; |
| 13 | |
| 14 | /** |
| 15 | * |
| 16 | * Copies all data from an input stream to an output stream. |
| 17 | * @author Pavel Vlasov |
| 18 | * @version $Revision: 1.3 $ |
| 19 | */ |
| 20 | public class Pumper implements Callable<Long> { |
| 21 | |
| 22 | private final static int SIZE = 1024; |
| 23 | private InputStream is; |
| 24 | private OutputStream os; |
| 25 | |
| 26 | private boolean closeStreams=false; |
| 27 | private Reader reader; |
| 28 | private Writer writer; |
| 29 | |
| 30 | /** |
| 31 | * Create a new pumper. |
| 32 | * |
| 33 | * @param is input stream to read data from |
| 34 | * @param os output stream to write data to. |
| 35 | */ |
| 36 | public Pumper(InputStream is, OutputStream os, boolean closeStreams) { |
| 37 | this.is = is; |
| 38 | this.os = os; |
| 39 | this.closeStreams=closeStreams; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create a new pumper. |
| 44 | * |
| 45 | * @param reader Reader to read data from |
| 46 | * @param writer Writer to write data to. |
| 47 | */ |
| 48 | public Pumper(Reader reader, Writer writer, boolean closeStreams) { |
| 49 | this.reader = reader; |
| 50 | this.writer = writer; |
| 51 | this.closeStreams=closeStreams; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Copies data from the input stream to the output stream. |
| 56 | * Terminates as soon as the input stream is closed or an error occurs. |
| 57 | */ |
| 58 | public Long call() throws Exception { |
| 59 | if (is==null) { |
| 60 | return pump(reader, writer, closeStreams); |
| 61 | } |
| 62 | |
| 63 | return pump(is, os, closeStreams); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Copies data from the input stream to the output stream. |
| 68 | */ |
| 69 | public static long pump(InputStream in, OutputStream out, boolean closeStreams) throws IOException { |
| 70 | try { |
| 71 | try { |
| 72 | final byte[] buf = new byte[SIZE]; |
| 73 | int length; |
| 74 | long ret = 0; |
| 75 | while ((length = in.read(buf)) != -1) { |
| 76 | out.write(buf, 0, length); |
| 77 | ret+=length; |
| 78 | } |
| 79 | return ret; |
| 80 | } finally { |
| 81 | if (closeStreams) { |
| 82 | in.close(); |
| 83 | } |
| 84 | } |
| 85 | } finally { |
| 86 | if (closeStreams) { |
| 87 | out.close(); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Copies data from the reader to the writer. |
| 94 | */ |
| 95 | public static long pump(Reader in, Writer out, boolean closeStreams) throws IOException { |
| 96 | try { |
| 97 | try { |
| 98 | final char[] buf = new char[SIZE]; |
| 99 | 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 | } |