Skip to content

WinPTYInputStream.read does not implement the Inputstream Interface correctly #176

@psu-core

Description

@psu-core

Hi, first thank you all for this open source lib.

The problem: The method WinPTYInputStream.read returns all kind of negative values and even worse it can return -1 without the stream having reached the end. This is not meeting the InputStream specification stated below:

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end
of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an
exception is thrown.
from https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/io/InputStream.html#read()

Proposed Fix:

// proposed fix

  @Override
  public int read() throws IOException {
    byte b[] = new byte[1];
    if (1 != read(b, 0, 1)) {
      return -1;
    }
    return Byte.toUnsignedInt(b[0]); // the original code returns the byte directly
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions