1 /** 2 * 3 * Copyright 2004 The Apache Software Foundation 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 * 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.BufferedOutputStream; 20 import java.io.ByteArrayOutputStream; 21 import java.io.File; 22 import java.io.FileInputStream; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import java.io.OutputStream; 28 import java.io.Reader; 29 import java.io.Writer; 30 import java.io.FileNotFoundException; 31 import java.net.MalformedURLException; 32 import java.net.URL; 33 import java.net.URLStreamHandler; 34 import java.net.URLConnection; 35 import java.util.Collection; 36 import java.util.Collections; 37 import java.util.Enumeration; 38 import java.util.LinkedList; 39 import java.util.jar.JarFile; 40 import java.util.jar.JarOutputStream; 41 import java.util.jar.Manifest; 42 import java.util.zip.ZipEntry; 43 import java.util.zip.ZipFile; 44 45 import org.apache.geronimo.kernel.classloader.ResourceLocation; 46 import org.apache.geronimo.kernel.classloader.JarResourceLocation; 47 import org.apache.geronimo.kernel.classloader.DirectoryResourceLocation; 48 49 /** 50 * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $ 51 */ 52 public final class IoUtil { 53 private IoUtil() { 54 } 55 56 public static byte[] getBytes(InputStream inputStream) throws IOException { 57 try { 58 byte[] buffer = new byte[4096]; 59 ByteArrayOutputStream out = new ByteArrayOutputStream(); 60 for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) { 61 out.write(buffer, 0, count); 62 } 63 byte[] bytes = out.toByteArray(); 64 return bytes; 65 } finally { 66 close(inputStream); 67 } 68 } 69 70 public static void flush(OutputStream thing) { 71 if (thing != null) { 72 try { 73 thing.flush(); 74 } catch(Exception ignored) { 75 } 76 } 77 } 78 79 public static void flush(Writer thing) { 80 if (thing != null) { 81 try { 82 thing.flush(); 83 } catch(Exception ignored) { 84 } 85 } 86 } 87 88 public static void close(JarFile thing) { 89 if (thing != null) { 90 try { 91 thing.close(); 92 } catch(Exception ignored) { 93 } 94 } 95 } 96 97 public static void close(InputStream thing) { 98 if (thing != null) { 99 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 }