Search code examples
javakeystore

Keystore-java inputstream


In the below code,why we using FileInputStream?For what purpose?Don't say refer java docs,wiki or anything. I need a practical answer.

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
// get user password and file input stream
char[] password = getPassword();

java.io.FileInputStream fis = null;
try {
    fis = new java.io.FileInputStream("keyStoreName");
    ks.load(fis, password);
} finally {
    if (fis != null) {
        fis.close();
    }
}

Solution

  • KeyStore.load does not take a FileInputStream. It takes an InputStream as a parameter and since FileInputStream extends InputStream you can pass it as a parameter. Why does the KeyStore require an input stream? Because the keystore saves its structure as a stream of bytes. For getting the stored state back, there should be a mechanism to load from some place. The KeyStore designers decided to use InputStream as the right choice. They could easily have designed a load method that takes a byte[] as a parameter.