afplib icon indicating copy to clipboard operation
afplib copied to clipboard

AfpInputStream relies on the underlying Stream to always read the requested number of bytes

Open moritzfl opened this issue 3 years ago • 0 comments

In the implementation of AfpInputStream, various calls are made to super.read() - for example:

	public AfpInputStream(InputStream in) {
		super(in);
		try {
            super.read(header, 0, header.length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}

They rely on the input stream providing/reading the exact number of bytes requested. However, the Javadoc for InputStream says:

     * <p> If <code>len</code> is zero, then no bytes are read and
     * <code>0</code> is returned; otherwise, there is an attempt to read at
     * least one byte. If no byte is available because the stream is at end of
     * file, the value <code>-1</code> is returned; otherwise, at least one
     * byte is read and stored into <code>b</code>.
...
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the stream has been reached.

This means that any InputStream-Implementation can in choose to deliver less than the requested number of bytes, even if it has not reached the files end. When relying on a fixed number of bytes being read, this should be considered when calling super.read().

moritzfl avatar Feb 04 '22 06:02 moritzfl