Is it possible to view the HttpClient org.apache.http.conn.EofSensorInputStream?
I am trying to peer into the content of an input stream from HttpClient, up to 64k bytes.
The stream is coming from HttpGet, nothing unusual about that:
HttpGet requestGet = new HttpGet(encodedUrl);
HttpResponse httpResponse = httpClient.execute(requestGet);
int status = httpResponse.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
return httpResponse.getEntity().getContent();
}
The input stream it returns is of type org.apache.http.conn.EofSensorInputStream
Our use case is that we need to "look" into the first (up to 64k) bytes of the input stream. I am using the algorithm described here. How do I look into the first two bytes in an InputStream?
PushbackInputStream pis = new PushbackInputStream(inputStream, DEFAULT_PEEK_BUFFER_SIZE);
byte [] peekBytes = new byte[DEFAULT_PEEK_BUFFER_SIZE];
int read = pis.read(peekBytes);
if (read < DEFAULT_PEEK_BUFFER_SIZE) {
byte[] trimmed = new byte[read];
System.arraycopy(peekBytes, 0, trimmed, 0, read);
peekBytes = trimmed;
}
pis.unread(peekBytes);
When I use ByteArrayInputStream it works without issue.
Problem: When using, org.apache.http.conn.EofSensorInputStream
I get a small amount of bytes at the beginning of the stream. usually about 400 bytes. When I was expecting up to 64k bytes.
I also tried using BufferedInputStream
where I read up to the first 64k bytes then called .reset()
, but that doesn't work either. Same problem.
Why might this be? I don't think that something is closing the stream, because if you call IOUtils.toString(inputStream)
I get all the content.
source to share
See InputStream # read (byte [] b, int off, int len) contract:
Reads up to len bytes of data from the input stream into a byte array. An attempt is made to read as many as len bytes , but less can be read . The number of bytes actually read is returned as an integer
Instead of using this method, use IOUtils.read
one that reads until you get the number of bytes you requested in the loop.
source to share