1 | /** |
2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
3 | */ |
4 | package org.sourceforge.xradar.util; |
5 | |
6 | import java.io.BufferedInputStream; |
7 | import java.io.BufferedWriter; |
8 | import java.io.File; |
9 | import java.io.FileInputStream; |
10 | import java.io.FileNotFoundException; |
11 | import java.io.FileOutputStream; |
12 | import java.io.FileWriter; |
13 | import java.io.IOException; |
14 | import java.io.InputStream; |
15 | import java.io.OutputStream; |
16 | import java.io.Writer; |
17 | import java.net.MalformedURLException; |
18 | import java.net.URL; |
19 | |
20 | import javax.xml.transform.stream.StreamResult; |
21 | |
22 | import org.sourceforge.xradar.XRadarException; |
23 | |
24 | /** |
25 | * <p>An utility class to regroup every common functions |
26 | * about stream handling.</p> |
27 | * |
28 | * @author rpelisse, belaran@gmail.com |
29 | * |
30 | */ |
31 | //TODO: Maybe a good candidate to create java5 static imports |
32 | public final class StreamUtils { |
33 | |
34 | private static final int EOF = -1; |
35 | // Should not be instantiated |
36 | private StreamUtils() {} |
37 | |
38 | /** |
39 | * <p>Handy method to easily copy from one inputstream to a file |
40 | * <code> |
41 | * copy(new File("source.dat"), new File("destination.dat")); |
42 | * </code> |
43 | * </p> |
44 | * @param src, inputstream <u>to copy</u>. |
45 | * @param dst, outstream. |
46 | * @throws Exception |
47 | */ |
48 | public static void copy(InputStream in, OutputStream out) throws XRadarException |
49 | { |
50 | try { |
51 | StreamUtils.copyStreamToStream(in, out); |
52 | } catch (FileNotFoundException e) { |
53 | new XRadarException("Failed to copy",e); |
54 | } catch (Exception e) { |
55 | new XRadarException("Failed to copy",e); |
56 | } |
57 | } |
58 | |
59 | /** |
60 | * <p>Commodity method to copy a file into an other file, based only with |
61 | * their filename. Also, wraps any exception around an XRadarException.</p> |
62 | * |
63 | * @param fileIn |
64 | * @param fileOut |
65 | * @throws XRadarException |
66 | */ |
67 | public static void copyFileToFile(String fileIn, String fileOut) throws XRadarException |
68 | { |
69 | |
70 | try { |
71 | StreamUtils.copy(new FileInputStream(fileIn), new FileOutputStream(fileOut)); |
72 | } catch (FileNotFoundException e) { |
73 | new XRadarException("Failed to copy",e); |
74 | } catch (Exception e) { |
75 | new XRadarException("Failed to copy",e); |
76 | } |
77 | } |
78 | |
79 | /** |
80 | * <p>Commodity method to copy an inputStream into an Outputstream</p> |
81 | * @param in |
82 | * @param out |
83 | * @throws IOException |
84 | */ |
85 | public static void copyStreamToStream(InputStream in, OutputStream out) throws IOException |
86 | { |
87 | // copying data, byte to byte. |
88 | byte[] buf = new byte[1024]; |
89 | int len; |
90 | while ( ( len = in.read(buf) ) > 0 ) //NOPMD |
91 | out.write(buf, 0, len); |
92 | // closing both resources |
93 | in.close(); |
94 | out.close(); |
95 | } |
96 | |
97 | /* |
98 | * Commodity method |
99 | */ |
100 | public static StreamResult createOuput(Writer writer) { |
101 | return new StreamResult(new BufferedWriter(writer)); |
102 | } |
103 | |
104 | /** |
105 | * <p>Commodity method to construct a simple FileWriter with its filename.</p> |
106 | * |
107 | * @param output |
108 | * @return |
109 | * @throws XRadarException |
110 | */ |
111 | public static Writer createFileWriter(String output) throws XRadarException { |
112 | Writer writer = null; |
113 | try { |
114 | writer = new FileWriter(new File(output)); |
115 | } catch (IOException e) { |
116 | throw new XRadarException("Can't create file " + output,e); |
117 | } |
118 | return writer; |
119 | } |
120 | |
121 | /** |
122 | * Get an InputStream from a path filename. |
123 | * @throws XRadarException |
124 | */ |
125 | public static InputStream retrieveFileContentAsStream(String filename) throws XRadarException { |
126 | try |
127 | { |
128 | //FIXME: move to calling this following line to any calling code |
129 | //logger.log(Level.FINE,"Retrieving filename from URL:" + filename); |
130 | return new BufferedInputStream(new URL(filename).openStream()); |
131 | } |
132 | catch (MalformedURLException e) { |
133 | throw new XRadarException("Error while retrieving filename from URL" + filename,e); |
134 | } |
135 | catch (IOException e) { |
136 | throw new XRadarException("Error while retrieving filename from URL" + filename,e); |
137 | } |
138 | } |
139 | |
140 | /** |
141 | * Get an InputStream from a file, on the file system. This method does not |
142 | * use the XRadar URL resolver, and will fail with URL. |
143 | * |
144 | * @param filename |
145 | * @return |
146 | * @throws XRadarException |
147 | */ |
148 | public static InputStream readFile(String filename) throws XRadarException |
149 | { |
150 | try |
151 | { |
152 | return new FileInputStream(new File(filename)); |
153 | } |
154 | catch (FileNotFoundException e) |
155 | { |
156 | throw new XRadarException("Unable to read file : " + filename,e); |
157 | } |
158 | } |
159 | |
160 | /** |
161 | * <p>Utility method to create an InputStream, based on a filename.</p> |
162 | * @param filename |
163 | * @return |
164 | * @throws XRadarException |
165 | */ |
166 | public static InputStream fromFilenameToInputStream(String filename) throws XRadarException { |
167 | try |
168 | { |
169 | return new FileInputStream(filename); |
170 | } |
171 | catch (FileNotFoundException e) |
172 | { |
173 | new XRadarException("Can't convert file to stream",e); |
174 | } |
175 | return null; |
176 | } |
177 | |
178 | public static OutputStream createOutputStream(String filename) throws XRadarException |
179 | { |
180 | try { |
181 | return new FileOutputStream(new File(filename)); |
182 | } catch (FileNotFoundException e) { |
183 | throw new XRadarException("Can't create file " + filename,e); |
184 | } |
185 | } |
186 | |
187 | public static InputStream returnStream(InputStream stream,String filename) { |
188 | if ( stream.markSupported() ) { |
189 | stream.mark(1); |
190 | try { |
191 | if ( stream.read() == EOF ) |
192 | stream = StreamUtils.rebuildFileInputStream(stream, filename); |
193 | else |
194 | stream.reset(); |
195 | } |
196 | catch (IOException error ) { // this should never happens ! |
197 | stream = StreamUtils.rebuildFileInputStream(stream, filename); |
198 | } |
199 | } |
200 | else |
201 | { |
202 | try { |
203 | stream.available(); // throw an exception if stream invalid so ... |
204 | } |
205 | catch (IOException e) { // ... we created again the stream |
206 | stream = StreamUtils.rebuildFileInputStream(stream, filename); |
207 | } |
208 | } |
209 | return stream; |
210 | } |
211 | |
212 | /** |
213 | * <p>Rather weird method wrote in order to rebuild a stream that can't be |
214 | * "rewinded". Please do not use this method, it is a hack, doomed to |
215 | * disappear at some point.</p> |
216 | * @param stream |
217 | * @param filename |
218 | * @return |
219 | */ |
220 | public static InputStream rebuildFileInputStream(InputStream stream,String filename) { |
221 | try { |
222 | stream = new FileInputStream(filename); |
223 | } |
224 | catch (FileNotFoundException fileNotFoundException) { |
225 | // well, this should never happens... :) |
226 | stream = null; |
227 | } |
228 | return stream; |
229 | } |
230 | |
231 | public static void closeStream(InputStream stream) throws XRadarException { |
232 | if ( stream != null ) { |
233 | try { |
234 | stream.close(); |
235 | } catch (IOException e) { |
236 | new XRadarException(e); |
237 | } |
238 | } |
239 | } |
240 | } |