1 16 package org.mortbay.util; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InterruptedIOException ; 22 import java.io.OutputStream ; 23 import java.io.Reader ; 24 import java.io.Writer ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 29 30 34 public class IO extends ThreadPool 35 { 36 private static Log log = LogFactory.getLog(IO.class); 37 38 39 public final static String 40 CRLF = "\015\012"; 41 42 43 public final static byte[] 44 CRLF_BYTES = {(byte)'\015',(byte)'\012'}; 45 46 47 public static int bufferSize = Integer.getInteger("org.mortbay.util.IO.bufferSize", 8192).intValue(); 48 49 50 private static class Singleton { 51 static final IO __instance=new IO(); 52 static 53 { 54 try{__instance.start();} 55 catch(Exception e){log.fatal(e); System.exit(1);} 56 } 57 } 58 59 public static IO instance() 60 { 61 return Singleton.__instance; 62 } 63 64 65 static class Job 66 { 67 InputStream in; 68 OutputStream out; 69 Reader read; 70 Writer write; 71 72 Job(InputStream in,OutputStream out) 73 { 74 this.in=in; 75 this.out=out; 76 this.read=null; 77 this.write=null; 78 } 79 Job(Reader read,Writer write) 80 { 81 this.in=null; 82 this.out=null; 83 this.read=read; 84 this.write=write; 85 } 86 } 87 88 89 92 public static void copyThread(InputStream in, OutputStream out) 93 { 94 try{ 95 instance().run(new Job(in,out)); 96 } 97 catch(InterruptedException e) 98 { 99 log.warn(LogSupport.EXCEPTION,e); 100 } 101 } 102 103 104 106 public static void copy(InputStream in, OutputStream out) 107 throws IOException 108 { 109 copy(in,out,-1); 110 } 111 112 113 116 public static void copyThread(Reader in, Writer out) 117 { 118 try 119 { 120 instance().run(new Job(in,out)); 121 } 122 catch(InterruptedException e) 123 { 124 log.warn(LogSupport.EXCEPTION,e); 125 } 126 } 127 128 129 132 public static void copy(Reader in, Writer out) 133 throws IOException 134 { 135 copy(in,out,-1); 136 } 137 138 139 142 public static void copy(InputStream in, 143 OutputStream out, 144 long byteCount) 145 throws IOException 146 { 147 byte buffer[] = new byte[bufferSize]; 148 int len=bufferSize; 149 150 if (byteCount>=0) 151 { 152 while (byteCount>0) 153 { 154 if (byteCount<bufferSize) 155 len=in.read(buffer,0,(int)byteCount); 156 else 157 len=in.read(buffer,0,bufferSize); 158 159 if (len==-1) 160 break; 161 162 byteCount -= len; 163 out.write(buffer,0,len); 164 } 165 } 166 else 167 { 168 while (true) 169 { 170 len=in.read(buffer,0,bufferSize); 171 if (len<0 ) 172 break; 173 out.write(buffer,0,len); 174 } 175 } 176 } 177 178 179 181 public static void copy(Reader in, 182 Writer out, 183 long byteCount) 184 throws IOException 185 { 186 char buffer[] = new char[bufferSize]; 187 int len=bufferSize; 188 189 if (byteCount>=0) 190 { 191 while (byteCount>0) 192 { 193 if (byteCount<bufferSize) 194 len=in.read(buffer,0,(int)byteCount); 195 else 196 len=in.read(buffer,0,bufferSize); 197 198 if (len==-1) 199 break; 200 201 byteCount -= len; 202 out.write(buffer,0,len); 203 } 204 } 205 else 206 { 207 while (true) 208 { 209 len=in.read(buffer,0,bufferSize); 210 if (len==-1) 211 break; 212 out.write(buffer,0,len); 213 } 214 } 215 } 216 217 218 220 public static String toString(InputStream in) 221 throws IOException 222 { 223 ByteArrayOutputStream out = new ByteArrayOutputStream (); 224 copy(in,out); 225 return new String (out.toByteArray()); 226 } 227 228 229 230 234 public static boolean delete(File file) 235 { 236 if (!file.exists()) 237 return false; 238 if (file.isDirectory()) 239 { 240 File [] files = file.listFiles(); 241 for (int i=0;files!=null && i<files.length;i++) 242 delete(files[i]); 243 } 244 return file.delete(); 245 } 246 247 248 249 251 public void handle(Object o) 252 { 253 Job job=(Job)o; 254 try { 255 if (job.in!=null) 256 copy(job.in,job.out,-1); 257 else 258 copy(job.read,job.write,-1); 259 } 260 catch(IOException e) 261 { 262 LogSupport.ignore(log,e); 263 try{ 264 if (job.out!=null) 265 job.out.close(); 266 if (job.write!=null) 267 job.write.close(); 268 } 269 catch(IOException e2) 270 { 271 LogSupport.ignore(log,e2); 272 } 273 } 274 } 275 276 277 280 public static OutputStream getNullStream() 281 { 282 return __nullStream; 283 } 284 285 290 public static void close(InputStream is) 291 { 292 try 293 { 294 if (is != null) 295 is.close(); 296 } 297 catch (IOException e) 298 { 299 LogSupport.ignore(log,e); 300 } 301 } 302 303 308 public static void close(OutputStream os) 309 { 310 try 311 { 312 if (os != null) 313 os.close(); 314 } 315 catch (IOException e) 316 { 317 LogSupport.ignore(log,e); 318 } 319 } 320 321 322 323 324 private static class NullOS extends OutputStream 325 { 326 public void close(){} 327 public void flush(){} 328 public void write(byte[]b){} 329 public void write(byte[]b,int i,int l){} 330 public void write(int b){} 331 } 332 private static NullOS __nullStream = new NullOS(); 333 334 335 338 public static Writer getNullWriter() 339 { 340 return __nullWriter; 341 } 342 343 344 345 private static class NullWrite extends Writer 346 { 347 public void close(){} 348 public void flush(){} 349 public void write(char[]b){} 350 public void write(char[]b,int o,int l){} 351 public void write(int b){} 352 public void write(String s){} 353 public void write(String s,int o,int l){} 354 } 355 private static NullWrite __nullWriter = new NullWrite(); 356 } 357 358 359 360 361 362 363 364 365 366 | Popular Tags |