简体   繁体   English

是否可以窥视HttpClient的org.apache.http.conn.EofSensorInputStream?

[英]Is it possible to peek HttpClient's org.apache.http.conn.EofSensorInputStream?

I am trying to peek at an input stream contents from HttpClient, up to 64k bytes. 我试图从HttpClient查看输入流内容,最多64k字节。

The stream comes from an HttpGet, nothing unusual about it: 该流来自HttpGet,没有什么不寻常之处:

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 它返回的输入流的类型为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. 我们的用例是这样的,我们需要“窥视”输入流的第一个(最多64k)字节。 I use an algorithm described here How do I peek at the first two bytes in an InputStream? 我使用此处描述的算法如何查看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 a ByteArrayInputStream, this works with no problem. 当我使用ByteArrayInputStream时,这没有问题。

The Issue: When using the org.apache.http.conn.EofSensorInputStream I only get a small number of bytes at the beginning of the stream. 问题:当使用org.apache.http.conn.EofSensorInputStream我只在流的开头获得少量字节。 usually around 400 bytes. 通常大约400字节。 When I expected up to 64k bytes. 当我预计高达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. 我也尝试使用BufferedInputStream ,我读取前64k字节,然后调用.reset()但这也不起作用。 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. 我认为没有任何东西正在关闭流,因为如果你调用IOUtils.toString(inputStream)我会得到所有的内容。

See InputStream#read(byte[] b,int off,int len) contract: 请参阅InputStream #read(byte [] b,int off,int len)合同:

Reads up to len bytes of data from the input stream into an array of bytes. 将输入流中最多len个字节的数据读入一个字节数组。 An attempt is made to read as many as len bytes, but a smaller number may be read . 尝试读取len个字节, 但可以读取较小的数字 The number of bytes actually read is returned as an integer 实际读取的字节数以整数形式返回

Instead of using this method, use IOUtils.read which reads until you get the number of bytes you requested in a loop. 而不是使用此方法,使用IOUtils.read读取,直到您获得循环中请求的字节数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apache HttpClient in Java, instream.toString = org.apache.http.conn.EofSensorInputStream - Apache HttpClient in Java, instream.toString = org.apache.http.conn.EofSensorInputStream Websphere httpclient NoSuchMethodError org.apache.http.conn.Scheme - Websphere httpclient NoSuchMethodError org.apache.http.conn.Scheme httpclient 异常“org.apache.http.conn.ConnectionPoolTimeoutException:等待连接超时” - httpclient exception “org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection” HTTPClient“主要” java.lang.NoSuchFieldError:实例位于org.apache.http.conn.ssl.SSLConnectionSocketFactory。 <clinit> - HTTPClient “main” java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit> NoClassDefFoundError:org / apache / http / conn / SchemePortResolver-硒 - NoClassDefFoundError: org/apache/http/conn/SchemePortResolver - Selenium org.apache.http.conn.HttpHostConnectException:拒绝连接到http:// localhost - org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused java.lang.NoClassDefFoundError:org / apache / http / impl / conn / PoolingClientConnectionManager - java.lang.NoClassDefFoundError: org/apache/http/impl/conn/PoolingClientConnectionManager java.lang.NoClassDefFoundError:带有AmazonHttpClient的org / apache / http / conn / SchemePortResolver - java.lang.NoClassDefFoundError: org/apache/http/conn/SchemePortResolver with AmazonHttpClient Wildfly 8.1 ClassNotFound org.apache.http.conn.ClientConnectionManager - Wildfly 8.1 ClassNotFound org.apache.http.conn.ClientConnectionManager NoSuchMethodError org.apache.http.conn.scheme.Scheme - NoSuchMethodError org.apache.http.conn.scheme.Scheme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM