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>
33 public class StreamCopier {
35 /** Default buffer size is 64k. */
36 private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
38 /** The current buffer size. */
39 private static int bufferSize = DEFAULT_BUFFER_SIZE;
42 * Sets the buffer size for following transfers.
47 public static void setBufferSize(int bufferSize) {
48 StreamCopier.bufferSize = bufferSize;
52 * Copies <code>length</code> bytes from the source input stream to the
53 * destination output stream. If <code>length</code> is <code>-1</code>
54 * as much bytes as possible will be copied (i.e. until
55 * {@link InputStream#read()} returns <code>-1</code> to signal the end of
59 * The input stream to read from
61 * The output stream to write to
63 * The number of bytes to copy
65 * if an I/O error occurs
67 public static void copy(InputStream source, OutputStream destination, long length) throws IOException {
68 long remaining = length;
69 byte[] buffer = new byte[bufferSize];
71 while ((remaining == -1) || (remaining > 0)) {
72 read = source.read(buffer, 0, ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining);
77 throw new EOFException("stream reached eof");
79 destination.write(buffer, 0, read);
85 * Copies as much bytes as possible (i.e. until {@link InputStream#read()}
86 * returns <code>-1</code>) from the source input stream to the
87 * destination output stream.
90 * The input stream to read from
92 * The output stream to write to
94 * if an I/O error occurs
96 public static void copy(InputStream source, OutputStream destination) throws IOException {
97 copy(source, destination, -1);
101 * Finds the length of the input stream by reading until
102 * {@link InputStream#read(byte[])} returns <code>-1</code>.
105 * The input stream to measure
106 * @return The length of the input stream in bytes
107 * @throws IOException
108 * if an I/O error occurs
110 public static long findLength(InputStream source) throws IOException {
112 byte[] buffer = new byte[bufferSize];
115 read = source.read(buffer);