hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nicholas DiPiazza (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPCLIENT-1854) Cannot peek contents of org.apache.http.conn.EofSensorInputStream
Date Mon, 12 Jun 2017 15:22:00 GMT

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

Nicholas DiPiazza commented on HTTPCLIENT-1854:

I tried to call {{{read(scratch, totalRead, n)}}} in a loop but this does not work. 

Upon further inspection, I called {{{available()}}} it returns 439.
I fetch those 439 characters using {{{read(scratch, totalRead, n)}}}

I call {{{available()}}} a second time, it returns 0. 

> Cannot peek contents of org.apache.http.conn.EofSensorInputStream
> -----------------------------------------------------------------
>                 Key: HTTPCLIENT-1854
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1854
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>    Affects Versions: 4.5.2
>            Reporter: Nicholas DiPiazza
> I am trying to peek at an input stream contents from HttpClient, up to 64k bytes.
> The stream comes from an HttpGet, nothing unusual about it:
> {code}
> HttpGet requestGet = new HttpGet(encodedUrl);
> HttpResponse httpResponse = httpClient.execute(requestGet);
> int status = httpResponse.getStatusLine().getStatusCode();
> if (status == HttpStatus.SC_OK) {
>   return httpResponse.getEntity().getContent();
> }
> {code}
> The input stream it returns is of type org.apache.http.conn.EofSensorInputStream
> Our use-case is such that we need to "peek" at the first (up to 64k) bytes of the input
stream. I use an algorithm described here How do I peek at the first two bytes in an InputStream?
> {code}
> PushbackInputStream pis = new PushbackInputStream(inputStream, DEFAULT_PEEK_BUFFER_SIZE);
> byte [] peekBytes = new byte[DEFAULT_PEEK_BUFFER_SIZE];
> int read = pis.read(peekBytes);
>   byte[] trimmed = new byte[read];
>   System.arraycopy(peekBytes, 0, trimmed, 0, read);
>   peekBytes = trimmed;
> }
> pis.unread(peekBytes);
> {code}
> When I use a ByteArrayInputStream, this works with no problem.
> When using the org.apache.http.conn.EofSensorInputStream I only get a small number of
bytes at the beginning of the stream. usually around 400 bytes. When I expected up to 64k
> I also tried using a BufferedInputStream where I read up to the first 64k bytes then
call a .reset() but that doesn't work either. Same issue.
> Why might this be? I do not think anything is closing the stream because if you call
IOUtils.toString(inputStream) I do get all the content.
> See https://stackoverflow.com/questions/44474191/is-it-possible-to-peek-httpclients-org-apache-http-conn-eofsensorinputstream

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