KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > FileCopyUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.io.Writer JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Simple utility methods for file and stream copying.
38  * All copy methods use a block size of 4096 bytes,
39  * and close all affected streams when done.
40  *
41  * <p>Mainly for use within the framework,
42  * but also useful for application code.
43  *
44  * @author Juergen Hoeller
45  * @since 06.10.2003
46  */

47 public abstract class FileCopyUtils {
48
49     private static final Log logger = LogFactory.getLog(FileCopyUtils.class);
50
51     public static final int BUFFER_SIZE = 4096;
52
53
54     //---------------------------------------------------------------------
55
// Copy methods for java.io.File
56
//---------------------------------------------------------------------
57

58     /**
59      * Copy the contents of the given input File to the given output File.
60      * @param in the file to copy from
61      * @param out the file to copy to
62      * @return the number of bytes copied
63      * @throws IOException in case of I/O errors
64      */

65     public static int copy(File JavaDoc in, File JavaDoc out) throws IOException JavaDoc {
66         Assert.notNull(in, "No input File specified");
67         Assert.notNull(out, "No output File specified");
68         return copy(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(in)),
69             new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(out)));
70     }
71
72     /**
73      * Copy the contents of the given byte array to the given output File.
74      * @param in the byte array to copy from
75      * @param out the file to copy to
76      * @throws IOException in case of I/O errors
77      */

78     public static void copy(byte[] in, File JavaDoc out) throws IOException JavaDoc {
79         Assert.notNull(in, "No input byte array specified");
80         Assert.notNull(out, "No output File specified");
81         ByteArrayInputStream JavaDoc inStream = new ByteArrayInputStream JavaDoc(in);
82         OutputStream JavaDoc outStream = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(out));
83         copy(inStream, outStream);
84     }
85
86     /**
87      * Copy the contents of the given input File into a new byte array.
88      * @param in the file to copy from
89      * @return the new byte array that has been copied to
90      * @throws IOException in case of I/O errors
91      */

92     public static byte[] copyToByteArray(File JavaDoc in) throws IOException JavaDoc {
93         Assert.notNull(in, "No input File specified");
94         return copyToByteArray(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(in)));
95     }
96
97
98     //---------------------------------------------------------------------
99
// Copy methods for java.io.InputStream / java.io.OutputStream
100
//---------------------------------------------------------------------
101

102     /**
103      * Copy the contents of the given InputStream to the given OutputStream.
104      * Closes both streams when done.
105      * @param in the stream to copy from
106      * @param out the stream to copy to
107      * @return the number of bytes copied
108      * @throws IOException in case of I/O errors
109      */

110     public static int copy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
111         Assert.notNull(in, "No InputStream specified");
112         Assert.notNull(out, "No OutputStream specified");
113         try {
114             int byteCount = 0;
115             byte[] buffer = new byte[BUFFER_SIZE];
116             int bytesRead = -1;
117             while ((bytesRead = in.read(buffer)) != -1) {
118                 out.write(buffer, 0, bytesRead);
119                 byteCount += bytesRead;
120             }
121             out.flush();
122             return byteCount;
123         }
124         finally {
125             try {
126                 in.close();
127             }
128             catch (IOException JavaDoc ex) {
129                 logger.warn("Could not close InputStream", ex);
130             }
131             try {
132                 out.close();
133             }
134             catch (IOException JavaDoc ex) {
135                 logger.warn("Could not close OutputStream", ex);
136             }
137         }
138     }
139
140     /**
141      * Copy the contents of the given byte array to the given OutputStream.
142      * Closes the stream when done.
143      * @param in the byte array to copy from
144      * @param out the OutputStream to copy to
145      * @throws IOException in case of I/O errors
146      */

147     public static void copy(byte[] in, OutputStream JavaDoc out) throws IOException JavaDoc {
148         Assert.notNull(in, "No input byte array specified");
149         Assert.notNull(out, "No OutputStream specified");
150         try {
151             out.write(in);
152         }
153         finally {
154             try {
155                 out.close();
156             }
157             catch (IOException JavaDoc ex) {
158                 logger.warn("Could not close OutputStream", ex);
159             }
160         }
161     }
162
163     /**
164      * Copy the contents of the given InputStream into a new byte array.
165      * Closes the stream when done.
166      * @param in the stream to copy from
167      * @return the new byte array that has been copied to
168      * @throws IOException in case of I/O errors
169      */

170     public static byte[] copyToByteArray(InputStream JavaDoc in) throws IOException JavaDoc {
171         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(BUFFER_SIZE);
172         copy(in, out);
173         return out.toByteArray();
174     }
175
176
177     //---------------------------------------------------------------------
178
// Copy methods for java.io.Reader / java.io.Writer
179
//---------------------------------------------------------------------
180

181     /**
182      * Copy the contents of the given Reader to the given Writer.
183      * Closes both when done.
184      * @param in the Reader to copy from
185      * @param out the Writer to copy to
186      * @return the number of characters copied
187      * @throws IOException in case of I/O errors
188      */

189     public static int copy(Reader JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc {
190         Assert.notNull(in, "No Reader specified");
191         Assert.notNull(out, "No Writer specified");
192         try {
193             int byteCount = 0;
194             char[] buffer = new char[BUFFER_SIZE];
195             int bytesRead = -1;
196             while ((bytesRead = in.read(buffer)) != -1) {
197                 out.write(buffer, 0, bytesRead);
198                 byteCount += bytesRead;
199             }
200             out.flush();
201             return byteCount;
202         }
203         finally {
204             try {
205                 in.close();
206             }
207             catch (IOException JavaDoc ex) {
208                 logger.warn("Could not close Reader", ex);
209             }
210             try {
211                 out.close();
212             }
213             catch (IOException JavaDoc ex) {
214                 logger.warn("Could not close Writer", ex);
215             }
216         }
217     }
218
219     /**
220      * Copy the contents of the given String to the given output Writer.
221      * Closes the write when done.
222      * @param in the String to copy from
223      * @param out the Writer to copy to
224      * @throws IOException in case of I/O errors
225      */

226     public static void copy(String JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc {
227         Assert.notNull(in, "No input String specified");
228         Assert.notNull(out, "No Writer specified");
229         try {
230             out.write(in);
231         }
232         finally {
233             try {
234                 out.close();
235             }
236             catch (IOException JavaDoc ex) {
237                 logger.warn("Could not close Writer", ex);
238             }
239         }
240     }
241
242     /**
243      * Copy the contents of the given Reader into a String.
244      * Closes the reader when done.
245      * @param in the reader to copy from
246      * @return the String that has been copied to
247      * @throws IOException in case of I/O errors
248      */

249     public static String JavaDoc copyToString(Reader JavaDoc in) throws IOException JavaDoc {
250         StringWriter JavaDoc out = new StringWriter JavaDoc();
251         copy(in, out);
252         return out.toString();
253     }
254
255 }
256
Popular Tags