private void expandBuf( int idelta)
throws HdfException
{
  //if (hdfFile.bugs >= 10) {
  //  prtf("expandBuf: idelta: %d  bbuf: pos: %d  limit: %d  capacity: %d",
  //    idelta, getPos(), bbuf.limit(), bbuf.capacity());
  //}

  if (getPos() + idelta > bbuf.capacity()) {
    if (outChannel == null) {
      // Expand bbuf
      int newLen = 100 + 2 * (getPos() + idelta);
      if (hdfFile.bugs >= 10) {
        prtf("expandBuf: expand A: getPos: %d  idelta: %d  newLen: %d",
          getPos(), idelta, newLen);
      }
      ByteBuffer newbuf = ByteBuffer.allocateDirect( newLen);
      newbuf.order( ByteOrder.LITTLE_ENDIAN);

      byte[] oldVals = new byte[ getPos()];
      bbuf.flip();
      bbuf.get( oldVals);

      newbuf.put( oldVals);
      bbuf = newbuf;
    }
    else {        // else we have outChannel: write it
      // Write bbuf to outChannel
      if (hdfFile.bugs >= 10) {
        prtf("expandBuf: write: getPos: %d  idelta: %d  compressionLevel: %d",
          getPos(), idelta, compressionLevel);
      }

      try {
        if (compressionLevel > 0) writeCompressedOutput();
        else {
          bbuf.flip();
          outChannel.write( bbuf);
        }
      }
      catch( IOException exc) {
        exc.printStackTrace();
        throwerr("caught: %s", exc);
      }

      bbuf.clear();
      // If idelta > bbuf.capacity, we still need to reallocate
      if (idelta > bbuf.capacity()) {
        // Expand bbuf
        int newLen = 100 + 2 * idelta;
        if (hdfFile.bugs >= 10) {
          prtf("expandBuf: expand with outChannel: idelta: %d  newLen: %d",
            idelta, newLen);
        }
        bbuf = ByteBuffer.allocateDirect( newLen);
        bbuf.order( ByteOrder.LITTLE_ENDIAN);
      }
    }
  }
} // end expandBuf