comparison src/org/eclipse/jetty/util/IO.java @ 862:2bb375e94f64

simplify jetty.util.IO
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 02 Oct 2016 05:17:11 -0600
parents 8e9db0bbf4f9
children
comparison
equal deleted inserted replaced
861:cdab48877cfe 862:2bb375e94f64
31 import java.io.StringWriter; 31 import java.io.StringWriter;
32 import java.io.Writer; 32 import java.io.Writer;
33 33
34 import org.slf4j.Logger; 34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory; 35 import org.slf4j.LoggerFactory;
36 import org.eclipse.jetty.util.thread.QueuedThreadPool;
37 36
38 /* ======================================================================== */ 37 /* ======================================================================== */
39 /** IO Utilities. 38 /** IO Utilities.
40 * Provides stream handling utilities in 39 * Provides stream handling utilities in
41 * singleton Threadpool implementation accessed by static members. 40 * singleton Threadpool implementation accessed by static members.
42 */ 41 */
43 public class IO 42 public class IO
44 { 43 {
45 private static final Logger LOG = LoggerFactory.getLogger(IO.class); 44 private static final Logger LOG = LoggerFactory.getLogger(IO.class);
46 45
47 /* ------------------------------------------------------------------- */ 46 /* ------------------------------------------------------------------- */
48 public final static String 47 private static int bufferSize = 64*1024;
49 CRLF = "\015\012"; 48
50 49 /* ------------------------------------------------------------------- */
51 /* ------------------------------------------------------------------- */ 50 /** Copy Stream in to Stream out until EOF or exception.
52 public final static byte[] 51 */
53 CRLF_BYTES = {(byte)'\015',(byte)'\012'}; 52 public static void copy(InputStream in, OutputStream out)
54 53 throws IOException
55 /* ------------------------------------------------------------------- */ 54 {
56 public static int bufferSize = 64*1024; 55 copy(in,out,-1);
57 56 }
58 /* ------------------------------------------------------------------- */ 57
59 // TODO get rid of this singleton! 58 /* ------------------------------------------------------------------- */
60 private static class Singleton { 59 /** Copy Reader to Writer out until EOF or exception.
61 static final QueuedThreadPool __pool=new QueuedThreadPool(); 60 */
62 static 61 public static void copy(Reader in, Writer out)
63 { 62 throws IOException
64 try{__pool.start();} 63 {
65 catch(Exception e){LOG.warn("",e); System.exit(1);} 64 copy(in,out,-1);
66 } 65 }
67 } 66
68 67 /* ------------------------------------------------------------------- */
69 /* ------------------------------------------------------------------- */ 68 /** Copy Stream in to Stream for byteCount bytes or until EOF or exception.
70 static class Job implements Runnable 69 */
71 { 70 public static void copy(InputStream in,
72 InputStream in; 71 OutputStream out,
73 OutputStream out; 72 long byteCount)
74 Reader read; 73 throws IOException
75 Writer write; 74 {
76 75 byte buffer[] = new byte[bufferSize];
77 Job(InputStream in,OutputStream out) 76 int len=bufferSize;
78 { 77
79 this.in=in; 78 if (byteCount>=0)
80 this.out=out; 79 {
81 this.read=null; 80 while (byteCount>0)
82 this.write=null; 81 {
83 } 82 int max = byteCount<bufferSize?(int)byteCount:bufferSize;
84 Job(Reader read,Writer write) 83 len=in.read(buffer,0,max);
85 { 84
86 this.in=null; 85 if (len==-1)
87 this.out=null; 86 break;
88 this.read=read; 87
89 this.write=write; 88 byteCount -= len;
90 } 89 out.write(buffer,0,len);
91 90 }
92 /* ------------------------------------------------------------ */ 91 }
93 /* 92 else
94 * @see java.lang.Runnable#run() 93 {
95 */ 94 while (true)
96 public void run() 95 {
97 { 96 len=in.read(buffer,0,bufferSize);
98 try { 97 if (len<0 )
99 if (in!=null) 98 break;
100 copy(in,out,-1); 99 out.write(buffer,0,len);
101 else 100 }
102 copy(read,write,-1); 101 }
103 } 102 }
104 catch(IOException e) 103
105 { 104 /* ------------------------------------------------------------------- */
106 LOG.trace("",e); 105 /** Copy Reader to Writer for byteCount bytes or until EOF or exception.
107 try{ 106 */
108 if (out!=null) 107 public static void copy(Reader in,
109 out.close(); 108 Writer out,
110 if (write!=null) 109 long byteCount)
111 write.close(); 110 throws IOException
112 } 111 {
113 catch(IOException e2) 112 char buffer[] = new char[bufferSize];
114 { 113 int len=bufferSize;
115 LOG.trace("",e2); 114
116 } 115 if (byteCount>=0)
117 } 116 {
118 } 117 while (byteCount>0)
119 } 118 {
120 119 if (byteCount<bufferSize)
121 /* ------------------------------------------------------------------- */ 120 len=in.read(buffer,0,(int)byteCount);
122 /** Copy Stream in to Stream out until EOF or exception. 121 else
123 * in own thread 122 len=in.read(buffer,0,bufferSize);
124 */ 123
125 public static void copyThread(InputStream in, OutputStream out) 124 if (len==-1)
126 { 125 break;
127 try{ 126
128 Job job=new Job(in,out); 127 byteCount -= len;
129 if (!Singleton.__pool.dispatch(job)) 128 out.write(buffer,0,len);
130 job.run(); 129 }
131 } 130 }
132 catch(Exception e) 131 else if (out instanceof PrintWriter)
133 { 132 {
134 LOG.warn("",e); 133 PrintWriter pout=(PrintWriter)out;
135 } 134 while (!pout.checkError())
136 } 135 {
137 136 len=in.read(buffer,0,bufferSize);
138 /* ------------------------------------------------------------------- */ 137 if (len==-1)
139 /** Copy Stream in to Stream out until EOF or exception. 138 break;
140 */ 139 out.write(buffer,0,len);
141 public static void copy(InputStream in, OutputStream out) 140 }
142 throws IOException 141 }
143 { 142 else
144 copy(in,out,-1); 143 {
145 } 144 while (true)
146 145 {
147 /* ------------------------------------------------------------------- */ 146 len=in.read(buffer,0,bufferSize);
148 /** Copy Stream in to Stream out until EOF or exception 147 if (len==-1)
149 * in own thread 148 break;
150 */ 149 out.write(buffer,0,len);
151 public static void copyThread(Reader in, Writer out) 150 }
152 { 151 }
153 try 152 }
154 { 153
155 Job job=new Job(in,out); 154 /* ------------------------------------------------------------ */
156 if (!Singleton.__pool.dispatch(job)) 155 /** Copy files or directories
157 job.run(); 156 * @param from
158 } 157 * @param to
159 catch(Exception e) 158 * @throws IOException
160 { 159 */
161 LOG.warn("",e); 160 public static void copy(File from,File to) throws IOException
162 } 161 {
163 } 162 if (from.isDirectory())
164 163 copyDir(from,to);
165 /* ------------------------------------------------------------------- */ 164 else
166 /** Copy Reader to Writer out until EOF or exception. 165 copyFile(from,to);
167 */ 166 }
168 public static void copy(Reader in, Writer out) 167
169 throws IOException 168 /* ------------------------------------------------------------ */
170 { 169 public static void copyDir(File from,File to) throws IOException
171 copy(in,out,-1); 170 {
172 } 171 if (to.exists())
173 172 {
174 /* ------------------------------------------------------------------- */ 173 if (!to.isDirectory())
175 /** Copy Stream in to Stream for byteCount bytes or until EOF or exception. 174 throw new IllegalArgumentException(to.toString());
176 */ 175 }
177 public static void copy(InputStream in, 176 else
178 OutputStream out, 177 to.mkdirs();
179 long byteCount) 178
180 throws IOException 179 File[] files = from.listFiles();
181 { 180 if (files!=null)
182 byte buffer[] = new byte[bufferSize]; 181 {
183 int len=bufferSize; 182 for (int i=0;i<files.length;i++)
184 183 {
185 if (byteCount>=0) 184 String name = files[i].getName();
186 { 185 if (".".equals(name) || "..".equals(name))
187 while (byteCount>0) 186 continue;
188 { 187 copy(files[i],new File(to,name));
189 int max = byteCount<bufferSize?(int)byteCount:bufferSize; 188 }
190 len=in.read(buffer,0,max); 189 }
191 190 }
192 if (len==-1) 191
193 break; 192 /* ------------------------------------------------------------ */
194 193 public static void copyFile(File from,File to) throws IOException
195 byteCount -= len; 194 {
196 out.write(buffer,0,len); 195 FileInputStream in=new FileInputStream(from);
197 } 196 FileOutputStream out=new FileOutputStream(to);
198 } 197 copy(in,out);
199 else 198 in.close();
200 { 199 out.close();
201 while (true) 200 }
202 { 201
203 len=in.read(buffer,0,bufferSize); 202
204 if (len<0 ) 203 /* ------------------------------------------------------------ */
205 break; 204 /**
206 out.write(buffer,0,len); 205 * closes any {@link Closeable}
207 } 206 *
208 } 207 * @param c the closeable to close
209 } 208 */
210 209 public static void close(Closeable c)
211 /* ------------------------------------------------------------------- */ 210 {
212 /** Copy Reader to Writer for byteCount bytes or until EOF or exception. 211 try
213 */ 212 {
214 public static void copy(Reader in, 213 if (c != null)
215 Writer out, 214 c.close();
216 long byteCount) 215 }
217 throws IOException 216 catch (IOException e)
218 { 217 {
219 char buffer[] = new char[bufferSize]; 218 LOG.trace("",e);
220 int len=bufferSize; 219 }
221 220 }
222 if (byteCount>=0) 221
223 { 222 /**
224 while (byteCount>0) 223 * closes an input stream, and logs exceptions
225 { 224 *
226 if (byteCount<bufferSize) 225 * @param is the input stream to close
227 len=in.read(buffer,0,(int)byteCount); 226 */
228 else 227 public static void close(InputStream is)
229 len=in.read(buffer,0,bufferSize); 228 {
230 229 try
231 if (len==-1) 230 {
232 break; 231 if (is != null)
233 232 is.close();
234 byteCount -= len; 233 }
235 out.write(buffer,0,len); 234 catch (IOException e)
236 } 235 {
237 } 236 LOG.trace("",e);
238 else if (out instanceof PrintWriter) 237 }
239 { 238 }
240 PrintWriter pout=(PrintWriter)out; 239
241 while (!pout.checkError()) 240 /**
242 { 241 * closes a reader, and logs exceptions
243 len=in.read(buffer,0,bufferSize); 242 *
244 if (len==-1) 243 * @param reader the reader to close
245 break; 244 */
246 out.write(buffer,0,len); 245 public static void close(Reader reader)
247 } 246 {
248 } 247 try
249 else 248 {
250 { 249 if (reader != null)
251 while (true) 250 reader.close();
252 { 251 } catch (IOException e)
253 len=in.read(buffer,0,bufferSize); 252 {
254 if (len==-1) 253 LOG.trace("",e);
255 break; 254 }
256 out.write(buffer,0,len); 255 }
257 } 256
258 } 257 /**
259 } 258 * closes a writer, and logs exceptions
260 259 *
261 /* ------------------------------------------------------------ */ 260 * @param writer the writer to close
262 /** Copy files or directories 261 */
263 * @param from 262 public static void close(Writer writer)
264 * @param to 263 {
265 * @throws IOException 264 try
266 */ 265 {
267 public static void copy(File from,File to) throws IOException 266 if (writer != null)
268 { 267 writer.close();
269 if (from.isDirectory()) 268 } catch (IOException e)
270 copyDir(from,to); 269 {
271 else 270 LOG.trace("",e);
272 copyFile(from,to); 271 }
273 } 272 }
274 273
275 /* ------------------------------------------------------------ */ 274 /* ------------------------------------------------------------ */
276 public static void copyDir(File from,File to) throws IOException 275 /**
277 { 276 * closes an output stream, and logs exceptions
278 if (to.exists()) 277 *
279 { 278 * @param os the output stream to close
280 if (!to.isDirectory()) 279 */
281 throw new IllegalArgumentException(to.toString()); 280 public static void close(OutputStream os)
282 } 281 {
283 else 282 try
284 to.mkdirs(); 283 {
285 284 if (os != null)
286 File[] files = from.listFiles(); 285 os.close();
287 if (files!=null) 286 }
288 { 287 catch (IOException e)
289 for (int i=0;i<files.length;i++) 288 {
290 { 289 LOG.trace("",e);
291 String name = files[i].getName(); 290 }
292 if (".".equals(name) || "..".equals(name)) 291 }
293 continue; 292
294 copy(files[i],new File(to,name)); 293 /* ------------------------------------------------------------ */
295 } 294 /**
296 } 295 * @return An outputstream to nowhere
297 } 296 */
298 297 public static InputStream getClosedStream()
299 /* ------------------------------------------------------------ */ 298 {
300 public static void copyFile(File from,File to) throws IOException 299 return __closedStream;
301 { 300 }
302 FileInputStream in=new FileInputStream(from); 301
303 FileOutputStream out=new FileOutputStream(to); 302 /* ------------------------------------------------------------ */
304 copy(in,out); 303 /* ------------------------------------------------------------ */
305 in.close(); 304 private static class ClosedIS extends InputStream
306 out.close(); 305 {
307 } 306 @Override
308 307 public int read() throws IOException
309 /* ------------------------------------------------------------ */ 308 {
310 /** Read input stream to string. 309 return -1;
311 */ 310 }
312 public static String toString(InputStream in) 311 }
313 throws IOException 312 private static ClosedIS __closedStream = new ClosedIS();
314 {
315 return toString(in,null);
316 }
317
318 /* ------------------------------------------------------------ */
319 /** Read input stream to string.
320 */
321 public static String toString(InputStream in,String encoding)
322 throws IOException
323 {
324 StringWriter writer=new StringWriter();
325 InputStreamReader reader = encoding==null?new InputStreamReader(in):new InputStreamReader(in,encoding);
326
327 copy(reader,writer);
328 return writer.toString();
329 }
330
331 /* ------------------------------------------------------------ */
332 /** Read input stream to string.
333 */
334 public static String toString(Reader in)
335 throws IOException
336 {
337 StringWriter writer=new StringWriter();
338 copy(in,writer);
339 return writer.toString();
340 }
341
342
343 /* ------------------------------------------------------------ */
344 /** Delete File.
345 * This delete will recursively delete directories - BE CAREFULL
346 * @param file The file to be deleted.
347 */
348 public static boolean delete(File file)
349 {
350 if (!file.exists())
351 return false;
352 if (file.isDirectory())
353 {
354 File[] files = file.listFiles();
355 for (int i=0;files!=null && i<files.length;i++)
356 delete(files[i]);
357 }
358 return file.delete();
359 }
360
361 /* ------------------------------------------------------------ */
362 /**
363 * closes any {@link Closeable}
364 *
365 * @param c the closeable to close
366 */
367 public static void close(Closeable c)
368 {
369 try
370 {
371 if (c != null)
372 c.close();
373 }
374 catch (IOException e)
375 {
376 LOG.trace("",e);
377 }
378 }
379
380 /**
381 * closes an input stream, and logs exceptions
382 *
383 * @param is the input stream to close
384 */
385 public static void close(InputStream is)
386 {
387 try
388 {
389 if (is != null)
390 is.close();
391 }
392 catch (IOException e)
393 {
394 LOG.trace("",e);
395 }
396 }
397
398 /**
399 * closes a reader, and logs exceptions
400 *
401 * @param reader the reader to close
402 */
403 public static void close(Reader reader)
404 {
405 try
406 {
407 if (reader != null)
408 reader.close();
409 } catch (IOException e)
410 {
411 LOG.trace("",e);
412 }
413 }
414
415 /**
416 * closes a writer, and logs exceptions
417 *
418 * @param writer the writer to close
419 */
420 public static void close(Writer writer)
421 {
422 try
423 {
424 if (writer != null)
425 writer.close();
426 } catch (IOException e)
427 {
428 LOG.trace("",e);
429 }
430 }
431
432 /* ------------------------------------------------------------ */
433 public static byte[] readBytes(InputStream in)
434 throws IOException
435 {
436 ByteArrayOutputStream bout = new ByteArrayOutputStream();
437 copy(in,bout);
438 return bout.toByteArray();
439 }
440
441 /* ------------------------------------------------------------ */
442 /**
443 * closes an output stream, and logs exceptions
444 *
445 * @param os the output stream to close
446 */
447 public static void close(OutputStream os)
448 {
449 try
450 {
451 if (os != null)
452 os.close();
453 }
454 catch (IOException e)
455 {
456 LOG.trace("",e);
457 }
458 }
459
460 /* ------------------------------------------------------------ */
461 /**
462 * @return An outputstream to nowhere
463 */
464 public static OutputStream getNullStream()
465 {
466 return __nullStream;
467 }
468
469 /* ------------------------------------------------------------ */
470 /**
471 * @return An outputstream to nowhere
472 */
473 public static InputStream getClosedStream()
474 {
475 return __closedStream;
476 }
477
478 /* ------------------------------------------------------------ */
479 /* ------------------------------------------------------------ */
480 private static class NullOS extends OutputStream
481 {
482 @Override
483 public void close(){}
484 @Override
485 public void flush(){}
486 @Override
487 public void write(byte[]b){}
488 @Override
489 public void write(byte[]b,int i,int l){}
490 @Override
491 public void write(int b){}
492 }
493 private static NullOS __nullStream = new NullOS();
494
495
496 /* ------------------------------------------------------------ */
497 /* ------------------------------------------------------------ */
498 private static class ClosedIS extends InputStream
499 {
500 @Override
501 public int read() throws IOException
502 {
503 return -1;
504 }
505 }
506 private static ClosedIS __closedStream = new ClosedIS();
507
508 /* ------------------------------------------------------------ */
509 /**
510 * @return An writer to nowhere
511 */
512 public static Writer getNullWriter()
513 {
514 return __nullWriter;
515 }
516
517 /* ------------------------------------------------------------ */
518 /**
519 * @return An writer to nowhere
520 */
521 public static PrintWriter getNullPrintWriter()
522 {
523 return __nullPrintWriter;
524 }
525
526 /* ------------------------------------------------------------ */
527 /* ------------------------------------------------------------ */
528 private static class NullWrite extends Writer
529 {
530 @Override
531 public void close(){}
532 @Override
533 public void flush(){}
534 @Override
535 public void write(char[]b){}
536 @Override
537 public void write(char[]b,int o,int l){}
538 @Override
539 public void write(int b){}
540 @Override
541 public void write(String s){}
542 @Override
543 public void write(String s,int o,int l){}
544 }
545 private static NullWrite __nullWriter = new NullWrite();
546 private static PrintWriter __nullPrintWriter = new PrintWriter(__nullWriter);
547 } 313 }
548
549
550
551
552
553
554
555
556