001: /*
002: * Copyright 2004-2006 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.compass.core.util;
018:
019: import java.io.*;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * Simple utility methods for file and stream copying.
026: * All copy methods use a block size of 4096 bytes,
027: * and close all affected streams when done.
028: *
029: * @author kimhcy
030: */
031: public abstract class CopyUtils {
032:
033: private static final Log log = LogFactory.getLog(CopyUtils.class);
034:
035: public static final int BUFFER_SIZE = 4096;
036:
037: //---------------------------------------------------------------------
038: // Copy methods for java.io.File
039: //---------------------------------------------------------------------
040:
041: /**
042: * Copy the contents of the given input File to the given output File.
043: *
044: * @param in the file to copy from
045: * @param out the file to copy to
046: * @return the number of bytes copied
047: * @throws IOException in case of I/O errors
048: */
049: public static int copy(File in, File out) throws IOException {
050: Assert.notNull(in, "No input File specified");
051: Assert.notNull(out, "No output File specified");
052: return copy(new BufferedInputStream(new FileInputStream(in)),
053: new BufferedOutputStream(new FileOutputStream(out)));
054: }
055:
056: /**
057: * Copy the contents of the given byte array to the given output File.
058: *
059: * @param in the byte array to copy from
060: * @param out the file to copy to
061: * @throws IOException in case of I/O errors
062: */
063: public static void copy(byte[] in, File out) throws IOException {
064: Assert.notNull(in, "No input byte array specified");
065: Assert.notNull(out, "No output File specified");
066: ByteArrayInputStream inStream = new ByteArrayInputStream(in);
067: OutputStream outStream = new BufferedOutputStream(
068: new FileOutputStream(out));
069: copy(inStream, outStream);
070: }
071:
072: /**
073: * Copy the contents of the given input File into a new byte array.
074: *
075: * @param in the file to copy from
076: * @return the new byte array that has been copied to
077: * @throws IOException in case of I/O errors
078: */
079: public static byte[] copyToByteArray(File in) throws IOException {
080: Assert.notNull(in, "No input File specified");
081: return copyToByteArray(new BufferedInputStream(
082: new FileInputStream(in)));
083: }
084:
085: //---------------------------------------------------------------------
086: // Copy methods for java.io.InputStream / java.io.OutputStream
087: //---------------------------------------------------------------------
088:
089: /**
090: * Copy the contents of the given InputStream to the given OutputStream.
091: * Closes both streams when done.
092: *
093: * @param in the stream to copy from
094: * @param out the stream to copy to
095: * @return the number of bytes copied
096: * @throws IOException in case of I/O errors
097: */
098: public static int copy(InputStream in, OutputStream out)
099: throws IOException {
100: Assert.notNull(in, "No InputStream specified");
101: Assert.notNull(out, "No OutputStream specified");
102: try {
103: int byteCount = 0;
104: byte[] buffer = new byte[BUFFER_SIZE];
105: int bytesRead = -1;
106: while ((bytesRead = in.read(buffer)) != -1) {
107: out.write(buffer, 0, bytesRead);
108: byteCount += bytesRead;
109: }
110: out.flush();
111: return byteCount;
112: } finally {
113: try {
114: in.close();
115: } catch (IOException ex) {
116: log.warn("Could not close InputStream", ex);
117: }
118: try {
119: out.close();
120: } catch (IOException ex) {
121: log.warn("Could not close OutputStream", ex);
122: }
123: }
124: }
125:
126: /**
127: * Copy the contents of the given byte array to the given OutputStream.
128: * Closes the stream when done.
129: *
130: * @param in the byte array to copy from
131: * @param out the OutputStream to copy to
132: * @throws IOException in case of I/O errors
133: */
134: public static void copy(byte[] in, OutputStream out)
135: throws IOException {
136: Assert.notNull(in, "No input byte array specified");
137: Assert.notNull(out, "No OutputStream specified");
138: try {
139: out.write(in);
140: } finally {
141: try {
142: out.close();
143: } catch (IOException ex) {
144: log.warn("Could not close OutputStream", ex);
145: }
146: }
147: }
148:
149: /**
150: * Copy the contents of the given InputStream into a new byte array.
151: * Closes the stream when done.
152: *
153: * @param in the stream to copy from
154: * @return the new byte array that has been copied to
155: * @throws IOException in case of I/O errors
156: */
157: public static byte[] copyToByteArray(InputStream in)
158: throws IOException {
159: ByteArrayOutputStream out = new ByteArrayOutputStream(
160: BUFFER_SIZE);
161: copy(in, out);
162: return out.toByteArray();
163: }
164:
165: //---------------------------------------------------------------------
166: // Copy methods for java.io.Reader / java.io.Writer
167: //---------------------------------------------------------------------
168:
169: /**
170: * Copy the contents of the given Reader to the given Writer.
171: * Closes both when done.
172: *
173: * @param in the Reader to copy from
174: * @param out the Writer to copy to
175: * @return the number of characters copied
176: * @throws IOException in case of I/O errors
177: */
178: public static int copy(Reader in, Writer out) throws IOException {
179: Assert.notNull(in, "No Reader specified");
180: Assert.notNull(out, "No Writer specified");
181: try {
182: int byteCount = 0;
183: char[] buffer = new char[BUFFER_SIZE];
184: int bytesRead = -1;
185: while ((bytesRead = in.read(buffer)) != -1) {
186: out.write(buffer, 0, bytesRead);
187: byteCount += bytesRead;
188: }
189: out.flush();
190: return byteCount;
191: } finally {
192: try {
193: in.close();
194: } catch (IOException ex) {
195: log.warn("Could not close Reader", ex);
196: }
197: try {
198: out.close();
199: } catch (IOException ex) {
200: log.warn("Could not close Writer", ex);
201: }
202: }
203: }
204:
205: /**
206: * Copy the contents of the given String to the given output Writer.
207: * Closes the write when done.
208: *
209: * @param in the String to copy from
210: * @param out the Writer to copy to
211: * @throws IOException in case of I/O errors
212: */
213: public static void copy(String in, Writer out) throws IOException {
214: Assert.notNull(in, "No input String specified");
215: Assert.notNull(out, "No Writer specified");
216: try {
217: out.write(in);
218: } finally {
219: try {
220: out.close();
221: } catch (IOException ex) {
222: log.warn("Could not close Writer", ex);
223: }
224: }
225: }
226:
227: /**
228: * Copy the contents of the given Reader into a String.
229: * Closes the reader when done.
230: *
231: * @param in the reader to copy from
232: * @return the String that has been copied to
233: * @throws IOException in case of I/O errors
234: */
235: public static String copyToString(Reader in) throws IOException {
236: StringWriter out = new StringWriter();
237: copy(in, out);
238: return out.toString();
239: }
240:
241: }
|