private ByteBuffer ensureBuffer(int size,ByteBuffer buffer,WritableByteChannel output) throws IOException {
  if (buffer.remaining() >= size) {
    return buffer;
  }
  flushBuffer(buffer,output);
  if (buffer.remaining() >= size) {
    return buffer;
  }
  return ByteBuffer.allocateDirect(Math.max(buffer.capacity() << 1,buffer.capacity() + size));
}

private ByteBuffer ensureBuffer ( int size , ByteBuffer buffer , WritableByteChannel output ) throws IOException { if ( buffer . remaining ( ) >= size ) { return buffer ; } flushBuffer ( buffer , output ) ; if ( buffer . remaining ( ) >= size ) { return buffer ; } return ByteBuffer . allocateDirect ( Math . max ( buffer . capacity ( ) << 1 , buffer . capacity ( ) + size ) ) ; }
ensure the buffer is large enough .
