1 34 35 36 package com.micronova.util; 37 38 import java.util.*; 39 import java.io.*; 40 41 42 43 public class IOUtil 44 { 45 46 47 public static final int DEFAULTBUFFERSIZE = 2048; 48 49 50 51 public static final void tryClose(InputStream x) 52 { 53 if (x != null) 54 { 55 try 56 { 57 x.close(); 58 } 59 catch (Exception e) 60 { 61 } 62 } 63 } 64 65 66 67 public static final void tryClose(OutputStream x) 68 { 69 if (x != null) 70 { 71 try 72 { 73 x.close(); 74 } 75 catch (Exception e) 76 { 77 } 78 } 79 } 80 81 82 83 public static final void tryClose(Reader x) 84 { 85 if (x != null) 86 { 87 try 88 { 89 x.close(); 90 } 91 catch (Exception e) 92 { 93 } 94 } 95 } 96 97 98 99 public static final void tryClose(Writer x) 100 { 101 if (x != null) 102 { 103 try 104 { 105 x.close(); 106 } 107 catch (Exception e) 108 { 109 } 110 } 111 } 112 113 114 115 public static final int copy(InputStream in, OutputStream out, byte[] buffer) throws Exception 116 { 117 int bytesCopied = 0; 118 119 int readSize = buffer.length; 120 121 for (;;) 122 { 123 int bytesRead = in.read(buffer, 0, readSize); 124 125 if (bytesRead <= 0) 126 { 127 break; 128 } 129 130 out.write(buffer, 0, bytesRead); 131 132 bytesCopied += bytesRead; 133 } 134 135 return bytesCopied; 136 } 137 138 139 140 public static final int copy(Reader in, Writer out, char[] buffer) throws Exception 141 { 142 int charsCopied = 0; 143 144 int readSize = buffer.length; 145 146 for (;;) 147 { 148 int charsRead = in.read(buffer, 0, readSize); 149 150 if (charsRead <= 0) 151 { 152 break; 153 } 154 155 out.write(buffer, 0, charsRead); 156 157 charsCopied += charsRead; 158 } 159 160 return charsCopied; 161 } 162 163 public static final int copy(InputStream in, OutputStream out) throws Exception 164 { 165 return copy(in, out, new byte[DEFAULTBUFFERSIZE]); 166 } 167 168 public static final int copy(Reader in, Writer out) throws Exception 169 { 170 return copy(in, out, new char[DEFAULTBUFFERSIZE]); 171 } 172 173 174 175 public static final byte[] readAll(InputStream in, byte[] buffer, int size) throws Exception 176 { 177 ByteArrayOutputStream bOut = new ByteArrayOutputStream(size); 178 179 copy(in, bOut, buffer); 180 181 return bOut.toByteArray(); 182 } 183 184 public static final byte[] readAll(InputStream in, int size) throws Exception 185 { 186 return readAll(in, new byte[DEFAULTBUFFERSIZE], size); 187 } 188 189 public static final byte[] readAll(InputStream in) throws Exception 190 { 191 return readAll(in, DEFAULTBUFFERSIZE); 192 } 193 194 195 196 public static final char[] readAll(Reader in, char[] buffer, int size) throws Exception 197 { 198 CharArrayWriter out = new CharArrayWriter(size); 199 200 copy(in, out, buffer); 201 202 return out.toCharArray(); 203 } 204 205 public static final char[] readAll(Reader in, int size) throws Exception 206 { 207 return readAll(in, new char[DEFAULTBUFFERSIZE], size); 208 } 209 210 public static final char[] readAll(Reader in) throws Exception 211 { 212 return readAll(in, DEFAULTBUFFERSIZE); 213 } 214 } 215 | Popular Tags |