KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > cpp > lib > IOHelper


1 /*====================================================================
2
3 ObjectWeb Util Preprocessor Package.
4 Copyright (C) 2003 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 --------------------------------------------------------------------
26 $Id: IOHelper.java,v 1.1 2004/02/05 20:29:57 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.cpp.lib;
30
31 import java.io.BufferedReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.io.PrintStream JavaDoc;
37 import java.io.Reader JavaDoc;
38
39 import org.objectweb.util.misc.api.ExceptionWrapper;
40
41 /**
42  * This provides some helper functions on Java IO streams.
43  *
44  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
45  * @version 0.1
46  */

47 public class IOHelper
48 {
49     // ==================================================================
50
//
51
// Internal state.
52
//
53
// ==================================================================
54

55     // ==================================================================
56
//
57
// Constructor.
58
//
59
// ==================================================================
60

61     // ==================================================================
62
//
63
// Internal methods.
64
//
65
// ==================================================================
66

67     // ==================================================================
68
//
69
// Static public methods.
70
//
71
// ==================================================================
72

73     /**
74      * Closes the InputStream.
75      * @param input InputStream to close.
76      */

77     public static void close(InputStream JavaDoc input) {
78         try {
79             input.close();
80         } catch(IOException JavaDoc exc) {
81             // Wraps any java.io.IOException exception.
82
throw new ExceptionWrapper(exc);
83         }
84     }
85
86     /**
87      * Closes the reader stream.
88      * @param input stream tp close.
89      */

90     public static void close(Reader JavaDoc input) {
91         try {
92             input.close();
93         } catch(IOException JavaDoc exc) {
94             // Wraps any java.io.IOException exception.
95
throw new ExceptionWrapper(exc);
96         }
97     }
98
99     /**
100      * Closes the OutputStream.
101      * @param output the OutputStream to close.
102      */

103     public static void close(OutputStream JavaDoc output) {
104         try {
105             output.close();
106         } catch(IOException JavaDoc exc) {
107             // Wraps any java.io.IOException exception.
108
throw new ExceptionWrapper(exc);
109         }
110     }
111
112     /**
113      * Dump the content of the input into the output.
114      * @param input source stream.
115      * @param output target stream.
116      */

117     public static void dump(Reader JavaDoc input, PrintStream JavaDoc output) {
118         // Creates a buffered input reader stream.
119
BufferedReader JavaDoc in = new BufferedReader JavaDoc(input);
120
121         // Displays the content of the buffered input stream.
122
try {
123             String JavaDoc line = null;
124             while( (line = in.readLine()) != null)
125                 output.println(line);
126         } catch(IOException JavaDoc exc) {
127             // Wraps any java.io.IOException exception.
128
throw new ExceptionWrapper(exc);
129         } finally {
130             // Always closes the input stream.
131
close(in);
132         }
133     }
134
135     /**
136      * Dump the content of the input into the output.
137      * @param input source stream.
138      * @param output target stream.
139      */

140     public static void dump(InputStream JavaDoc input, PrintStream JavaDoc output) {
141         dump(new InputStreamReader JavaDoc(input), output);
142     }
143 }
144
Popular Tags