2 * jSite2 - StreamCopier.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.util.io;
22 import java.io.EOFException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
28 * Helper class that copies bytes from an {@link InputStream} to an
29 * {@link OutputStream}.
31 * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
34 public class StreamCopier {
36 /** Default buffer size is 64k. */
37 private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
39 /** The current buffer size. */
40 private static int bufferSize = DEFAULT_BUFFER_SIZE;
43 * Sets the buffer size for following transfers.
48 public static void setBufferSize(int bufferSize) {
49 StreamCopier.bufferSize = bufferSize;
53 * Copies <code>length</code> bytes from the source input stream to the
54 * destination output stream. If <code>length</code> is <code>-1</code>
55 * as much bytes as possible will be copied (i.e. until
56 * {@link InputStream#read()} returns <code>-1</code> to signal the end of
60 * The input stream to read from
62 * The output stream to write to
64 * The number of bytes to copy
66 * if an I/O error occurs
68 public static void copy(InputStream source, OutputStream destination, long length) throws IOException {
69 long remaining = length;
70 byte[] buffer = new byte[bufferSize];
72 while ((remaining == -1) || (remaining > 0)) {
73 read = source.read(buffer, 0, ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining);
78 throw new EOFException("stream reached eof");
80 destination.write(buffer, 0, read);
86 * Copies as much bytes as possible (i.e. until {@link InputStream#read()}
87 * returns <code>-1</code>) from the source input stream to the
88 * destination output stream.
91 * The input stream to read from
93 * The output stream to write to
95 * if an I/O error occurs
97 public static void copy(InputStream source, OutputStream destination) throws IOException {
98 copy(source, destination, -1);
102 * Finds the length of the input stream by reading until
103 * {@link InputStream#read(byte[])} returns <code>-1</code>.
106 * The input stream to measure
107 * @return The length of the input stream in bytes
108 * @throws IOException
109 * if an I/O error occurs
111 public static long findLength(InputStream source) throws IOException {
113 byte[] buffer = new byte[bufferSize];
116 read = source.read(buffer);