001: /*--
002:
003: Copyright (C) 2000-2003 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The name "EdenLib" must not be used to endorse or promote products
019: derived from this software without prior written permission. For
020: written permission, please contact me@anthonyeden.com.
021:
022: 4. Products derived from this software may not be called "EdenLib", nor
023: may "EdenLib" appear in their name, without prior written permission
024: from Anthony Eden (me@anthonyeden.com).
025:
026: In addition, I request (but do not require) that you include in the
027: end-user documentation provided with the redistribution and/or in the
028: software itself an acknowledgement equivalent to the following:
029: "This product includes software developed by
030: Anthony Eden (http://www.anthonyeden.com/)."
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
036: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
037: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
038: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
039: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
040: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
041: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
042: POSSIBILITY OF SUCH DAMAGE.
043:
044: For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:
046: */
047:
048: package com.anthonyeden.lib.util;
049:
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052: import org.apache.commons.vfs.FileContent;
053: import org.apache.commons.vfs.FileObject;
054:
055: import java.io.*;
056:
057: /**
058: * Useful IO utilities.
059: *
060: * @author Anthony Eden
061: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
062: */
063:
064: public class IOUtilities {
065:
066: private static final Log log = LogFactory.getLog(IOUtilities.class);
067: public static final int BUFFER_SIZE = 4096;
068:
069: private IOUtilities() {
070:
071: }
072:
073: /**
074: * Close the given stream if the stream is not null.
075: *
076: * @param s The stream
077: */
078:
079: public static void close(InputStream s) {
080: if (s != null) {
081: try {
082: s.close();
083: } catch (Exception e) {
084: log.error("Error closing stream: " + e.getMessage());
085: }
086: }
087: }
088:
089: /**
090: * Close the given stream if the stream is not null.
091: *
092: * @param s The stream
093: */
094:
095: public static void close(Reader s) {
096: if (s != null) {
097: try {
098: s.close();
099: } catch (Exception e) {
100: log.error("Error closing reader: " + e.getMessage());
101: }
102: }
103: }
104:
105: /**
106: * Close the given stream if the stream is not null.
107: *
108: * @param s The stream
109: */
110:
111: public static void close(OutputStream s) {
112: if (s != null) {
113: try {
114: s.close();
115: } catch (Exception e) {
116: log.error("Error closing stream: " + e.getMessage());
117: }
118: }
119: }
120:
121: /**
122: * Close the given stream if the stream is not null.
123: *
124: * @param s The stream
125: */
126:
127: public static void close(Writer s) {
128: if (s != null) {
129: try {
130: s.close();
131: } catch (Exception e) {
132: log.error("Error closing writer: " + e.getMessage());
133: }
134: }
135: }
136:
137: /**
138: * Read the data from the given file into a byte array and return
139: * the array.
140: *
141: * @param file The file
142: * @return The byte array
143: * @throws IOException
144: */
145:
146: public static byte[] readData(File file) throws IOException {
147: BufferedInputStream in = null;
148: ByteArrayOutputStream out = null;
149:
150: try {
151: in = new BufferedInputStream(new FileInputStream(file));
152: out = new ByteArrayOutputStream();
153:
154: int c = -1;
155: while ((c = in.read()) != -1) {
156: out.write(c);
157: }
158:
159: return out.toByteArray();
160: } finally {
161: IOUtilities.close(in);
162: IOUtilities.close(out);
163: }
164: }
165:
166: /**
167: * Read the data from the given file into a byte array and return
168: * the array.
169: *
170: * @param file The file
171: * @return The byte array
172: * @throws IOException
173: */
174:
175: public static byte[] readData(FileObject file) throws IOException {
176: BufferedInputStream in = null;
177: ByteArrayOutputStream out = null;
178:
179: try {
180: FileContent content = file.getContent();
181: in = new BufferedInputStream(content.getInputStream());
182: out = new ByteArrayOutputStream();
183:
184: int c = -1;
185: while ((c = in.read()) != -1) {
186: out.write(c);
187: }
188:
189: return out.toByteArray();
190: } finally {
191: IOUtilities.close(in);
192: IOUtilities.close(out);
193: }
194: }
195:
196: /**
197: * Write the byte array to the given file.
198: *
199: * @param file The file to write to
200: * @param data The data array
201: * @throws IOException
202: */
203:
204: public static void writeData(File file, byte[] data)
205: throws IOException {
206: FileOutputStream out = null;
207: try {
208: out = new FileOutputStream(file);
209: out.write(data);
210: } finally {
211: close(out);
212: }
213: }
214:
215: /**
216: * Write the byte array to the given file.
217: *
218: * @param file The file to write to
219: * @param data The data array
220: * @throws IOException
221: */
222:
223: public static void writeData(FileObject file, byte[] data)
224: throws IOException {
225: OutputStream out = null;
226: try {
227: FileContent content = file.getContent();
228: out = content.getOutputStream();
229: out.write(data);
230: } finally {
231: close(out);
232: }
233: }
234:
235: /**
236: * Read the data from the given reader and return it is a single String.
237: *
238: * @param in The Reader
239: * @return The String
240: * @throws IOException
241: */
242:
243: public static String getStringFromReader(Reader in)
244: throws IOException {
245: return copyToString(in);
246: }
247:
248: /**
249: * Copy the contents of the given Reader into a String.
250: * Closes the reader when done.
251: *
252: * @param in the reader to copy from
253: * @return the String that has been copied to
254: * @throws IOException in case of I/O errors
255: */
256: public static String copyToString(Reader in) throws IOException {
257: StringWriter out = new StringWriter();
258: copy(in, out);
259: return out.toString();
260: }
261:
262: /**
263: * Copy the contents of the given Reader to the given Writer.
264: * Closes both when done.
265: *
266: * @param in the Reader to copy from
267: * @param out the Writer to copy to
268: * @return the number of characters copied
269: * @throws IOException in case of I/O errors
270: */
271: public static int copy(Reader in, Writer out) throws IOException {
272: try {
273: int byteCount = 0;
274: char[] buffer = new char[BUFFER_SIZE];
275: int bytesRead = -1;
276: while ((bytesRead = in.read(buffer)) != -1) {
277: out.write(buffer, 0, bytesRead);
278: byteCount += bytesRead;
279: }
280: out.flush();
281: return byteCount;
282: } finally {
283: try {
284: in.close();
285: } catch (IOException ex) {
286: log.warn("Could not close Reader", ex);
287: }
288: try {
289: out.close();
290: } catch (IOException ex) {
291: log.warn("Could not close Writer", ex);
292: }
293: }
294: }
295: }
|