private void callbackDone(
      final AIOCallback callback, final long sequence, final ByteBuffer buffer) {
    maxIOSemaphore.release();

    pendingWrites.countDown();

    callbackLock.lock();

    try {

      if (sequence == -1) {
        callback.done();
      } else {
        if (sequence == nextReadSequence) {
          nextReadSequence++;
          callback.done();
          flushCallbacks();
        } else {
          pendingCallbacks.add(new CallbackHolder(sequence, callback));
        }
      }

      // The buffer is not sent on callback for read operations
      if (bufferCallback != null && buffer != null) {
        bufferCallback.bufferDone(buffer);
      }
    } finally {
      callbackLock.unlock();
    }
  }
 public void writeInternal(long positionToWrite, long size, ByteBuffer bytes)
     throws HornetQException {
   try {
     writeInternal(handler, positionToWrite, size, bytes);
   } catch (HornetQException e) {
     fireExceptionListener(e.getType().getCode(), e.getMessage());
     throw e;
   }
   if (bufferCallback != null) {
     bufferCallback.bufferDone(bytes);
   }
 }
  // Called by the JNI layer.. just ignore the
  // warning
  private void callbackError(
      final AIOCallback callback,
      final long sequence,
      final ByteBuffer buffer,
      final int errorCode,
      final String errorMessage) {
    HornetQJournalLogger.LOGGER.callbackError(errorMessage);

    fireExceptionListener(errorCode, errorMessage);

    maxIOSemaphore.release();

    pendingWrites.countDown();

    callbackLock.lock();

    try {
      if (sequence == -1) {
        callback.onError(errorCode, errorMessage);
      } else {
        if (sequence == nextReadSequence) {
          nextReadSequence++;
          callback.onError(errorCode, errorMessage);
          flushCallbacks();
        } else {
          pendingCallbacks.add(new ErrorCallback(sequence, callback, errorCode, errorMessage));
        }
      }
    } finally {
      callbackLock.unlock();
    }

    // The buffer is not sent on callback for read operations
    if (bufferCallback != null && buffer != null) {
      bufferCallback.bufferDone(buffer);
    }
  }