public int write(ByteBuffer buf) throws IOException {
   if (buf == null) throw new NullPointerException();
   synchronized (writeLock) {
     synchronized (stateLock) {
       ensureOpen();
       if (!isConnected()) throw new NotYetConnectedException();
     }
     int n = 0;
     try {
       begin();
       if (!isOpen()) return 0;
       writerThread = NativeThread.current();
       do {
         n = IOUtil.write(fd, buf, -1, nd);
       } while ((n == IOStatus.INTERRUPTED) && isOpen());
       return IOStatus.normalize(n);
     } finally {
       writerThread = 0;
       end((n > 0) || (n == IOStatus.UNAVAILABLE));
       assert IOStatus.check(n);
     }
   }
 }
 public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
   if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
     throw new IndexOutOfBoundsException();
   synchronized (writeLock) {
     synchronized (stateLock) {
       ensureOpen();
       if (!isConnected()) throw new NotYetConnectedException();
     }
     long n = 0;
     try {
       begin();
       if (!isOpen()) return 0;
       writerThread = NativeThread.current();
       do {
         n = IOUtil.write(fd, srcs, offset, length, nd);
       } while ((n == IOStatus.INTERRUPTED) && isOpen());
       return IOStatus.normalize(n);
     } finally {
       writerThread = 0;
       end((n > 0) || (n == IOStatus.UNAVAILABLE));
       assert IOStatus.check(n);
     }
   }
 }