private void writeChunks(OutputStream outputStream,byte[] bytes,int length) throws IOException {
synchronized (outputStream) {
    int chunksize=8 * 1024;
    for (int p=0; p < length; p+=chunksize) {
      int chunk=p + chunksize < length ? chunksize : length - p;
      outputStream.write(bytes,p,chunk);
    }
  }
  outputStream.flush();
}

private void writeChunks ( OutputStream outputStream , byte [ ] bytes , int length ) throws IOException { synchronized ( outputStream ) { int chunksize = 8 * 1024 ; for ( int p = 0 ; p < length ; p += chunksize ) { int chunk = p + chunksize < length ? chunksize : length - p ; outputStream . write ( bytes , p , chunk ) ; } } outputStream . flush ( ) ; }
a private function to write things out . this needs to be synchronized as writes can occur from multiple threads . we write in chunks to allow the other side to synchronize for large sized writes .
