public int read()

in src/com/pty4j/windows/conpty/WinHandleInputStream.java [42:83]


  public int read(byte @NotNull [] b, int off, int len) throws IOException {
    Objects.checkFromIndexSize(off, len, b.length);
    myLock.lock();
    try {
      myReadCount++;
      myReadCountChanged.signalAll();
    }
    finally {
      myLock.unlock();
    }
    if (len == 0) {
      return 0;
    }
    if (myClosed) {
      if (myClosedExplicitly) {
        throw new IOException("Closed stdin");
      }
      return -1;
    }
    byte[] buffer = new byte[len];
    IntByReference lpNumberOfBytesRead = new IntByReference(0);
    boolean result = Kernel32.INSTANCE.ReadFile(myReadPipe, buffer, buffer.length, lpNumberOfBytesRead, null);
    if (!result) {
      int lastError = Native.getLastError();
      if (lastError == WinError.ERROR_BROKEN_PIPE) {
        // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile
        // If an anonymous pipe is being used and the write handle has been closed,
        // when ReadFile attempts to read using the pipe's corresponding read handle,
        // the function returns FALSE and GetLastError returns ERROR_BROKEN_PIPE.
        return -1;
      }
      throw new LastErrorExceptionEx("ReadFile stdin", lastError);
    }
    int bytesRead = lpNumberOfBytesRead.getValue();
    if (bytesRead == 0) {
      // If lpOverlapped is NULL, then when a synchronous read operation reaches the end of a file,
      // ReadFile returns TRUE and sets *lpNumberOfBytesRead to zero.
      return -1;
    }
    System.arraycopy(buffer, 0, b, off, len);
    return bytesRead;
  }