KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > IOUtil


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Grzegorz Lukasik
5  * Copyright (C) 2006 John Lewis
6  *
7  * Note: This file is dual licensed under the GPL and the Apache
8  * Source License (so that it can be used from both the main
9  * Cobertura classes and the ant tasks).
10  *
11  * Cobertura is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published
13  * by the Free Software Foundation; either version 2 of the License,
14  * or (at your option) any later version.
15  *
16  * Cobertura is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Cobertura; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24  * USA
25  */

26
27 package net.sourceforge.cobertura.util;
28
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 /**
38  * Helper class with useful I/O operations.
39  *
40  * @author Grzegorz Lukasik
41  */

42 public abstract class IOUtil
43 {
44
45     /**
46      * Copies bytes from input stream into the output stream. Stops
47      * when the input stream read method returns -1. Does not close
48      * the streams.
49      *
50      * @throws IOException If either passed stream will throw IOException.
51      * @throws NullPointerException If either passed stream is null.
52      */

53     public static void copyStream(InputStream JavaDoc in, OutputStream JavaDoc out)
54             throws IOException JavaDoc
55     {
56         // NullPointerException is explicity thrown to guarantee expected behaviour
57
if (in == null || out == null)
58             throw new NullPointerException JavaDoc();
59
60         int el;
61         byte[] buffer = new byte[1 << 15];
62         while ((el = in.read(buffer)) != -1)
63         {
64             out.write(buffer, 0, el);
65         }
66     }
67
68     /**
69      * Returns an array that contains values read from the
70      * given input stream.
71      *
72      * @throws NullPointerException If null stream is passed.
73      */

74     public static byte[] createByteArrayFromInputStream(InputStream JavaDoc in)
75             throws IOException JavaDoc
76     {
77         ByteArrayOutputStream JavaDoc byteArray = new ByteArrayOutputStream JavaDoc();
78         copyStream(in, byteArray);
79         return byteArray.toByteArray();
80     }
81
82     /**
83      * Moves a file from one location to other.
84      *
85      * @throws IOException If IO exception occur during moving.
86      * @throws NullPointerException If either passed file is null.
87      */

88     public static void moveFile(File JavaDoc sourceFile, File JavaDoc destinationFile)
89             throws IOException JavaDoc
90     {
91         if (destinationFile.exists())
92         {
93             destinationFile.delete();
94         }
95
96         // Move file using File method if possible
97
boolean succesfulMove = sourceFile.renameTo(destinationFile);
98         if (succesfulMove)
99             return;
100
101         // Copy file from source to destination
102
InputStream JavaDoc in = null;
103         OutputStream JavaDoc out = null;
104         try
105         {
106             in = new FileInputStream JavaDoc(sourceFile);
107             out = new FileOutputStream JavaDoc(destinationFile);
108             copyStream(in, out);
109         }
110         finally
111         {
112             in = closeInputStream(in);
113             out = closeOutputStream(out);
114         }
115
116         // Remove source file
117
sourceFile.delete();
118     }
119
120     /**
121      * Closes an input stream.
122      *
123      * @param in The stream to close.
124      * @return null unless an exception was thrown while closing, else
125      * returns the stream
126      */

127     public static InputStream JavaDoc closeInputStream(InputStream JavaDoc in)
128     {
129         if (in != null)
130         {
131             try
132             {
133                 in.close();
134                 in = null;
135             }
136             catch (IOException JavaDoc e)
137             {
138                 System.err.println("Cobertura: Error closing input stream.");
139                 e.printStackTrace();
140             }
141         }
142         return in;
143     }
144
145     /**
146      * Closes an output stream.
147      *
148      * @param out The stream to close.
149      * @return null unless an exception was thrown while closing, else
150      * returns the stream.
151      */

152     public static OutputStream JavaDoc closeOutputStream(OutputStream JavaDoc out)
153     {
154         if (out != null)
155         {
156             try
157             {
158                 out.close();
159                 out = null;
160             }
161             catch (IOException JavaDoc e)
162             {
163                 System.err.println("Cobertura: Error closing output stream.");
164                 e.printStackTrace();
165             }
166         }
167         return out;
168     }
169
170 }
171
Popular Tags