d1d0b11e65e4564980b6076ad87ce40d22072106
[jSite2.git] / src / net / pterodactylus / util / io / StreamCopier.java
1 /*
2  * jSite2 - StreamCopier.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.util.io;
21
22 import java.io.EOFException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27 /**
28  * Helper class that copies bytes from an {@link InputStream} to an
29  * {@link OutputStream}.
30  * 
31  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
32  * @version $Id$
33  */
34 public class StreamCopier {
35
36         /** Default buffer size is 64k. */
37         private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
38
39         /** The current buffer size. */
40         private static int bufferSize = DEFAULT_BUFFER_SIZE;
41
42         /**
43          * Sets the buffer size for following transfers.
44          * 
45          * @param bufferSize
46          *            The new buffer size
47          */
48         public static void setBufferSize(int bufferSize) {
49                 StreamCopier.bufferSize = bufferSize;
50         }
51
52         /**
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
57          * the stream).
58          * 
59          * @param source
60          *            The input stream to read from
61          * @param destination
62          *            The output stream to write to
63          * @param length
64          *            The number of bytes to copy
65          * @throws IOException
66          *             if an I/O error occurs
67          */
68         public static void copy(InputStream source, OutputStream destination, long length) throws IOException {
69                 long remaining = length;
70                 byte[] buffer = new byte[bufferSize];
71                 int read = 0;
72                 while ((remaining == -1) || (remaining > 0)) {
73                         read = source.read(buffer, 0, ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining);
74                         if (read == -1) {
75                                 if (length == -1) {
76                                         return;
77                                 }
78                                 throw new EOFException("stream reached eof");
79                         }
80                         destination.write(buffer, 0, read);
81                         remaining -= read;
82                 }
83         }
84
85         /**
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.
89          * 
90          * @param source
91          *            The input stream to read from
92          * @param destination
93          *            The output stream to write to
94          * @throws IOException
95          *             if an I/O error occurs
96          */
97         public static void copy(InputStream source, OutputStream destination) throws IOException {
98                 copy(source, destination, -1);
99         }
100
101         /**
102          * Finds the length of the input stream by reading until
103          * {@link InputStream#read(byte[])} returns <code>-1</code>.
104          * 
105          * @param source
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
110          */
111         public static long findLength(InputStream source) throws IOException {
112                 long length = 0;
113                 byte[] buffer = new byte[bufferSize];
114                 int read = 0;
115                 while (read != -1) {
116                         read = source.read(buffer);
117                         if (read != -1) {
118                                 length += read;
119                         }
120                 }
121                 return length;
122         }
123
124 }