private static int addToBuffer(Writer out,char[] buffer,int bufferIndex,int bufferLength,char[] toAdd) throws IOException {
  if (bufferIndex >= bufferLength || (toAdd.length + bufferIndex >= bufferLength)) {
    out.write(buffer,0,bufferIndex);
    bufferIndex=0;
  }
  System.arraycopy(toAdd,0,buffer,bufferIndex,toAdd.length);
  return bufferIndex + toAdd.length;
}

private static int addToBuffer ( Writer out , char [ ] buffer , int bufferIndex , int bufferLength , char [ ] toAdd ) throws IOException { if ( bufferIndex >= bufferLength || ( toAdd . length + bufferIndex >= bufferLength ) ) { out . write ( buffer , 0 , bufferIndex ) ; bufferIndex = 0 ; } System . arraycopy ( toAdd , 0 , buffer , bufferIndex , toAdd . length ) ; return bufferIndex + toAdd . length ; }
add an array of characters to the buffer , flushing the buffer if the buffer is full , and returning the new buffer index .
