EMMA Coverage Report (generated Wed Dec 16 17:19:59 CET 2009)
[all classes][org.sourceforge.xradar.util]

COVERAGE SUMMARY FOR SOURCE FILE [StreamUtils.java]

nameclass, %method, %block, %line, %
StreamUtils.java100% (1/1)31%  (4/13)11%  (28/257)10%  (7/67)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StreamUtils100% (1/1)31%  (4/13)11%  (28/257)10%  (7/67)
StreamUtils (): void 0%   (0/1)0%   (0/3)0%   (0/1)
copy (InputStream, OutputStream): void 0%   (0/1)0%   (0/20)0%   (0/7)
copyFileToFile (String, String): void 0%   (0/1)0%   (0/26)0%   (0/7)
copyStreamToStream (InputStream, OutputStream): void 0%   (0/1)0%   (0/20)0%   (0/6)
createFileWriter (String): Writer 0%   (0/1)0%   (0/27)0%   (0/6)
createOutputStream (String): OutputStream 0%   (0/1)0%   (0/22)0%   (0/3)
readFile (String): InputStream 0%   (0/1)0%   (0/22)0%   (0/3)
rebuildFileInputStream (InputStream, String): InputStream 0%   (0/1)0%   (0/11)0%   (0/5)
returnStream (InputStream, String): InputStream 0%   (0/1)0%   (0/35)0%   (0/13)
retrieveFileContentAsStream (String): InputStream 100% (1/1)24%  (9/37)20%  (1/5)
fromFilenameToInputStream (String): InputStream 100% (1/1)36%  (5/14)25%  (1/4)
closeStream (InputStream): void 100% (1/1)50%  (6/12)67%  (4/6)
createOuput (Writer): StreamResult 100% (1/1)100% (8/8)100% (1/1)

1/**
2 * BSD-style license; for more info see http://xradar.sourceforge.net/license.html
3 */
4package org.sourceforge.xradar.util;
5 
6import java.io.BufferedInputStream;
7import java.io.BufferedWriter;
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.FileNotFoundException;
11import java.io.FileOutputStream;
12import java.io.FileWriter;
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.OutputStream;
16import java.io.Writer;
17import java.net.MalformedURLException;
18import java.net.URL;
19 
20import javax.xml.transform.stream.StreamResult;
21 
22import 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
32public 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}

[all classes][org.sourceforge.xradar.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov