1 17 package org.apache.geronimo.kernel.classloader; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.Reader ; 24 import java.io.Writer ; 25 import java.util.jar.JarFile ; 26 27 30 public final class IoUtil { 31 private IoUtil() { 32 } 33 34 public static byte[] getBytes(InputStream inputStream) throws IOException { 35 try { 36 byte[] buffer = new byte[4096]; 37 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 thing) { 49 if (thing != null) { 50 try { 51 thing.flush(); 52 } catch(Exception ignored) { 53 } 54 } 55 } 56 57 public static void flush(Writer thing) { 58 if (thing != null) { 59 try { 60 thing.flush(); 61 } catch(Exception ignored) { 62 } 63 } 64 } 65 66 public static void close(JarFile thing) { 67 if (thing != null) { 68 try { 69 thing.close(); 70 } catch(Exception ignored) { 71 } 72 } 73 } 74 75 public static void close(InputStream thing) { 76 if (thing != null) { 77 try { 78 thing.close(); 79 } catch(Exception ignored) { 80 } 81 } 82 } 83 84 public static void close(OutputStream thing) { 85 if (thing != null) { 86 try { 87 thing.close(); 88 } catch(Exception ignored) { 89 } 90 } 91 } 92 93 public static void close(Reader thing) { 94 if (thing != null) { 95 try { 96 thing.close(); 97 } catch(Exception ignored) { 98 } 99 } 100 } 101 102 public static void close(Writer thing) { 103 if (thing != null) { 104 try { 105 thing.close(); 106 } catch(Exception ignored) { 107 } 108 } 109 } 110 111 public static final class EmptyInputStream extends InputStream { 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 |