f4137448c8818072a16c0c7709c7cc36e80979ee
[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  */
33 public class StreamCopier {
34
35         /** Default buffer size is 64k. */
36         private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
37
38         /** The current buffer size. */
39         private static int bufferSize = DEFAULT_BUFFER_SIZE;
40
41         /**
42          * Sets the buffer size for following transfers.
43          * 
44          * @param bufferSize
45          *            The new buffer size
46          */
47         public static void setBufferSize(int bufferSize) {
48                 StreamCopier.bufferSize = bufferSize;
49         }
50
51         /**
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
56          * the stream).
57          * 
58          * @param source
59          *            The input stream to read from
60          * @param destination
61          *            The output stream to write to
62          * @param length
63          *            The number of bytes to copy
64          * @throws IOException
65          *             if an I/O error occurs
66          */
67         public static void copy(InputStream source, OutputStream destination, long length) throws IOException {
68                 long remaining = length;
69                 byte[] buffer = new byte[bufferSize];
70                 int read = 0;
71                 while ((remaining == -1) || (remaining > 0)) {
72                         read = source.read(buffer, 0, ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining);
73                         if (read == -1) {
74                                 if (length == -1) {
75                                         return;
76                                 }
77                                 throw new EOFException("stream reached eof");
78                         }
79                         destination.write(buffer, 0, read);
80                         remaining -= read;
81                 }
82         }
83
84         /**
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.
88          * 
89          * @param source
90          *            The input stream to read from
91          * @param destination
92          *            The output stream to write to
93          * @throws IOException
94          *             if an I/O error occurs
95          */
96         public static void copy(InputStream source, OutputStream destination) throws IOException {
97                 copy(source, destination, -1);
98         }
99
100         /**
101          * Finds the length of the input stream by reading until
102          * {@link InputStream#read(byte[])} returns <code>-1</code>.
103          * 
104          * @param source
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
109          */
110         public static long findLength(InputStream source) throws IOException {
111                 long length = 0;
112                 byte[] buffer = new byte[bufferSize];
113                 int read = 0;
114                 while (read != -1) {
115                         read = source.read(buffer);
116                         if (read != -1) {
117                                 length += read;
118                         }
119                 }
120                 return length;
121         }
122
123 }