1

我正在从一个包装了 HTTP 连接的 InputStream 中读取数据,如下所示:

       InputStream in = s3Object.getObjectContent();
       ByteArrayOutputStream out = new ByteArrayOutputStream(8192);
        byte[] tmp = new byte[8192];
        int r;
        while (true) {
            r = in.read(tmp);
            if(r <= 0) break;
            out.write(tmp,0,r);
        }
        in.close();

并获得“套接字已关闭”异常,线路日志显示以下内容:

DEBUG org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager - Shutting down
DEBUG org.apache.http.wire - << "Lr[0x86]{[0xf5]u[0xae][0xcc][0x8d]6[0x8a][0x16]([y%[0xb3][0xbb][0xd0]Va[0xc8]h[0x9][0x81][0xf2][0xb5][0x90]F[0x81])[0xf][0xfb]-`[0x94][0x14][0x4][0xb7];[0xd0][0x1d]"R>S[0xd1];[0xb1][0x81][0xad][0xc5][0xd6]:[0xc][0xf2]![0xdc][0xdb][0xce]g[0x5][0xdc]2"af[0xe6][0x15][0xb5]4[0xac][0xb9]L>[0x99]n7h[0x9e]z[0xdf][0x91]w[\r][0x19]!z[0x2])[0xde]d"
DEBUG org.apache.http.wire - << "J2[0x6][0xb0]b8j[0xc7]6[0xfa][0xb][0xe3]][0xf5][0x1b][0x94][0xa1][0xb6]F[0xda]k[0xb2]O[0xaa][0xfe][0xcd][0xe5]R[0x8f]w[0x84]JDx[0xe2][0xf4]3[0x15]d[0xf4]H*[0xce][0xc2][0xf3][0xec][0xd5][0x17]$[0xd6]Y[0xfd][0x8c]d[0xc9][0xa0][0x85][0xa2][0xc6][0xfb][0xee][0xdb][0xd9][0x1a][0x7][0xd7]R[0xc8]E'@[0x8b][0xe0][0xc3][0xe6][0xab][0xa3][0xbe]S^?[0x81][0xa3]8F[0xcf][0xe1]u[0xb2][0xbe][0xfc][0xa9][0xbb][0xba]^?[0x8c][0xba]/[0xd7][0xd5]+[0xf5][0xb2]+[0x16]M[0xc0][0xb0][0x3][0x8a]:5"
DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Closing connection [HttpRoute[{s}->https://svkbucket.s3.amazonaws.com]][null]
DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection shut down
DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection closed
DEBUG org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager - Released connection is not reusable.
DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Releasing connection [HttpRoute[{s}->https://svkbucket.s3.amazonaws.com]][null]
DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection closed
[Endpoint : 2] ERROR com.xxx.secures3proxy.activity.GetObjectActivity -   Exception:  Socket is closed
java.net.SocketException: Socket is closed
     at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1467)
     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
     at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
     at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:191)
     at org.apache.http.impl.conn.LoggingSessionInputBuffer.read(LoggingSessionInputBuffer.java:82)
     at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:164)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
    at java.security.DigestInputStream.read(DigestInputStream.java:161)

Apache HttpClient 4.1.1.510.540 和 Apache HttpCore 4.1.521.51

这是否意味着在我完成读取之前远程端已经关闭了套接字?或者我的 HttpClient 库或我正在使用的某个库正在关闭连接,然后才能读取所有数据?

4

2 回答 2

3

您的应用程序似乎HttpClient过早地关闭了连接池。您在问题中显示的代码中没有发生这种情况。该代码看起来像“无辜的受害者”。


这是我使用的 HttpClient 版本的问题吗?

可能不是。

还是我的环境中的某些东西改变了行为?

呃...可能是。特别是如果“我的环境”是一种晦涩难懂的方式来表达程序的其他部分。

于 2013-08-01T01:28:46.940 回答
0

“套接字已关闭”异常

你的一端关闭了套接字。如果对等端关闭了他的端,您在读取时会收到某种 EOS,或者在写入时会收到 IOException: 'connection reset'。

于 2013-08-01T01:33:35.393 回答