View Javadoc

1   package org.springframework.roo.support.util;
2   
3   /*
4    * Copyright 2002-2008 the original author or authors.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Simple utility methods for file and stream copying.
35   * All copy methods use a block size of 4096 bytes,
36   * and close all affected streams when done.
37   *
38   * <p>Mainly for use within the framework,
39   * but also useful for application code.
40   *
41   * @author Juergen Hoeller
42   * @since 06.10.2003
43   */
44  public abstract class FileCopyUtils {
45  
46  	public static final int BUFFER_SIZE = 4096;
47  
48  
49  	//---------------------------------------------------------------------
50  	// Copy methods for java.io.File
51  	//---------------------------------------------------------------------
52  
53  	/**
54  	 * Copy the contents of the given input File to the given output File.
55  	 * @param in the file to copy from
56  	 * @param out the file to copy to
57  	 * @return the number of bytes copied
58  	 * @throws IOException in case of I/O errors
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  	 * Copy the contents of the given byte array to the given output File.
69  	 * @param in the byte array to copy from
70  	 * @param out the file to copy to
71  	 * @throws IOException in case of I/O errors
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  	 * Copy the contents of the given input File into a new byte array.
83  	 * @param in the file to copy from
84  	 * @return the new byte array that has been copied to
85  	 * @throws IOException in case of I/O errors
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  	// Copy methods for java.io.InputStream / java.io.OutputStream
95  	//---------------------------------------------------------------------
96  
97  	/**
98  	 * Copy the contents of the given InputStream to the given OutputStream.
99  	 * Closes both streams when done.
100 	 * @param in the stream to copy from
101 	 * @param out the stream to copy to
102 	 * @return the number of bytes copied
103 	 * @throws IOException in case of I/O errors
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 	 * Copy the contents of the given byte array to the given OutputStream.
135 	 * Closes the stream when done.
136 	 * @param in the byte array to copy from
137 	 * @param out the OutputStream to copy to
138 	 * @throws IOException in case of I/O errors
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 	 * Copy the contents of the given InputStream into a new byte array.
157 	 * Closes the stream when done.
158 	 * @param in the stream to copy from
159 	 * @return the new byte array that has been copied to
160 	 * @throws IOException in case of I/O errors
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 	// Copy methods for java.io.Reader / java.io.Writer
171 	//---------------------------------------------------------------------
172 
173 	/**
174 	 * Copy the contents of the given Reader to the given Writer.
175 	 * Closes both when done.
176 	 * @param in the Reader to copy from
177 	 * @param out the Writer to copy to
178 	 * @return the number of characters copied
179 	 * @throws IOException in case of I/O errors
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 	 * Copy the contents of the given String to the given output Writer.
211 	 * Closes the write when done.
212 	 * @param in the String to copy from
213 	 * @param out the Writer to copy to
214 	 * @throws IOException in case of I/O errors
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 	 * Copy the contents of the given Reader into a String.
233 	 * Closes the reader when done.
234 	 * @param in the reader to copy from
235 	 * @return the String that has been copied to
236 	 * @throws IOException in case of I/O errors
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