关于java中流关闭的问题
黄舟
黄舟 2017-04-18 09:30:13
0
2
376

有如下代码:

private static String extractContent(HttpResponse response) throws Exception {
    String htmStr = null;
    if (response.getStatusLine().getStatusCode() == 200) {
        if (response != null) {
            BufferedReader br = null;
            InputStream ins = null;
            try {
                HttpEntity entity = response.getEntity();
                ins = entity.getContent();
                br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
                StringBuffer sbf = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sbf.append(line);
                }
                // 处理内容
                htmStr = sbf.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != br) {
                    br.close();
                }
                if (null != ins) {
                    ins.close();
                }
            }
        }
    }
    return htmStr;
}

代码中的br 和ins都正确的关闭掉了。但是在使用InputStreamReader的时候,是通过
br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
来使用的,那么InputStreamReader是否需要显示的关闭,即改成:

private static String extractContent(HttpResponse response) throws Exception {
    String htmStr = null;
    if (response.getStatusLine().getStatusCode() == 200) {
        if (response != null) {
            BufferedReader br = null;
            InputStream ins = null;
            InputStreamReader inr = null;
            try {
                HttpEntity entity = response.getEntity();
                ins = entity.getContent();
                inr = new InputStreamReader(ins, "UTF-8");
                br = new BufferedReader(inr);
                StringBuffer sbf = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sbf.append(line);
                }
                // 处理内容
                htmStr = sbf.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != br) {
                    br.close();
                }
                if (null != ins) {
                    ins.close();
                }
                if (null != inr) {
                    inr.close();
                }
            }
        }
    }
    return htmStr;
}

我知道在JDK7之后提供了try with resources的方法解决了这种问题,但是在JDK7之前是不是必须的这样用,显示的分别new相应流对象,然后最后再关闭,而不能
br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
像这样嵌套这来用。
我的问题:
1.如果没有流关闭,会带来安全隐患,具体带来何种安全隐患?最好能举例说明。还有说没有正确的关闭流,会造成系统资源的浪费,具体是指什么?(我的理解是浪费CPU资源,导致CPU轮询去查询是否有I/O传输,这样的理解对不对啊)??
2.jdk1.7之前,流的使用,是不是必须要用这种结构:

BufferedReader ins = null;
InputStreamReader inr = null;
    try{
        ins = ...
        inr = ...
    }catch{
    ...
    }finally{
        if(null != ins) ins.close;
        if(null != inr) inr.close;
    }

而不能使用这样的结构:

BufferedReader ins = null;
    try{
        ins = new InputStreamReader(new InputStrea(System.in, "UTF-8"));
    }catch{
    ...
    }finally{
        if(null != ins) ins.close;
    }

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
小葫芦

https://segmentfault.com/q/1010000005970993?_ea=969143

迷茫
  1. If the stream is not closed:

    数据可能还在缓冲区,没有flush。
    文件可能还被系统lock,没有释放。
    。。。(待补充)
  2. When the close method of the wrapping stream is called, the close of the wrapped stream will be automatically called. (You can check it in the jdk source code)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!