View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2002 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   */
24  
25  package org.microemu.app.util;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileOutputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.io.Writer;
34  
35  /**
36   * General IO stream manipulation utilities.
37   * Some functions are based on org.apache.commons.io
38   * 
39   * <p>
40   * This class provides static utility methods for input/output operations.
41   * <ul>
42   * <li>closeQuietly - these methods close a stream ignoring nulls and exceptions
43   * </ul>
44   * <p>
45   */
46  
47  public class IOUtils {
48  
49  	/**
50  	 * Solution for JVM bug 
51  	 *  http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351751
52  	 */
53  	public static String getCanonicalFileURL(File file) {
54  		String path = file.getAbsoluteFile().getPath();
55  		if (File.separatorChar != '/') {
56  			path = path.replace(File.separatorChar, '/');
57  		}
58  		// Not network path
59  		if (!path.startsWith("//")) {
60  			if (path.startsWith("/")) {
61  				path = "//" + path;
62  			} else {
63  				path = "///" + path;
64  			}
65  		}
66  		return "file:" + path;
67  	}
68  	
69  	public static String getCanonicalFileClassLoaderURL(File file) {
70  		String url = getCanonicalFileURL(file);
71  		if ((file.isDirectory()) && (!url.endsWith("/"))) {
72  			url += "/";
73  		}
74  		return url;
75  	}
76  	
77  	public static void copyFile(File src, File dst) throws IOException {
78  		FileInputStream fis = null;
79  		try {
80  			fis = new FileInputStream(src);
81  			copyToFile(fis, dst);
82  		} finally {
83  			closeQuietly(fis); 
84  		}
85  	}
86  	
87  	public static void copyToFile(InputStream is, File dst) throws IOException {
88  		FileOutputStream fos = null;
89  		try {
90  			fos = new FileOutputStream(dst);
91  			byte[] buf = new byte[1024]; 
92  			int i = 0;
93  			while ((i = is.read(buf)) != -1) { 
94  				fos.write(buf, 0, i);
95  			}
96  		} finally {
97  			closeQuietly(fos);	
98  		}
99  	}
100 	
101     /**
102      * Unconditionally close an <code>InputStream</code>.
103      * <p>
104      * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
105      * This is typically used in finally blocks.
106      *
107      * @param input  the InputStream to close, may be null or already closed
108      */
109     public static void closeQuietly(InputStream input) {
110         try {
111             if (input != null) {
112                 input.close();
113             }
114         } catch (IOException ignore) {
115             // ignore
116         }
117     }
118     
119     /**
120      * Unconditionally close an <code>OutputStream</code>.
121      * <p>
122      * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
123      * This is typically used in finally blocks.
124      *
125      * @param output  the OutputStream to close, may be null or already closed
126      */
127     public static void closeQuietly(OutputStream output) {
128         try {
129             if (output != null) {
130                 output.close();
131             }
132         } catch (IOException ignore) {
133             // ignore
134         }
135     }
136     
137     /**
138      * Unconditionally close a <code>Writer</code>.
139      * <p>
140      * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
141      * This is typically used in finally blocks.
142      *
143      * @param output  the Writer to close, may be null or already closed
144      */
145     public static void closeQuietly(Writer output) {
146         try {
147             if (output != null) {
148                 output.close();
149             }
150         } catch (IOException ioe) {
151             // ignore
152         }
153     }
154 }