jira-importer opened a new issue, #1421:
URL: https://github.com/apache/httpcomponents-core/issues/1421

   **[John 
Flavin](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=flavin)** 
opened 
**[HTTPCORE-449](https://issues.apache.org/jira/browse/HTTPCORE-449?redirect=false)**
 and commented
   
   ##### BACKGROUND
   
   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.
   
   ##### PROBLEM
   
   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.
   
   ##### INVESTIGATION INTO PROBLEM
   
   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:
   
   ```java
   @Override
   public int available() throws IOException {
       if (this.in instanceof BufferInfo) {
           return ((BufferInfo) this.in).length();
       } else {
           return 0;
       }
   }
   ```
   
   The `in` in the above code is a `SessionInputBufferImpl`, which has a few 
methods that are seemingly relevant to this:
   
   ```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();
       }
   ```
   
   ##### QUESTION
   
   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`.
   
   
   ---
   
   **Affects:** 4.4.5, 4.4.6
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to