1 package org.springframework.roo.support.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.Reader;
30 import java.io.StringWriter;
31 import java.io.Writer;
32
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class FileCopyUtils {
45
46 public static final int BUFFER_SIZE = 4096;
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public static int copy(File in, File out) throws IOException {
61 Assert.notNull(in, "No input File specified");
62 Assert.notNull(out, "No output File specified");
63 return copy(new BufferedInputStream(new FileInputStream(in)),
64 new BufferedOutputStream(new FileOutputStream(out)));
65 }
66
67
68
69
70
71
72
73 public static void copy(byte[] in, File out) throws IOException {
74 Assert.notNull(in, "No input byte array specified");
75 Assert.notNull(out, "No output File specified");
76 ByteArrayInputStream inStream = new ByteArrayInputStream(in);
77 OutputStream outStream = new BufferedOutputStream(new FileOutputStream(out));
78 copy(inStream, outStream);
79 }
80
81
82
83
84
85
86
87 public static byte[] copyToByteArray(File in) throws IOException {
88 Assert.notNull(in, "No input File specified");
89 return copyToByteArray(new BufferedInputStream(new FileInputStream(in)));
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public static int copy(InputStream in, OutputStream out) throws IOException {
106 Assert.notNull(in, "No InputStream specified");
107 Assert.notNull(out, "No OutputStream specified");
108 try {
109 int byteCount = 0;
110 byte[] buffer = new byte[BUFFER_SIZE];
111 int bytesRead = -1;
112 while ((bytesRead = in.read(buffer)) != -1) {
113 out.write(buffer, 0, bytesRead);
114 byteCount += bytesRead;
115 }
116 out.flush();
117 return byteCount;
118 }
119 finally {
120 try {
121 in.close();
122 }
123 catch (IOException ex) {
124 }
125 try {
126 out.close();
127 }
128 catch (IOException ex) {
129 }
130 }
131 }
132
133
134
135
136
137
138
139
140 public static void copy(byte[] in, OutputStream out) throws IOException {
141 Assert.notNull(in, "No input byte array specified");
142 Assert.notNull(out, "No OutputStream specified");
143 try {
144 out.write(in);
145 }
146 finally {
147 try {
148 out.close();
149 }
150 catch (IOException ex) {
151 }
152 }
153 }
154
155
156
157
158
159
160
161
162 public static byte[] copyToByteArray(InputStream in) throws IOException {
163 ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
164 copy(in, out);
165 return out.toByteArray();
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 public static int copy(Reader in, Writer out) throws IOException {
182 Assert.notNull(in, "No Reader specified");
183 Assert.notNull(out, "No Writer specified");
184 try {
185 int byteCount = 0;
186 char[] buffer = new char[BUFFER_SIZE];
187 int bytesRead = -1;
188 while ((bytesRead = in.read(buffer)) != -1) {
189 out.write(buffer, 0, bytesRead);
190 byteCount += bytesRead;
191 }
192 out.flush();
193 return byteCount;
194 }
195 finally {
196 try {
197 in.close();
198 }
199 catch (IOException ex) {
200 }
201 try {
202 out.close();
203 }
204 catch (IOException ex) {
205 }
206 }
207 }
208
209
210
211
212
213
214
215
216 public static void copy(String in, Writer out) throws IOException {
217 Assert.notNull(in, "No input String specified");
218 Assert.notNull(out, "No Writer specified");
219 try {
220 out.write(in);
221 }
222 finally {
223 try {
224 out.close();
225 }
226 catch (IOException ex) {
227 }
228 }
229 }
230
231
232
233
234
235
236
237
238 public static String copyToString(Reader in) throws IOException {
239 StringWriter out = new StringWriter();
240 copy(in, out);
241 return out.toString();
242 }
243
244 }
245