Feb 01

The Java InputStream and OutputStream class are synchronized to be thread safe. If you are using them in a single thread application, or know that they will only be called by a single thread then this is an unnecessary overhead.

To remove the synchronization you can copy the source code from the Sun classes, rename the class, and remove the synchronization from all methods. You’ll then have an unsynchronized version of the stream class to use in your application.

written by objects \\ tags: , , ,

Oct 19

You need to read all bytes from source stream to the destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.


public static void copy(InputStream in, OutputStream out
   int bufferSize)
   throws IOException
{
   // Read bytes and write to destination until eof

   byte[] buf = new byte[bufferSize];
   int len = 0;
   while ((len = in.read(buf)) >= 0)
   {
      out.write(buf, 0, len);
   }
}

written by objects \\ tags: , , ,