KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > classloader > IoUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.classloader;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.util.jar.JarFile JavaDoc;
26
27 /**
28  * @version $Rev: 476457 $ $Date: 2006-11-18 01:49:20 -0500 (Sat, 18 Nov 2006) $
29  */

30 public final class IoUtil {
31     private IoUtil() {
32     }
33
34     public static byte[] getBytes(InputStream JavaDoc inputStream) throws IOException JavaDoc {
35         try {
36             byte[] buffer = new byte[4096];
37             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
38             for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) {
39                 out.write(buffer, 0, count);
40             }
41             byte[] bytes = out.toByteArray();
42             return bytes;
43         } finally {
44             close(inputStream);
45         }
46     }
47
48     public static void flush(OutputStream JavaDoc thing) {
49         if (thing != null) {
50             try {
51                 thing.flush();
52             } catch(Exception JavaDoc ignored) {
53             }
54         }
55     }
56
57     public static void flush(Writer JavaDoc thing) {
58         if (thing != null) {
59             try {
60                 thing.flush();
61             } catch(Exception JavaDoc ignored) {
62             }
63         }
64     }
65
66     public static void close(JarFile JavaDoc thing) {
67         if (thing != null) {
68             try {
69                 thing.close();
70             } catch(Exception JavaDoc ignored) {
71             }
72         }
73     }
74
75     public static void close(InputStream JavaDoc thing) {
76         if (thing != null) {
77             try {
78                 thing.close();
79             } catch(Exception JavaDoc ignored) {
80             }
81         }
82     }
83
84     public static void close(OutputStream JavaDoc thing) {
85         if (thing != null) {
86             try {
87                 thing.close();
88             } catch(Exception JavaDoc ignored) {
89             }
90         }
91     }
92
93     public static void close(Reader JavaDoc thing) {
94         if (thing != null) {
95             try {
96                 thing.close();
97             } catch(Exception JavaDoc ignored) {
98             }
99         }
100     }
101
102     public static void close(Writer JavaDoc thing) {
103         if (thing != null) {
104             try {
105                 thing.close();
106             } catch(Exception JavaDoc ignored) {
107             }
108         }
109     }
110
111     public static final class EmptyInputStream extends InputStream JavaDoc {
112         public int read() {
113             return -1;
114         }
115
116         public int read(byte b[]) {
117             return -1;
118         }
119
120         public int read(byte b[], int off, int len) {
121             return -1;
122         }
123
124         public long skip(long n) {
125             return 0;
126         }
127
128         public int available() {
129             return 0;
130         }
131
132         public void close() {
133         }
134
135         public synchronized void mark(int readlimit) {
136         }
137
138         public synchronized void reset() {
139         }
140
141         public boolean markSupported() {
142             return false;
143         }
144     }
145 }
146
Popular Tags