Example #1
0
  /** @inheritDoc */
  public int readBuf(int fd, byte b[], int offset, int length) throws IOException {
    byte[] buf = b;
    if (offset != 0) {
      buf = new byte[length];
    }
    int result =
        libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking

    if (result < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EWOULDBLOCK) {
        VMThread.getSystemEvents().waitForReadEvent(fd);
        result =
            libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking
      }
      LibCUtil.errCheckNeg(result);
    }

    if (result == 0) {
      // If remote side has shut down the connection gracefully, and all
      // data has been received, recv() will complete immediately with
      // zero bytes received.
      //
      // This is true for Win32/CE and Linux
      result = -1;
    }

    return result;
  }