public void read(
     final long position,
     final long size,
     final ByteBuffer directByteBuffer,
     final AIOCallback aioPackage)
     throws HornetQException {
   checkOpened();
   if (poller == null) {
     startPoller();
   }
   pendingWrites.countUp();
   maxIOSemaphore.acquireUninterruptibly();
   try {
     read(handler, position, size, directByteBuffer, aioPackage);
   } catch (HornetQException e) {
     // Release only if an exception happened
     maxIOSemaphore.release();
     pendingWrites.countDown();
     throw e;
   } catch (RuntimeException e) {
     // Release only if an exception happened
     maxIOSemaphore.release();
     pendingWrites.countDown();
     throw e;
   }
 }
  public void write(
      final long position,
      final long size,
      final ByteBuffer directByteBuffer,
      final AIOCallback aioCallback) {
    if (aioCallback == null) {
      throw new NullPointerException("Null Callback");
    }

    checkOpened();
    if (poller == null) {
      startPoller();
    }

    pendingWrites.countUp();

    if (writeExecutor != null) {
      maxIOSemaphore.acquireUninterruptibly();

      writeExecutor.execute(
          new Runnable() {
            public void run() {
              long sequence = nextWritingSequence.getAndIncrement();

              try {
                write(handler, sequence, position, size, directByteBuffer, aioCallback);
              } catch (HornetQException e) {
                callbackError(
                    aioCallback, sequence, directByteBuffer, e.getType().getCode(), e.getMessage());
              } catch (RuntimeException e) {
                callbackError(
                    aioCallback,
                    sequence,
                    directByteBuffer,
                    HornetQExceptionType.INTERNAL_ERROR.getCode(),
                    e.getMessage());
              }
            }
          });
    } else {
      maxIOSemaphore.acquireUninterruptibly();

      long sequence = nextWritingSequence.getAndIncrement();

      try {
        write(handler, sequence, position, size, directByteBuffer, aioCallback);
      } catch (HornetQException e) {
        callbackError(
            aioCallback, sequence, directByteBuffer, e.getType().getCode(), e.getMessage());
      } catch (RuntimeException e) {
        callbackError(
            aioCallback,
            sequence,
            directByteBuffer,
            HornetQExceptionType.INTERNAL_ERROR.getCode(),
            e.getMessage());
      }
    }
  }