KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > IOUtils


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.util;
56
57 import java.io.InputStream JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.OutputStream JavaDoc;
60 import java.io.Reader JavaDoc;
61 import java.io.Writer JavaDoc;
62 import java.net.ServerSocket JavaDoc;
63 import java.net.Socket JavaDoc;
64 import java.net.HttpURLConnection JavaDoc;
65
66 /**
67  *
68  * @author jbriggs
69  */

70 public final class IOUtils {
71   
72   private IOUtils() {
73   }
74   
75  /**
76   * close an input stream, but ensure any exceptions are 'swallowed' (but dumped as
77   * a stack trace)
78   */

79   public static final void close(InputStream JavaDoc is) {
80     if (is != null) {
81       try {
82         is.close();
83       }
84       catch (Exception JavaDoc e) {
85         e.printStackTrace();
86       }
87     }
88   }
89
90  /**
91   * close an output stream, but ensure any exceptions are 'swallowed' (but dumped as
92   * a stack trace)
93   */

94   public static final void close(OutputStream JavaDoc os) {
95     if (os != null) {
96       try {
97         os.close();
98       }
99       catch (Exception JavaDoc e) {
100         e.printStackTrace();
101       }
102     }
103   }
104   
105  /**
106   * close a reader, but ensure any exceptions are 'swallowed' (but dumped as
107   * a stack trace)
108   */

109   public static final void close(Reader JavaDoc reader) {
110     if (reader != null) {
111       try {
112         reader.close();
113       }
114       catch (Exception JavaDoc e) {
115         e.printStackTrace();
116       }
117     }
118   }
119   
120  /**
121   * close a server socket, but ensure any exceptions are 'swallowed' (but dumped as
122   * a stack trace)
123   */

124   public static final void close(ServerSocket JavaDoc socket) {
125     if (socket != null) {
126       try {
127         socket.close();
128       }
129       catch (Exception JavaDoc e) {
130         e.printStackTrace();
131       }
132     }
133   }
134   
135  /**
136   * close a socket, but ensure any exceptions are 'swallowed' (but dumped as
137   * a stack trace)
138   */

139   public static final void close(Socket JavaDoc socket) {
140     if (socket != null) {
141       try {
142         socket.close();
143       }
144       catch (Exception JavaDoc e) {
145         e.printStackTrace();
146       }
147     }
148   }
149   
150  /**
151   * close a writer, but ensure any exceptions are 'swallowed' (but dumped as
152   * a stack trace)
153   */

154   public static final void close(Writer JavaDoc writer) {
155     if (writer != null) {
156       try {
157         writer.close();
158       }
159       catch (Exception JavaDoc e) {
160         e.printStackTrace();
161       }
162     }
163   }
164
165  /**
166   * disconnect an http connection, ensuring exceptions are 'swallowed'
167   */

168   public static final void disconnect(HttpURLConnection JavaDoc conn) {
169     if (conn != null) {
170       try {
171         conn.disconnect();
172       }
173       catch (Exception JavaDoc e) {
174         e.printStackTrace();
175       }
176     }
177   }
178   
179   public static final void flush(Writer JavaDoc w) {
180     if (w != null) {
181       try {
182         w.flush();
183       }
184       catch (IOException JavaDoc e) {
185         e.printStackTrace();
186       }
187     }
188   }
189   
190   public static final void flush(OutputStream JavaDoc os) {
191     if (os != null) {
192       try {
193         os.flush();
194       }
195       catch (IOException JavaDoc e) {
196         e.printStackTrace();
197       }
198     }
199   }
200   
201   public static final byte[] read(InputStream JavaDoc is) throws IOException JavaDoc {
202     int size = 0;
203     
204     int offset = 0;
205     byte[] b = new byte[1024];
206     byte[] tmp;
207     int len;
208     while ((len = is.read(b, offset, 1024)) > 0) {
209       size += len;
210       offset += len;
211
212       if (len < 1024) {
213         break;
214       }
215
216       tmp = new byte[size + 1024];
217       System.arraycopy(b, 0, tmp, 0, size);
218       b = tmp;
219     }
220     
221     if (b.length != size) {
222       tmp = new byte[size];
223       System.arraycopy(b, 0, tmp, 0, size);
224       b = tmp;
225     }
226     
227     return b;
228   }
229 }
Popular Tags