1 /*
2 * File: Copier.java
3 * Created: 13.04.2006 8:27:50
4 *
5 * Copyright 2006 Michal Burda.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 package net.sf.webmancer.util.io;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.io.Writer;
28
29 import net.sf.webmancer.util.ContractChecker;
30
31 /**
32 * @author Michal Burda
33 */
34 public class Copier {
35 /**
36 *
37 */
38 private int bufferSize;
39
40 /**
41 * @param bufferSize
42 */
43 public Copier(final int bufferSize) {
44 this.bufferSize = bufferSize;
45 }
46
47 /**
48 * @param input
49 * @param output
50 * @throws IOException
51 */
52 public void copy(final InputStream input, final OutputStream output) throws IOException {
53 ContractChecker.mustNotBeNull(input, "input");
54 ContractChecker.mustNotBeNull(output, "output");
55 byte[] buffer = new byte[this.bufferSize];
56 int read = -1;
57 try {
58 while ((read = input.read(buffer)) >= 0) {
59 output.write(buffer, 0, read);
60 }
61 } finally {
62 try {
63 input.close();
64 } finally {
65 output.close();
66 }
67 }
68 }
69
70 /**
71 * @param input
72 * @param output
73 * @throws IOException
74 */
75 public void copy(final Reader input, final Writer output) throws IOException {
76 ContractChecker.mustNotBeNull(input, "input");
77 ContractChecker.mustNotBeNull(output, "output");
78 char[] buffer = new char[this.bufferSize];
79 int read = -1;
80 try {
81 while ((read = input.read(buffer)) >= 0) {
82 output.write(buffer, 0, read);
83 }
84 } finally {
85 try {
86 input.close();
87 } finally {
88 output.close();
89 }
90 }
91 }
92
93 }