1 /*
2 * MicroEmulator
3 * Copyright (C) 2002 Bartek Teodorczyk <barteo@barteo.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package org.microemu.app.util;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.Writer;
29
30 /**
31 * General IO stream manipulation utilities.
32 * Some functions are based on org.apache.commons.io
33 *
34 * <p>
35 * This class provides static utility methods for input/output operations.
36 * <ul>
37 * <li>closeQuietly - these methods close a stream ignoring nulls and exceptions
38 * </ul>
39 * <p>
40 */
41
42 public class IOUtils {
43
44 /**
45 * Solution for JVM bug
46 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351751
47 */
48 public static String getCanonicalFileURL(File file) {
49 String path = file.getAbsoluteFile().getPath();
50 if (File.separatorChar != '/') {
51 path = path.replace(File.separatorChar, '/');
52 }
53 // Not network path
54 if (!path.startsWith("//")) {
55 if (path.startsWith("/")) {
56 path = "//" + path;
57 } else {
58 path = "///" + path;
59 }
60 }
61 return "file:" + path;
62 }
63
64 public static String getCanonicalFileClassLoaderURL(File file) {
65 String url = getCanonicalFileURL(file);
66 if ((file.isDirectory()) && (!url.endsWith("/"))) {
67 url += "/";
68 }
69 return url;
70 }
71
72 public static void copyFile(File src, File dst) throws IOException {
73 FileInputStream fis = null;
74 try {
75 fis = new FileInputStream(src);
76 copyToFile(fis, dst);
77 } finally {
78 closeQuietly(fis);
79 }
80 }
81
82 public static void copyToFile(InputStream is, File dst) throws IOException {
83 FileOutputStream fos = null;
84 try {
85 fos = new FileOutputStream(dst);
86 byte[] buf = new byte[1024];
87 int i = 0;
88 while ((i = is.read(buf)) != -1) {
89 fos.write(buf, 0, i);
90 }
91 } finally {
92 closeQuietly(fos);
93 }
94 }
95
96 /**
97 * Unconditionally close an <code>InputStream</code>.
98 * <p>
99 * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
100 * This is typically used in finally blocks.
101 *
102 * @param input the InputStream to close, may be null or already closed
103 */
104 public static void closeQuietly(InputStream input) {
105 try {
106 if (input != null) {
107 input.close();
108 }
109 } catch (IOException ignore) {
110 // ignore
111 }
112 }
113
114 /**
115 * Unconditionally close an <code>OutputStream</code>.
116 * <p>
117 * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
118 * This is typically used in finally blocks.
119 *
120 * @param output the OutputStream to close, may be null or already closed
121 */
122 public static void closeQuietly(OutputStream output) {
123 try {
124 if (output != null) {
125 output.close();
126 }
127 } catch (IOException ignore) {
128 // ignore
129 }
130 }
131
132 /**
133 * Unconditionally close a <code>Writer</code>.
134 * <p>
135 * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
136 * This is typically used in finally blocks.
137 *
138 * @param output the Writer to close, may be null or already closed
139 */
140 public static void closeQuietly(Writer output) {
141 try {
142 if (output != null) {
143 output.close();
144 }
145 } catch (IOException ioe) {
146 // ignore
147 }
148 }
149 }