Do you need to explicity close a Java KeyStore inp

2020-04-12 07:59发布

When reading in a KeyStore using a FileInputStream as follows, does one need to explicitly close the input-steam to stop system resources being wasted ?

FileInputStream fin = new FileInputStream("keystore.jks");
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(fin, password);

//  Is this line needed ??
fin.close();

Is this FileInputStream closed automatically by the load() method or is explicit manually intervention required?

3条回答
Lonely孤独者°
2楼-- · 2020-04-12 08:11

Is this FileInputStream closed automatically by the load() method or is explicit manually intervention required?

yes it required to close to over come unnecessary leaks.

Checkout example given in java doc of KeyStore http://docs.oracle.com/javase/7/docs/api/java/security/KeyStore.html

查看更多
3楼-- · 2020-04-12 08:15

You should definitly use org.apache.commons.io.IOUtils.closequietly when dealing with streams and APIs you are not sure about

查看更多
【Aperson】
4楼-- · 2020-04-12 08:17

Yes, try this test

    FileInputStream fin = new FileInputStream("keystore.jks") {
        public void close() throws java.io.IOException {
            System.out.println("close");
        }
    };
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(fin, "changeit".toCharArray());

and you will see that close() is not called

查看更多
登录 后发表回答