KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > IO


1 // ========================================================================
2
// $Id: IO.java,v 1.13 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InterruptedIOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28
29 /* ======================================================================== */
30 /** IO Utilities.
31  * Provides stream handling utilities in
32  * singleton Threadpool implementation accessed by static members.
33  */

34 public class IO extends ThreadPool
35 {
36     private static Log log = LogFactory.getLog(IO.class);
37
38     /* ------------------------------------------------------------------- */
39     public final static String JavaDoc
40         CRLF = "\015\012";
41
42     /* ------------------------------------------------------------------- */
43     public final static byte[]
44         CRLF_BYTES = {(byte)'\015',(byte)'\012'};
45
46     /* ------------------------------------------------------------------- */
47     public static int bufferSize = Integer.getInteger("org.mortbay.util.IO.bufferSize", 8192).intValue();
48     
49     /* ------------------------------------------------------------------- */
50     private static class Singleton {
51         static final IO __instance=new IO();
52         static
53         {
54             try{__instance.start();}
55             catch(Exception JavaDoc e){log.fatal(e); System.exit(1);}
56         }
57     }
58     
59     public static IO instance()
60     {
61         return Singleton.__instance;
62     }
63     
64     /* ------------------------------------------------------------------- */
65     static class Job
66     {
67         InputStream JavaDoc in;
68         OutputStream JavaDoc out;
69         Reader JavaDoc read;
70         Writer JavaDoc write;
71
72         Job(InputStream JavaDoc in,OutputStream JavaDoc out)
73         {
74             this.in=in;
75             this.out=out;
76             this.read=null;
77             this.write=null;
78         }
79         Job(Reader JavaDoc read,Writer JavaDoc write)
80         {
81             this.in=null;
82             this.out=null;
83             this.read=read;
84             this.write=write;
85         }
86     }
87     
88     /* ------------------------------------------------------------------- */
89     /** Copy Stream in to Stream out until EOF or exception.
90      * in own thread
91      */

92     public static void copyThread(InputStream JavaDoc in, OutputStream JavaDoc out)
93     {
94         try{
95             instance().run(new Job(in,out));
96         }
97         catch(InterruptedException JavaDoc e)
98         {
99             log.warn(LogSupport.EXCEPTION,e);
100         }
101     }
102     
103     /* ------------------------------------------------------------------- */
104     /** Copy Stream in to Stream out until EOF or exception.
105      */

106     public static void copy(InputStream JavaDoc in, OutputStream JavaDoc out)
107          throws IOException JavaDoc
108     {
109         copy(in,out,-1);
110     }
111     
112     /* ------------------------------------------------------------------- */
113     /** Copy Stream in to Stream out until EOF or exception
114      * in own thread
115      */

116     public static void copyThread(Reader JavaDoc in, Writer JavaDoc out)
117     {
118         try
119         {
120             instance().run(new Job(in,out));
121         }
122         catch(InterruptedException JavaDoc e)
123         {
124             log.warn(LogSupport.EXCEPTION,e);
125         }
126     }
127     
128     /* ------------------------------------------------------------------- */
129     /** Copy Reader to Writer out until EOF or exception.
130      * @return TODO
131      */

132     public static void copy(Reader JavaDoc in, Writer JavaDoc out)
133          throws IOException JavaDoc
134     {
135         copy(in,out,-1);
136     }
137     
138     /* ------------------------------------------------------------------- */
139     /** Copy Stream in to Stream for byteCount bytes or until EOF or exception.
140      * @return Copied bytes count or -1 if no bytes were read *and* EOF was reached
141      */

142     public static void copy(InputStream JavaDoc in,
143                             OutputStream JavaDoc out,
144                             long byteCount)
145          throws IOException JavaDoc
146     {
147         byte buffer[] = new byte[bufferSize];
148         int len=bufferSize;
149         
150         if (byteCount>=0)
151         {
152             while (byteCount>0)
153             {
154                 if (byteCount<bufferSize)
155                     len=in.read(buffer,0,(int)byteCount);
156                 else
157                     len=in.read(buffer,0,bufferSize);
158                 
159                 if (len==-1)
160                     break;
161                 
162                 byteCount -= len;
163                 out.write(buffer,0,len);
164             }
165         }
166         else
167         {
168             while (true)
169             {
170                 len=in.read(buffer,0,bufferSize);
171                 if (len<0 )
172                     break;
173                 out.write(buffer,0,len);
174             }
175         }
176     }
177
178     /* ------------------------------------------------------------------- */
179     /** Copy Reader to Writer for byteCount bytes or until EOF or exception.
180      */

181     public static void copy(Reader JavaDoc in,
182                             Writer JavaDoc out,
183                             long byteCount)
184          throws IOException JavaDoc
185     {
186         char buffer[] = new char[bufferSize];
187         int len=bufferSize;
188         
189         if (byteCount>=0)
190         {
191             while (byteCount>0)
192             {
193                 if (byteCount<bufferSize)
194                     len=in.read(buffer,0,(int)byteCount);
195                 else
196                     len=in.read(buffer,0,bufferSize);
197                 
198                 if (len==-1)
199                     break;
200                 
201                 byteCount -= len;
202                 out.write(buffer,0,len);
203             }
204         }
205         else
206         {
207             while (true)
208             {
209                 len=in.read(buffer,0,bufferSize);
210                 if (len==-1)
211                     break;
212                 out.write(buffer,0,len);
213             }
214         }
215     }
216
217     /* ------------------------------------------------------------ */
218     /** Read input stream to string.
219      */

220     public static String JavaDoc toString(InputStream JavaDoc in)
221         throws IOException JavaDoc
222     {
223         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
224         copy(in,out);
225         return new String JavaDoc(out.toByteArray());
226     }
227
228
229     /* ------------------------------------------------------------ */
230     /** Delete File.
231      * This delete will recursively delete directories - BE CAREFULL
232      * @param file The file to be deleted.
233      */

234     public static boolean delete(File JavaDoc file)
235     {
236         if (!file.exists())
237             return false;
238         if (file.isDirectory())
239         {
240             File JavaDoc[] files = file.listFiles();
241             for (int i=0;files!=null && i<files.length;i++)
242                 delete(files[i]);
243         }
244         return file.delete();
245     }
246     
247     
248     /* ------------------------------------------------------------ */
249     /** Run copy for copyThread()
250      */

251     public void handle(Object JavaDoc o)
252     {
253         Job job=(Job)o;
254         try {
255             if (job.in!=null)
256                 copy(job.in,job.out,-1);
257             else
258                 copy(job.read,job.write,-1);
259         }
260         catch(IOException JavaDoc e)
261         {
262             LogSupport.ignore(log,e);
263             try{
264                 if (job.out!=null)
265                     job.out.close();
266                 if (job.write!=null)
267                     job.write.close();
268             }
269             catch(IOException JavaDoc e2)
270             {
271                 LogSupport.ignore(log,e2);
272             }
273         }
274     }
275
276     /* ------------------------------------------------------------ */
277     /**
278      * @return An outputstream to nowhere
279      */

280     public static OutputStream JavaDoc getNullStream()
281     {
282         return __nullStream;
283     }
284
285     /**
286      * closes an input stream, and logs exceptions
287      *
288      * @param is the input stream to close
289      */

290     public static void close(InputStream JavaDoc is)
291     {
292         try
293         {
294             if (is != null)
295                 is.close();
296         }
297         catch (IOException JavaDoc e)
298         {
299             LogSupport.ignore(log,e);
300         }
301     }
302
303     /**
304      * closes an output stream, and logs exceptions
305      *
306      * @param os the output stream to close
307      */

308     public static void close(OutputStream JavaDoc os)
309     {
310         try
311         {
312             if (os != null)
313                 os.close();
314         }
315         catch (IOException JavaDoc e)
316         {
317             LogSupport.ignore(log,e);
318         }
319     }
320
321     
322     /* ------------------------------------------------------------ */
323     /* ------------------------------------------------------------ */
324     private static class NullOS extends OutputStream JavaDoc
325     {
326         public void close(){}
327         public void flush(){}
328         public void write(byte[]b){}
329         public void write(byte[]b,int i,int l){}
330         public void write(int b){}
331     }
332     private static NullOS __nullStream = new NullOS();
333     
334     /* ------------------------------------------------------------ */
335     /**
336      * @return An writer to nowhere
337      */

338     public static Writer JavaDoc getNullWriter()
339     {
340         return __nullWriter;
341     }
342     
343     /* ------------------------------------------------------------ */
344     /* ------------------------------------------------------------ */
345     private static class NullWrite extends Writer JavaDoc
346     {
347         public void close(){}
348         public void flush(){}
349         public void write(char[]b){}
350         public void write(char[]b,int o,int l){}
351         public void write(int b){}
352         public void write(String JavaDoc s){}
353         public void write(String JavaDoc s,int o,int l){}
354     }
355     private static NullWrite __nullWriter = new NullWrite();
356 }
357
358
359
360
361
362
363
364
365
366
Popular Tags