KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > IOUtil


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util;
37
38 import java.util.*;
39 import java.io.*;
40
41 /** IO-related utilities */
42
43 public class IOUtil
44 {
45     /** default buffer size used, if not given */
46
47     public static final int DEFAULTBUFFERSIZE = 2048;
48
49     /** tries to close an InputStream if not null */
50
51     public static final void tryClose(InputStream x)
52     {
53         if (x != null)
54         {
55             try
56             {
57                 x.close();
58             }
59             catch (Exception JavaDoc e)
60             {
61             }
62         }
63     }
64
65     /** tries to close an OutputStream if not null */
66
67     public static final void tryClose(OutputStream x)
68     {
69         if (x != null)
70         {
71             try
72             {
73                 x.close();
74             }
75             catch (Exception JavaDoc e)
76             {
77             }
78         }
79     }
80
81     /** tries to close a Reader if not null */
82
83     public static final void tryClose(Reader x)
84     {
85         if (x != null)
86         {
87             try
88             {
89                 x.close();
90             }
91             catch (Exception JavaDoc e)
92             {
93             }
94         }
95     }
96
97     /** tries to close a Writer if not null */
98
99     public static final void tryClose(Writer x)
100     {
101         if (x != null)
102         {
103             try
104             {
105                 x.close();
106             }
107             catch (Exception JavaDoc e)
108             {
109             }
110         }
111     }
112     
113     /** copies bytes from in to out using given buffer until EOF. Returns number of bytes copied. Throws Exception on IO errors */
114
115     public static final int copy(InputStream in, OutputStream out, byte[] buffer) throws Exception JavaDoc
116     {
117         int bytesCopied = 0;
118
119         int readSize = buffer.length;
120
121         for (;;)
122         {
123             int bytesRead = in.read(buffer, 0, readSize);
124
125             if (bytesRead <= 0)
126             {
127                 break;
128             }
129             
130             out.write(buffer, 0, bytesRead);
131
132             bytesCopied += bytesRead;
133         }
134
135         return bytesCopied;
136     }
137
138     /** Copies bytes from in to out using given buffer until EOF. Returns number of bytes copied. Throws Exception on IO errors */
139
140     public static final int copy(Reader in, Writer out, char[] buffer) throws Exception JavaDoc
141     {
142         int charsCopied = 0;
143
144         int readSize = buffer.length;
145
146         for (;;)
147         {
148             int charsRead = in.read(buffer, 0, readSize);
149
150             if (charsRead <= 0)
151             {
152                 break;
153             }
154             
155             out.write(buffer, 0, charsRead);
156
157             charsCopied += charsRead;
158         }
159
160         return charsCopied;
161     }
162
163     public static final int copy(InputStream in, OutputStream out) throws Exception JavaDoc
164     {
165         return copy(in, out, new byte[DEFAULTBUFFERSIZE]);
166     }
167
168     public static final int copy(Reader in, Writer out) throws Exception JavaDoc
169     {
170         return copy(in, out, new char[DEFAULTBUFFERSIZE]);
171     }
172
173     /** read the whole content of an inputstream into byte[] */
174
175     public static final byte[] readAll(InputStream in, byte[] buffer, int size) throws Exception JavaDoc
176     {
177         ByteArrayOutputStream bOut = new ByteArrayOutputStream(size);
178
179         copy(in, bOut, buffer);
180
181         return bOut.toByteArray();
182     }
183
184     public static final byte[] readAll(InputStream in, int size) throws Exception JavaDoc
185     {
186         return readAll(in, new byte[DEFAULTBUFFERSIZE], size);
187     }
188
189     public static final byte[] readAll(InputStream in) throws Exception JavaDoc
190     {
191         return readAll(in, DEFAULTBUFFERSIZE);
192     }
193
194     /** read the whole content of a Reader into String */
195
196     public static final char[] readAll(Reader in, char[] buffer, int size) throws Exception JavaDoc
197     {
198         CharArrayWriter out = new CharArrayWriter(size);
199
200         copy(in, out, buffer);
201
202         return out.toCharArray();
203     }
204
205     public static final char[] readAll(Reader in, int size) throws Exception JavaDoc
206     {
207         return readAll(in, new char[DEFAULTBUFFERSIZE], size);
208     }
209
210     public static final char[] readAll(Reader in) throws Exception JavaDoc
211     {
212         return readAll(in, DEFAULTBUFFERSIZE);
213     }
214 }
215
Popular Tags