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
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
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.
17 package de.todesbaum.util.io;
19 import java.io.EOFException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
25 * Copies input from an {@link InputStream} to an {@link OutputStream}.
27 * @author <a href="mailto:droden@gmail.com">David Roden</a>
30 public class StreamCopier {
33 * The default size of the buffer.
35 private static final int BUFFER_SIZE = 64 * 1024;
38 * The {@link InputStream} to read from.
40 private InputStream inputStream;
43 * The {@link OutputStream} to write to.
45 private OutputStream outputStream;
48 * The number of bytes to copy.
53 * The size of the buffer.
55 private int bufferSize;
58 * Creates a new StreamCopier with the specified parameters and the default
62 * The {@link InputStream} to read from
64 * The {@link OutputStream} to write to
66 * The number of bytes to copy
68 public StreamCopier(InputStream inputStream, OutputStream outputStream, long length) {
69 this(inputStream, outputStream, length, BUFFER_SIZE);
73 * Creates a new StreamCopier with the specified parameters and the default
77 * The {@link InputStream} to read from
79 * The {@link OutputStream} to write to
81 * The number of bytes to copy
83 * The number of bytes to copy at a time
85 public StreamCopier(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) {
86 this.inputStream = inputStream;
87 this.outputStream = outputStream;
89 this.bufferSize = bufferSize;
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
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
103 public void copy() throws EOFException, IOException {
104 copy(inputStream, outputStream, length, bufferSize);
108 * Copies <code>length</code> bytes from the <code>inputStream</code> to
109 * the <code>outputStream</code>.
112 * The input stream to read from
113 * @param outputStream
114 * The output stream to write to
116 * The number of bytes to copy
117 * @throws IOException
118 * if an I/O exception occurs
120 public static void copy(InputStream inputStream, OutputStream outputStream, long length) throws IOException {
121 copy(inputStream, outputStream, length, BUFFER_SIZE);
125 * Copies <code>length</code> bytes from the <code>inputStream</code> to
126 * the <code>outputStream</code> using a buffer with the specified size
129 * The input stream to read from
130 * @param outputStream
131 * The output stream to write to
133 * The number of bytes to copy
135 * The size of the copy buffer
136 * @throws IOException
137 * if an I/O exception occurs
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)));
145 throw new EOFException();
147 outputStream.write(buffer, 0, read);