Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 71170200C3E for ; Tue, 21 Mar 2017 18:30:48 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 6F774160B81; Tue, 21 Mar 2017 17:30:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B6758160B6E for ; Tue, 21 Mar 2017 18:30:47 +0100 (CET) Received: (qmail 5904 invoked by uid 500); 21 Mar 2017 17:30:46 -0000 Mailing-List: contact dev-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list dev@hc.apache.org Received: (qmail 5893 invoked by uid 99); 21 Mar 2017 17:30:46 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Mar 2017 17:30:46 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 3225D18028D for ; Tue, 21 Mar 2017 17:30:46 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -98.848 X-Spam-Level: X-Spam-Status: No, score=-98.848 tagged_above=-999 required=6.31 tests=[KAM_NUMSUBJECT=0.5, RP_MATCHES_RCVD=-0.001, SPF_NEUTRAL=0.652, URIBL_BLOCKED=0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id KkfVeTOyTrfR for ; Tue, 21 Mar 2017 17:30:45 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id 5499860CF9 for ; Tue, 21 Mar 2017 17:30:44 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id AD8BFE0AE8 for ; Tue, 21 Mar 2017 17:30:42 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 52187254D3 for ; Tue, 21 Mar 2017 17:30:42 +0000 (UTC) Date: Tue, 21 Mar 2017 17:30:42 +0000 (UTC) From: "John Flavin (JIRA)" To: dev@hc.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Closed] (HTTPCORE-449) IdentityInputStream.available() erroneously returns 0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Tue, 21 Mar 2017 17:30:48 -0000 [ https://issues.apache.org/jira/browse/HTTPCORE-449?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] John Flavin closed HTTPCORE-449. -------------------------------- Reviewed commit here: https://github.com/apache/httpcore/commit/a5dbd14bc8ea64ff529c4651aaad9bd2d89b9f53 Looks perfect to me. {{IdentityInputStream#available}} checks if there is anything in the buffer, if so it returns the buffer length, if not it returns the underlying {{InputStream#available}}. That's exactly what I was asking for. There is one improvement you could make to the test {{testAvailableInStream}}. Rather than calling {{in.read(tmp)}} before {{in.available()}}, you could call {{in.available()}} first and assert that it gives a non-zero value even before you have called {{read}}. It's certainly not necessary to do, but that test change would assure me that the code is doing what it should. > IdentityInputStream.available() erroneously returns 0 > ----------------------------------------------------- > > Key: HTTPCORE-449 > URL: https://issues.apache.org/jira/browse/HTTPCORE-449 > Project: HttpComponents HttpCore > Issue Type: Improvement > Components: HttpCore > Affects Versions: 4.4.5, 4.4.6 > Reporter: John Flavin > Priority: Minor > Fix For: 5.0-alpha3 > > > h5. 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. > h5. 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. > h5. 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: > {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} > h5. 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}}. -- This message was sent by Atlassian JIRA (v6.3.15#6346) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org For additional commands, e-mail: dev-help@hc.apache.org