2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
20 import java.io.ByteArrayOutputStream;
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.PrintWriter;
29 import java.io.Reader;
30 import java.io.StringWriter;
31 import java.io.Writer;
32 import java.nio.charset.Charset;
34 import org.eclipse.jetty.util.log.Log;
35 import org.eclipse.jetty.util.log.Logger;
37 /* ======================================================================== */
39 * Provides stream handling utilities in
40 * singleton Threadpool implementation accessed by static members.
44 private static final Logger LOG = Log.getLogger(IO.class);
46 /* ------------------------------------------------------------------- */
47 public final static String
50 /* ------------------------------------------------------------------- */
51 public final static byte[]
52 CRLF_BYTES = {(byte)'\015',(byte)'\012'};
54 /* ------------------------------------------------------------------- */
55 public static final int bufferSize = 64*1024;
57 /* ------------------------------------------------------------------- */
58 static class Job implements Runnable
65 Job(InputStream in,OutputStream out)
72 Job(Reader read,Writer write)
80 /* ------------------------------------------------------------ */
82 * @see java.lang.Runnable#run()
101 catch(IOException e2)
109 /* ------------------------------------------------------------------- */
110 /** Copy Stream in to Stream out until EOF or exception.
112 public static void copy(InputStream in, OutputStream out)
118 /* ------------------------------------------------------------------- */
119 /** Copy Reader to Writer out until EOF or exception.
121 public static void copy(Reader in, Writer out)
127 /* ------------------------------------------------------------------- */
128 /** Copy Stream in to Stream for byteCount bytes or until EOF or exception.
130 public static void copy(InputStream in,
135 byte buffer[] = new byte[bufferSize];
142 int max = byteCount<bufferSize?(int)byteCount:bufferSize;
143 len=in.read(buffer,0,max);
149 out.write(buffer,0,len);
156 len=in.read(buffer,0,bufferSize);
159 out.write(buffer,0,len);
164 /* ------------------------------------------------------------------- */
165 /** Copy Reader to Writer for byteCount bytes or until EOF or exception.
167 public static void copy(Reader in,
172 char buffer[] = new char[bufferSize];
179 if (byteCount<bufferSize)
180 len=in.read(buffer,0,(int)byteCount);
182 len=in.read(buffer,0,bufferSize);
188 out.write(buffer,0,len);
191 else if (out instanceof PrintWriter)
193 PrintWriter pout=(PrintWriter)out;
194 while (!pout.checkError())
196 len=in.read(buffer,0,bufferSize);
199 out.write(buffer,0,len);
206 len=in.read(buffer,0,bufferSize);
209 out.write(buffer,0,len);
214 /* ------------------------------------------------------------ */
215 /** Copy files or directories
218 * @throws IOException
220 public static void copy(File from,File to) throws IOException
222 if (from.isDirectory())
228 /* ------------------------------------------------------------ */
229 public static void copyDir(File from,File to) throws IOException
233 if (!to.isDirectory())
234 throw new IllegalArgumentException(to.toString());
239 File[] files = from.listFiles();
242 for (int i=0;i<files.length;i++)
244 String name = files[i].getName();
245 if (".".equals(name) || "..".equals(name))
247 copy(files[i],new File(to,name));
252 /* ------------------------------------------------------------ */
253 public static void copyFile(File from,File to) throws IOException
255 try (InputStream in=new FileInputStream(from);
256 OutputStream out=new FileOutputStream(to))
262 /* ------------------------------------------------------------ */
263 /** Read input stream to string.
265 public static String toString(InputStream in)
268 return toString(in,(Charset)null);
271 /* ------------------------------------------------------------ */
272 /** Read input stream to string.
274 public static String toString(InputStream in,String encoding)
277 return toString(in, encoding==null?null:Charset.forName(encoding));
280 /** Read input stream to string.
282 public static String toString(InputStream in, Charset encoding)
285 StringWriter writer=new StringWriter();
286 InputStreamReader reader = encoding==null?new InputStreamReader(in):new InputStreamReader(in,encoding);
289 return writer.toString();
292 /* ------------------------------------------------------------ */
293 /** Read input stream to string.
295 public static String toString(Reader in)
298 StringWriter writer=new StringWriter();
300 return writer.toString();
304 /* ------------------------------------------------------------ */
306 * This delete will recursively delete directories - BE CAREFULL
307 * @param file The file to be deleted.
309 public static boolean delete(File file)
313 if (file.isDirectory())
315 File[] files = file.listFiles();
316 for (int i=0;files!=null && i<files.length;i++)
319 return file.delete();
322 /* ------------------------------------------------------------ */
324 * closes an input stream, and logs exceptions
326 * @param is the input stream to close
328 public static void close(InputStream is)
335 catch (IOException e)
342 * closes a reader, and logs exceptions
344 * @param reader the reader to close
346 public static void close(Reader reader)
352 } catch (IOException e)
359 * closes a writer, and logs exceptions
361 * @param writer the writer to close
363 public static void close(Writer writer)
369 } catch (IOException e)
375 /* ------------------------------------------------------------ */
376 public static byte[] readBytes(InputStream in)
379 ByteArrayOutputStream bout = new ByteArrayOutputStream();
381 return bout.toByteArray();
384 /* ------------------------------------------------------------ */
386 * closes an output stream, and logs exceptions
388 * @param os the output stream to close
390 public static void close(OutputStream os)
397 catch (IOException e)
403 /* ------------------------------------------------------------ */
405 * @return An outputstream to nowhere
407 public static OutputStream getNullStream()
412 /* ------------------------------------------------------------ */
414 * @return An outputstream to nowhere
416 public static InputStream getClosedStream()
418 return __closedStream;
421 /* ------------------------------------------------------------ */
422 /* ------------------------------------------------------------ */
423 private static class NullOS extends OutputStream
426 public void close(){}
428 public void flush(){}
430 public void write(byte[]b){}
432 public void write(byte[]b,int i,int l){}
434 public void write(int b){}
436 private static NullOS __nullStream = new NullOS();
439 /* ------------------------------------------------------------ */
440 /* ------------------------------------------------------------ */
441 private static class ClosedIS extends InputStream
444 public int read() throws IOException
449 private static ClosedIS __closedStream = new ClosedIS();
451 /* ------------------------------------------------------------ */
453 * @return An writer to nowhere
455 public static Writer getNullWriter()
460 /* ------------------------------------------------------------ */
462 * @return An writer to nowhere
464 public static PrintWriter getNullPrintWriter()
466 return __nullPrintWriter;
469 /* ------------------------------------------------------------ */
470 /* ------------------------------------------------------------ */
471 private static class NullWrite extends Writer
474 public void close(){}
476 public void flush(){}
478 public void write(char[]b){}
480 public void write(char[]b,int o,int l){}
482 public void write(int b){}
484 public void write(String s){}
486 public void write(String s,int o,int l){}
488 private static NullWrite __nullWriter = new NullWrite();
489 private static PrintWriter __nullPrintWriter = new PrintWriter(__nullWriter);