001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.util;
018:
019: import java.io.BufferedInputStream;
020: import java.io.BufferedOutputStream;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029: import java.io.Reader;
030: import java.io.StringWriter;
031: import java.io.Writer;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * Simple utility methods for file and stream copying.
038: * All copy methods use a block size of 4096 bytes,
039: * and close all affected streams when done.
040: *
041: * <p>Mainly for use within the framework,
042: * but also useful for application code.
043: *
044: * @author Juergen Hoeller
045: * @since 06.10.2003
046: */
047: public abstract class FileCopyUtils {
048:
049: private static final Log logger = LogFactory
050: .getLog(FileCopyUtils.class);
051:
052: public static final int BUFFER_SIZE = 4096;
053:
054: //---------------------------------------------------------------------
055: // Copy methods for java.io.File
056: //---------------------------------------------------------------------
057:
058: /**
059: * Copy the contents of the given input File to the given output File.
060: * @param in the file to copy from
061: * @param out the file to copy to
062: * @return the number of bytes copied
063: * @throws IOException in case of I/O errors
064: */
065: public static int copy(File in, File out) throws IOException {
066: Assert.notNull(in, "No input File specified");
067: Assert.notNull(out, "No output File specified");
068: return copy(new BufferedInputStream(new FileInputStream(in)),
069: new BufferedOutputStream(new FileOutputStream(out)));
070: }
071:
072: /**
073: * Copy the contents of the given byte array to the given output File.
074: * @param in the byte array to copy from
075: * @param out the file to copy to
076: * @throws IOException in case of I/O errors
077: */
078: public static void copy(byte[] in, File out) throws IOException {
079: Assert.notNull(in, "No input byte array specified");
080: Assert.notNull(out, "No output File specified");
081: ByteArrayInputStream inStream = new ByteArrayInputStream(in);
082: OutputStream outStream = new BufferedOutputStream(
083: new FileOutputStream(out));
084: copy(inStream, outStream);
085: }
086:
087: /**
088: * Copy the contents of the given input File into a new byte array.
089: * @param in the file to copy from
090: * @return the new byte array that has been copied to
091: * @throws IOException in case of I/O errors
092: */
093: public static byte[] copyToByteArray(File in) throws IOException {
094: Assert.notNull(in, "No input File specified");
095: return copyToByteArray(new BufferedInputStream(
096: new FileInputStream(in)));
097: }
098:
099: //---------------------------------------------------------------------
100: // Copy methods for java.io.InputStream / java.io.OutputStream
101: //---------------------------------------------------------------------
102:
103: /**
104: * Copy the contents of the given InputStream to the given OutputStream.
105: * Closes both streams when done.
106: * @param in the stream to copy from
107: * @param out the stream to copy to
108: * @return the number of bytes copied
109: * @throws IOException in case of I/O errors
110: */
111: public static int copy(InputStream in, OutputStream out)
112: throws IOException {
113: Assert.notNull(in, "No InputStream specified");
114: Assert.notNull(out, "No OutputStream specified");
115: try {
116: int byteCount = 0;
117: byte[] buffer = new byte[BUFFER_SIZE];
118: int bytesRead = -1;
119: while ((bytesRead = in.read(buffer)) != -1) {
120: out.write(buffer, 0, bytesRead);
121: byteCount += bytesRead;
122: }
123: out.flush();
124: return byteCount;
125: } finally {
126: try {
127: in.close();
128: } catch (IOException ex) {
129: logger.warn("Could not close InputStream", ex);
130: }
131: try {
132: out.close();
133: } catch (IOException ex) {
134: logger.warn("Could not close OutputStream", ex);
135: }
136: }
137: }
138:
139: /**
140: * Copy the contents of the given byte array to the given OutputStream.
141: * Closes the stream when done.
142: * @param in the byte array to copy from
143: * @param out the OutputStream to copy to
144: * @throws IOException in case of I/O errors
145: */
146: public static void copy(byte[] in, OutputStream out)
147: throws IOException {
148: Assert.notNull(in, "No input byte array specified");
149: Assert.notNull(out, "No OutputStream specified");
150: try {
151: out.write(in);
152: } finally {
153: try {
154: out.close();
155: } catch (IOException ex) {
156: logger.warn("Could not close OutputStream", ex);
157: }
158: }
159: }
160:
161: /**
162: * Copy the contents of the given InputStream into a new byte array.
163: * Closes the stream when done.
164: * @param in the stream to copy from
165: * @return the new byte array that has been copied to
166: * @throws IOException in case of I/O errors
167: */
168: public static byte[] copyToByteArray(InputStream in)
169: throws IOException {
170: ByteArrayOutputStream out = new ByteArrayOutputStream(
171: BUFFER_SIZE);
172: copy(in, out);
173: return out.toByteArray();
174: }
175:
176: //---------------------------------------------------------------------
177: // Copy methods for java.io.Reader / java.io.Writer
178: //---------------------------------------------------------------------
179:
180: /**
181: * Copy the contents of the given Reader to the given Writer.
182: * Closes both when done.
183: * @param in the Reader to copy from
184: * @param out the Writer to copy to
185: * @return the number of characters copied
186: * @throws IOException in case of I/O errors
187: */
188: public static int copy(Reader in, Writer out) throws IOException {
189: Assert.notNull(in, "No Reader specified");
190: Assert.notNull(out, "No Writer specified");
191: try {
192: int byteCount = 0;
193: char[] buffer = new char[BUFFER_SIZE];
194: int bytesRead = -1;
195: while ((bytesRead = in.read(buffer)) != -1) {
196: out.write(buffer, 0, bytesRead);
197: byteCount += bytesRead;
198: }
199: out.flush();
200: return byteCount;
201: } finally {
202: try {
203: in.close();
204: } catch (IOException ex) {
205: logger.warn("Could not close Reader", ex);
206: }
207: try {
208: out.close();
209: } catch (IOException ex) {
210: logger.warn("Could not close Writer", ex);
211: }
212: }
213: }
214:
215: /**
216: * Copy the contents of the given String to the given output Writer.
217: * Closes the write when done.
218: * @param in the String to copy from
219: * @param out the Writer to copy to
220: * @throws IOException in case of I/O errors
221: */
222: public static void copy(String in, Writer out) throws IOException {
223: Assert.notNull(in, "No input String specified");
224: Assert.notNull(out, "No Writer specified");
225: try {
226: out.write(in);
227: } finally {
228: try {
229: out.close();
230: } catch (IOException ex) {
231: logger.warn("Could not close Writer", ex);
232: }
233: }
234: }
235:
236: /**
237: * Copy the contents of the given Reader into a String.
238: * Closes the reader when done.
239: * @param in the reader to copy from
240: * @return the String that has been copied to
241: * @throws IOException in case of I/O errors
242: */
243: public static String copyToString(Reader in) throws IOException {
244: StringWriter out = new StringWriter();
245: copy(in, out);
246: return out.toString();
247: }
248:
249: }
|