e79a900832bea372cd3c6e4f654203bbc4433892
[jSite.git] / src / de / todesbaum / util / io / StreamCopier.java
1 /*
2  * This program is free software; you can redistribute it and/or modify it under
3  * the terms of the GNU General Public License as published by the Free Software
4  * Foundation; either version 2 of the License, or (at your option) any later
5  * version.
6  * 
7  * This program is distributed in the hope that it will be useful, but WITHOUT
8  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10  * details.
11  * 
12  * You should have received a copy of the GNU General Public License along with
13  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
14  * Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 package de.todesbaum.util.io;
18
19 import java.io.EOFException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23
24 /**
25  * Copies input from an {@link InputStream} to an {@link OutputStream}.
26  * 
27  * @author <a href="mailto:droden@gmail.com">David Roden</a>
28  * @version $Id$
29  */
30 public class StreamCopier {
31
32         /**
33          * The default size of the buffer.
34          */
35         private static final int BUFFER_SIZE = 64 * 1024;
36
37         /**
38          * The {@link InputStream} to read from.
39          */
40         private InputStream inputStream;
41
42         /**
43          * The {@link OutputStream} to write to.
44          */
45         private OutputStream outputStream;
46
47         /**
48          * The number of bytes to copy.
49          */
50         private long length;
51
52         /**
53          * The size of the buffer.
54          */
55         private int bufferSize;
56
57         /**
58          * Creates a new StreamCopier with the specified parameters and the default
59          * buffer size.
60          * 
61          * @param inputStream
62          *            The {@link InputStream} to read from
63          * @param outputStream
64          *            The {@link OutputStream} to write to
65          * @param length
66          *            The number of bytes to copy
67          */
68         public StreamCopier(InputStream inputStream, OutputStream outputStream, long length) {
69                 this(inputStream, outputStream, length, BUFFER_SIZE);
70         }
71
72         /**
73          * Creates a new StreamCopier with the specified parameters and the default
74          * buffer size.
75          * 
76          * @param inputStream
77          *            The {@link InputStream} to read from
78          * @param outputStream
79          *            The {@link OutputStream} to write to
80          * @param length
81          *            The number of bytes to copy
82          * @param bufferSize
83          *            The number of bytes to copy at a time
84          */
85         public StreamCopier(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) {
86                 this.inputStream = inputStream;
87                 this.outputStream = outputStream;
88                 this.length = length;
89                 this.bufferSize = bufferSize;
90         }
91
92         /**
93          * Copies the stream data. If the input stream is depleted before the
94          * requested number of bytes have been read an {@link EOFException} is
95          * thrown.
96          * 
97          * @throws EOFException
98          *             if the input stream is depleted before the requested number
99          *             of bytes has been read
100          * @throws IOException
101          *             if an I/O error occurs
102          */
103         public void copy() throws EOFException, IOException {
104                 copy(inputStream, outputStream, length, bufferSize);
105         }
106
107         /**
108          * Copies <code>length</code> bytes from the <code>inputStream</code> to
109          * the <code>outputStream</code>.
110          * 
111          * @param inputStream
112          *            The input stream to read from
113          * @param outputStream
114          *            The output stream to write to
115          * @param length
116          *            The number of bytes to copy
117          * @throws IOException
118          *             if an I/O exception occurs
119          */
120         public static void copy(InputStream inputStream, OutputStream outputStream, long length) throws IOException {
121                 copy(inputStream, outputStream, length, BUFFER_SIZE);
122         }
123
124         /**
125          * Copies <code>length</code> bytes from the <code>inputStream</code> to
126          * the <code>outputStream</code> using a buffer with the specified size
127          * 
128          * @param inputStream
129          *            The input stream to read from
130          * @param outputStream
131          *            The output stream to write to
132          * @param length
133          *            The number of bytes to copy
134          * @param bufferSize
135          *            The size of the copy buffer
136          * @throws IOException
137          *             if an I/O exception occurs
138          */
139         public static void copy(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) throws IOException {
140                 long remaining = length;
141                 byte[] buffer = new byte[bufferSize];
142                 while (remaining > 0) {
143                         int read = inputStream.read(buffer, 0, (int) Math.min(Integer.MAX_VALUE, Math.min(bufferSize, remaining)));
144                         if (read == -1) {
145                                 throw new EOFException();
146                         }
147                         outputStream.write(buffer, 0, read);
148                         remaining -= read;
149                 }
150         }
151
152 }