hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "John Flavin (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPCORE-449) IdentityInputStream.available() erroneously returns 0
Date Tue, 28 Feb 2017 14:09:45 GMT

    [ https://issues.apache.org/jira/browse/HTTPCORE-449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15888073#comment-15888073

John Flavin commented on HTTPCORE-449:

Maybe "bug" is not the right word. But it seems to me that {{SessionInputBufferImpl#available}}
is supposed to tell me if there is data available *in the stream*, not in the buffer.

Compare to {{SessionInputBufferImpl#read(...)}} on [line 185|https://github.com/apache/httpcore/blob/4.4.x/httpcore/src/main/java/org/apache/http/impl/io/SessionInputBufferImpl.java#L185],
which first checks if the buffer has data (using {{SessionInputBufferImpl#hasBufferedData}}
method), but if the buffer is empty it reads data directly off the stream.

That's what I want {{SessionInputBufferImpl#available}} to do: sure, as a shortcut, first
check if data is buffered, but if not then tell me if data is available in the stream. That's
the contract of {{InputStream#available}}: does the stream have available data? The fact that
the stream data is buffered or not buffered is an implementation detail that need not be exposed
to consumers. 

> IdentityInputStream.available() erroneously returns 0
> -----------------------------------------------------
>                 Key: HTTPCORE-449
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-449
>             Project: HttpComponents HttpCore
>          Issue Type: Bug
>          Components: HttpCore
>    Affects Versions: 4.4.5, 4.4.6
>            Reporter: John Flavin
> I have an application that reads data from a stream. When data is available, I can read
from the stream perfectly fine. However, sometimes the stream will remain open with no data,
in which case reading from the stream blocks. I would like to use the {{available()}} method
on the stream to first check whether anything can be read before attempting to read.
> When I attempt to use the {{available()}} method on my {{InputStream}} instance it returns
{{0}}, i.e. no data available, even when I can successfully call {{read(...)}} and read data
off the stream. 
> I have traced the {{available()}} method on my {{InputStream}} instance through several
layers of wrapper classes around more {{InputStream}} instances. Each layer's {{available()}}
method simply calls into its wrapped {{InputStream}}'s {{available()}} method. That is, until
I reach the {{IdentityInputStream.available()}} method. The latter does this:
> {code:java}
>     @Override
>     public int available() throws IOException {
>         if (this.in instanceof BufferInfo) {
>             return ((BufferInfo) this.in).length();
>         } else {
>             return 0;
>         }
>     }
> {code}
> The {{in}} in the above code is a {{SessionInputBufferImpl}}, which has a few methods
that are seemingly relevant to this:
> {code:java}
>     @Override
>     public int capacity() {
>         return this.buffer.length;
>     }
>     @Override
>     public int length() {
>         return this.bufferlen - this.bufferpos;
>     }
>     @Override
>     public int available() {
>         return capacity() - length();
>     }
> {code}
> Why does {{IdentityInputStream.available()}} call {{((BufferInfo) this.in).length()}}
instead of {{((BufferInfo) this.in).available()}}? The former returns {{0}} when I call it
on a stream that has data which can be read, but has not been; the latter returns a number
greater than {{0}}.

This message was sent by Atlassian JIRA

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org

View raw message