2 // ========================================================================
3 // Copyright (c) 1995-2016 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.ByteBuffer;
33 import java.nio.channels.FileChannel;
34 import java.nio.channels.GatheringByteChannel;
35 import java.nio.charset.Charset;
37 import org.eclipse.jetty.util.log.Log;
38 import org.eclipse.jetty.util.log.Logger;
40 /* ======================================================================== */
42 * Provides stream handling utilities in
43 * singleton Threadpool implementation accessed by static members.
47 private static final Logger LOG = Log.getLogger(IO.class);
49 /* ------------------------------------------------------------------- */
50 public final static String
53 /* ------------------------------------------------------------------- */
54 public final static byte[]
55 CRLF_BYTES = {(byte)'\015',(byte)'\012'};
57 /* ------------------------------------------------------------------- */
58 public static final int bufferSize = 64*1024;
60 /* ------------------------------------------------------------------- */
61 static class Job implements Runnable
68 Job(InputStream in,OutputStream out)
75 Job(Reader read,Writer write)
83 /* ------------------------------------------------------------ */
85 * @see java.lang.Runnable#run()
104 catch(IOException e2)
112 /* ------------------------------------------------------------------- */
113 /** Copy Stream in to Stream out until EOF or exception.
115 public static void copy(InputStream in, OutputStream out)
121 /* ------------------------------------------------------------------- */
122 /** Copy Reader to Writer out until EOF or exception.
124 public static void copy(Reader in, Writer out)
130 /* ------------------------------------------------------------------- */
131 /** Copy Stream in to Stream for byteCount bytes or until EOF or exception.
133 public static void copy(InputStream in,
138 byte buffer[] = new byte[bufferSize];
145 int max = byteCount<bufferSize?(int)byteCount:bufferSize;
146 len=in.read(buffer,0,max);
152 out.write(buffer,0,len);
159 len=in.read(buffer,0,bufferSize);
162 out.write(buffer,0,len);
167 /* ------------------------------------------------------------------- */
168 /** Copy Reader to Writer for byteCount bytes or until EOF or exception.
170 public static void copy(Reader in,
175 char buffer[] = new char[bufferSize];
182 if (byteCount<bufferSize)
183 len=in.read(buffer,0,(int)byteCount);
185 len=in.read(buffer,0,bufferSize);
191 out.write(buffer,0,len);
194 else if (out instanceof PrintWriter)
196 PrintWriter pout=(PrintWriter)out;
197 while (!pout.checkError())
199 len=in.read(buffer,0,bufferSize);
202 out.write(buffer,0,len);
209 len=in.read(buffer,0,bufferSize);
212 out.write(buffer,0,len);
217 /* ------------------------------------------------------------ */
218 /** Copy files or directories
221 * @throws IOException
223 public static void copy(File from,File to) throws IOException
225 if (from.isDirectory())
231 /* ------------------------------------------------------------ */
232 public static void copyDir(File from,File to) throws IOException
236 if (!to.isDirectory())
237 throw new IllegalArgumentException(to.toString());
242 File[] files = from.listFiles();
245 for (int i=0;i<files.length;i++)
247 String name = files[i].getName();
248 if (".".equals(name) || "..".equals(name))
250 copy(files[i],new File(to,name));
255 /* ------------------------------------------------------------ */
256 public static void copyFile(File from,File to) throws IOException
258 try (InputStream in=new FileInputStream(from);
259 OutputStream out=new FileOutputStream(to))
265 /* ------------------------------------------------------------ */
266 /** Read input stream to string.
268 public static String toString(InputStream in)
271 return toString(in,(Charset)null);
274 /* ------------------------------------------------------------ */
275 /** Read input stream to string.
277 public static String toString(InputStream in,String encoding)
280 return toString(in, encoding==null?null:Charset.forName(encoding));
283 /** Read input stream to string.
285 public static String toString(InputStream in, Charset encoding)
288 StringWriter writer=new StringWriter();
289 InputStreamReader reader = encoding==null?new InputStreamReader(in):new InputStreamReader(in,encoding);
292 return writer.toString();
295 /* ------------------------------------------------------------ */
296 /** Read input stream to string.
298 public static String toString(Reader in)
301 StringWriter writer=new StringWriter();
303 return writer.toString();
307 /* ------------------------------------------------------------ */
309 * This delete will recursively delete directories - BE CAREFULL
310 * @param file The file to be deleted.
312 public static boolean delete(File file)
316 if (file.isDirectory())
318 File[] files = file.listFiles();
319 for (int i=0;files!=null && i<files.length;i++)
322 return file.delete();
325 /* ------------------------------------------------------------ */
327 * closes an input stream, and logs exceptions
329 * @param is the input stream to close
331 public static void close(InputStream is)
338 catch (IOException e)
345 * closes a reader, and logs exceptions
347 * @param reader the reader to close
349 public static void close(Reader reader)
355 } catch (IOException e)
362 * closes a writer, and logs exceptions
364 * @param writer the writer to close
366 public static void close(Writer writer)
373 catch (IOException e)
379 /* ------------------------------------------------------------ */
380 public static byte[] readBytes(InputStream in)
383 ByteArrayOutputStream bout = new ByteArrayOutputStream();
385 return bout.toByteArray();
388 /* ------------------------------------------------------------ */
390 * A gathering write utility wrapper.
391 * <p>This method wraps a gather write with a loop that handles the limitations of some operating systems that
392 * have a limit on the number of buffers written. The method loops on the write until either all the content
393 * is written or no progress is made.
394 * @param out The GatheringgByteChannel to write to
395 * @param buffers The buffers to write
396 * @param offset The offset into the buffers array
397 * @param length The length in buffers to write
398 * @return The total bytes written
399 * @throws IOException
401 public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) throws IOException
404 write: while (length>0)
406 // Write as much as we can
407 long wrote=out.write(buffers,offset,length);
409 // If we can't write any more, give up
416 // Look for unwritten content
417 for (int i=offset;i<buffers.length;i++)
419 if (buffers[i].hasRemaining())
421 // loop with new offset and length;
422 length=length-(i-offset);
434 /* ------------------------------------------------------------ */
436 * closes an output stream, and logs exceptions
438 * @param os the output stream to close
440 public static void close(OutputStream os)
447 catch (IOException e)
453 /* ------------------------------------------------------------ */
455 * @return An outputstream to nowhere
457 public static OutputStream getNullStream()
462 /* ------------------------------------------------------------ */
464 * @return An outputstream to nowhere
466 public static InputStream getClosedStream()
468 return __closedStream;
471 /* ------------------------------------------------------------ */
472 /* ------------------------------------------------------------ */
473 private static class NullOS extends OutputStream
476 public void close(){}
478 public void flush(){}
480 public void write(byte[]b){}
482 public void write(byte[]b,int i,int l){}
484 public void write(int b){}
486 private static NullOS __nullStream = new NullOS();
489 /* ------------------------------------------------------------ */
490 /* ------------------------------------------------------------ */
491 private static class ClosedIS extends InputStream
494 public int read() throws IOException
499 private static ClosedIS __closedStream = new ClosedIS();
501 /* ------------------------------------------------------------ */
503 * @return An writer to nowhere
505 public static Writer getNullWriter()
510 /* ------------------------------------------------------------ */
512 * @return An writer to nowhere
514 public static PrintWriter getNullPrintWriter()
516 return __nullPrintWriter;
519 /* ------------------------------------------------------------ */
520 /* ------------------------------------------------------------ */
521 private static class NullWrite extends Writer
524 public void close(){}
526 public void flush(){}
528 public void write(char[]b){}
530 public void write(char[]b,int o,int l){}
532 public void write(int b){}
534 public void write(String s){}
536 public void write(String s,int o,int l){}
538 private static NullWrite __nullWriter = new NullWrite();
539 private static PrintWriter __nullPrintWriter = new PrintWriter(__nullWriter);