Exemple #1
0
 /** @inheritDoc */
 public int available(int fd) throws IOException {
   int err = ioctl.ioctl(fd, Ioctl.FIONREAD, availableBuf.address().toUWord().toPrimitive());
   int result = availableBuf.getInt(0);
   LibCUtil.errCheckNeg(err);
   if (DEBUG) {
     System.err.println("available(" + fd + ") = " + result);
   }
   return result;
 }
Exemple #2
0
  private void set_blocking_flags(int fd, boolean is_blocking) throws IOException {
    if (tryFcntl) {
      int flags = libc.fcntl(fd, LibC.F_GETFL, 0);
      if (DEBUG) {
        System.out.println("set_blocking_flags: fcntl F_GETFL = " + flags);
      }

      if (flags >= 0) {
        if (is_blocking == true) {
          flags &= ~LibC.O_NONBLOCK;
        } else {
          flags |= LibC.O_NONBLOCK;
        }
        if (DEBUG) {
          System.out.println("set_blocking_flags: calling fcntl F_SETFL flags: " + flags);
        }
        int res = libc.fcntl(fd, LibC.F_SETFL, flags);
        if (res != -1) {
          return;
        }
      } else if (LibCUtil.errno() == LibC.EOPNOTSUPP) {
        tryFcntl = false; // once this fails, don't try again
      }
    }

    if (!tryFcntl) {
      if (DEBUG) {
        System.out.println("set_blocking_flags: calling ioctl FIONBIO = " + !is_blocking);
      }
      IntByReference setting =
          new IntByReference(is_blocking ? 0 : 1); /* if "is_blocking"==true, NBIO = FALSE */
      int res = ioctl.ioctl(fd, Ioctl.FIONBIO, setting);
      setting.free();
      if (DEBUG) {
        System.out.println("set_blocking_flags: ioctl returned: " + res);
      }
      if (res >= 0) {
        return;
      }
    }
    throw newError(fd, "set_blocking_flags");
  }