1 | /* |
2 | Copyright (C) 2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License version 2 as |
6 | published by the Free Software Foundation. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | |
17 | */ |
18 | package com.mysql.management.util; |
19 | |
20 | import java.io.BufferedInputStream; |
21 | import java.io.BufferedOutputStream; |
22 | import java.io.ByteArrayOutputStream; |
23 | import java.io.File; |
24 | import java.io.FileNotFoundException; |
25 | import java.io.FileOutputStream; |
26 | import java.io.IOException; |
27 | import java.io.InputStream; |
28 | import java.io.OutputStream; |
29 | import java.util.MissingResourceException; |
30 | import java.util.jar.JarEntry; |
31 | import java.util.jar.JarInputStream; |
32 | |
33 | /** |
34 | * Stream operation utility methods. |
35 | * |
36 | * This class is final simply as a hint to the compiler, it may be un-finalized |
37 | * safely. |
38 | * |
39 | * @author Eric Herman <eric@mysql.com> |
40 | * @version $Id: Streams.java,v 1.12 2005/12/01 21:45:31 eherman Exp $ |
41 | */ |
42 | public final class Streams { |
43 | |
44 | public static final String RESOURCE_SEPARATOR = "/"; |
45 | |
46 | private static final int END_OF_STREAM = -1; |
47 | |
48 | private Exceptions exceptions; |
49 | |
50 | public Streams() { |
51 | this.exceptions = new Exceptions(); |
52 | } |
53 | |
54 | /** |
55 | * Reads the data from the Input stream and writes to the output stream |
56 | * Buffers each stream. Terminates when a read from the Input stream results |
57 | * in EOF |
58 | */ |
59 | public void copy(InputStream from, OutputStream to) throws IOException { |
60 | copy(from, to, true, false); |
61 | } |
62 | |
63 | void copy(InputStream from, OutputStream to, boolean buffer, |
64 | boolean terminateOnFailure) throws IOException { |
65 | if (buffer) { |
66 | from = new BufferedInputStream(from); |
67 | to = new BufferedOutputStream(to); |
68 | } |
69 | while (true) { |
70 | int i; |
71 | try { |
72 | i = from.read(); |
73 | if (i == END_OF_STREAM) { |
74 | break; |
75 | } |
76 | to.write(i); |
77 | } catch (Exception e) { |
78 | if (terminateOnFailure) { |
79 | break; |
80 | } |
81 | if (e instanceof IOException) { |
82 | throw (IOException) e; |
83 | } |
84 | throw exceptions.toRuntime(e); |
85 | } |
86 | } |
87 | to.flush(); |
88 | } |
89 | |
90 | /** reads the entire contents of stream into a String */ |
91 | public String readString(InputStream from) throws IOException { |
92 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
93 | try { |
94 | copy(from, buf); |
95 | return buf.toString(); |
96 | } finally { |
97 | buf.close(); |
98 | } |
99 | } |
100 | |
101 | /** |
102 | * Uses ClassLoader which loaded Streams.class to obtain resource |
103 | * |
104 | * @return an InputStream |
105 | * @throws MissingResourceException |
106 | */ |
107 | public InputStream getResourceAsStream(String name) { |
108 | ClassLoader classLoader = getClass().getClassLoader(); |
109 | return getResourceAsStream(classLoader, name); |
110 | } |
111 | |
112 | /** |
113 | * Convenience method to check for MissingResource (null stream) |
114 | * |
115 | * @return an InputStream |
116 | * @throws MissingResourceException |
117 | */ |
118 | public InputStream getResourceAsStream(ClassLoader classLoader, String name) { |
119 | InputStream is = classLoader.getResourceAsStream(name); |
120 | if (is == null) { |
121 | String msg = "Resource '" + name + "' not found"; |
122 | throw new MissingResourceException(msg, null, name); |
123 | } |
124 | return is; |
125 | } |
126 | |
127 | /** |
128 | * Copies a resource to the location specified by the File parameter. |
129 | */ |
130 | public void createFileFromResource(final String resourceName, |
131 | final File file) { |
132 | File parent = file.getParentFile(); |
133 | if (parent != null) { |
134 | parent.mkdirs(); |
135 | } |
136 | Exceptions.VoidBlock block = new Exceptions.VoidBlock() { |
137 | public void inner() throws Exception { |
138 | InputStream is = getResourceAsStream(resourceName); |
139 | try { |
140 | FileOutputStream fos = new FileOutputStream(file); |
141 | try { |
142 | copy(is, fos); |
143 | } finally { |
144 | fos.close(); |
145 | } |
146 | } finally { |
147 | is.close(); |
148 | } |
149 | } |
150 | }; |
151 | block.exec(); |
152 | |
153 | } |
154 | |
155 | /** |
156 | * If the jar exists as a resource, the contents of the jar will be expanded |
157 | * on the file system in the location specified by the File parameter. |
158 | * |
159 | * If any files already exist, they will <b>not </b> be over-written. |
160 | */ |
161 | public void expandResourceJar(final File outputDir, |
162 | final String jarResourceName) { |
163 | Exceptions.VoidBlock block = new Exceptions.VoidBlock() { |
164 | public void inner() throws Exception { |
165 | expandResourceJarInner(outputDir, jarResourceName); |
166 | } |
167 | }; |
168 | block.exec(); |
169 | } |
170 | |
171 | private void expandResourceJarInner(File outputDir, String jarResourceName) |
172 | throws IOException { |
173 | InputStream is = getResourceAsStream(jarResourceName); |
174 | try { |
175 | JarInputStream jis = new JarInputStream(is); |
176 | try { |
177 | expandEachEntry(outputDir, jis); |
178 | } finally { |
179 | jis.close(); |
180 | } |
181 | } finally { |
182 | is.close(); |
183 | } |
184 | } |
185 | |
186 | private void expandEachEntry(File outputDir, JarInputStream jis) |
187 | throws IOException, FileNotFoundException { |
188 | while (true) { |
189 | JarEntry entry = jis.getNextJarEntry(); |
190 | if (entry == null) { |
191 | break; |
192 | } |
193 | |
194 | File file = new File(outputDir, entry.getName()); |
195 | if (!file.exists()) { |
196 | if (entry.isDirectory()) { |
197 | file.mkdirs(); |
198 | } else { |
199 | File parent = file.getParentFile(); |
200 | parent.mkdirs(); |
201 | FileOutputStream fos = new FileOutputStream(file); |
202 | try { |
203 | copy(jis, fos); |
204 | } finally { |
205 | fos.close(); |
206 | } |
207 | } |
208 | } |
209 | } |
210 | } |
211 | } |