简体   繁体   中英

Is it necessary to read/skip InputStream to completion before closing it?

Is it necessary to read/skip the InputStream before closing the stream? If the stream isn't read, will this cause any issues with future connections (using keep-alive)? Using Oracle JVM.

InputStream is = null;
try {
    URL url = new URL("http://example.com/executeTrigger?id=523");
    is = url.openStream();

} catch (Exception ex) {
} finally {
    if (is != null) {
        try {
            is.close();
        } catch (IOException e) {
        }
    }
}

Is the above valid, or should I call is.skip(Long.MAX_VALUE) right after executing the url.openStream() ?

In TCP it will cause the sender to get a 'connection reset by peer'. A web server is used to browsers disappearing, paging away, etc, so it won't cause any harm in this situation, but if you were implementing an application protocol it would be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM