001    /**
002     *
003     * Copyright 2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.kernel.classloader;
018    
019    import java.io.BufferedOutputStream;
020    import java.io.ByteArrayOutputStream;
021    import java.io.File;
022    import java.io.FileInputStream;
023    import java.io.FileOutputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.InputStreamReader;
027    import java.io.OutputStream;
028    import java.io.Reader;
029    import java.io.Writer;
030    import java.io.FileNotFoundException;
031    import java.net.MalformedURLException;
032    import java.net.URL;
033    import java.net.URLStreamHandler;
034    import java.net.URLConnection;
035    import java.util.Collection;
036    import java.util.Collections;
037    import java.util.Enumeration;
038    import java.util.LinkedList;
039    import java.util.jar.JarFile;
040    import java.util.jar.JarOutputStream;
041    import java.util.jar.Manifest;
042    import java.util.zip.ZipEntry;
043    import java.util.zip.ZipFile;
044    
045    import org.apache.geronimo.kernel.classloader.ResourceLocation;
046    import org.apache.geronimo.kernel.classloader.JarResourceLocation;
047    import org.apache.geronimo.kernel.classloader.DirectoryResourceLocation;
048    
049    /**
050     * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
051     */
052    public final class IoUtil {
053        private IoUtil() {
054        }
055    
056        public static byte[] getBytes(InputStream inputStream) throws IOException {
057            try {
058                byte[] buffer = new byte[4096];
059                ByteArrayOutputStream out = new ByteArrayOutputStream();
060                for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) {
061                    out.write(buffer, 0, count);
062                }
063                byte[] bytes = out.toByteArray();
064                return bytes;
065            } finally {
066                close(inputStream);
067            }
068        }
069    
070        public static void flush(OutputStream thing) {
071            if (thing != null) {
072                try {
073                    thing.flush();
074                } catch(Exception ignored) {
075                }
076            }
077        }
078    
079        public static void flush(Writer thing) {
080            if (thing != null) {
081                try {
082                    thing.flush();
083                } catch(Exception ignored) {
084                }
085            }
086        }
087    
088        public static void close(JarFile thing) {
089            if (thing != null) {
090                try {
091                    thing.close();
092                } catch(Exception ignored) {
093                }
094            }
095        }
096    
097        public static void close(InputStream thing) {
098            if (thing != null) {
099                try {
100                    thing.close();
101                } catch(Exception ignored) {
102                }
103            }
104        }
105    
106        public static void close(OutputStream thing) {
107            if (thing != null) {
108                try {
109                    thing.close();
110                } catch(Exception ignored) {
111                }
112            }
113        }
114    
115        public static void close(Reader thing) {
116            if (thing != null) {
117                try {
118                    thing.close();
119                } catch(Exception ignored) {
120                }
121            }
122        }
123    
124        public static void close(Writer thing) {
125            if (thing != null) {
126                try {
127                    thing.close();
128                } catch(Exception ignored) {
129                }
130            }
131        }
132    
133        public static final class EmptyInputStream extends InputStream {
134            public int read() {
135                return -1;
136            }
137    
138            public int read(byte b[])  {
139                return -1;
140            }
141    
142            public int read(byte b[], int off, int len) {
143                return -1;
144            }
145    
146            public long skip(long n) {
147                return 0;
148            }
149    
150            public int available() {
151                return 0;
152            }
153    
154            public void close() {
155            }
156    
157            public synchronized void mark(int readlimit) {
158            }
159    
160            public synchronized void reset() {
161            }
162    
163            public boolean markSupported() {
164                return false;
165            }
166        }
167    }