I am creating a new File object like so:
keystoreFile = new File(getDir("sslDir", Context.MODE_PRIVATE), "CAKeyStore.jks")
Later, I try to use the the file for an input stream like so:
BufferedInputStream bufstream = new BufferedInputStream(new FileInputStream(keystoreFile));
Log.d(TAG, "Available? " + bufstream.available());
sslKeystore.load(bufstream , KEYSTORE_PASSWORD.toCharArray());
and in logcat I get an EOF Exception, and "available" bytes shown is zero
Available? 0
07-09 18:00:13.090 11229-11229/de.blinkt.openvpn W/System.err: java.io.EOFException
07-09 18:00:13.090 11229-11229/de.blinkt.openvpn W/System.err: at libcore.io.Streams.readFully(Streams.java:83)
07-09 18:00:13.090 11229-11229/de.blinkt.openvpn W/System.err: at java.io.DataInputStream.readInt(DataInputStream.java:103)
07-09 18:00:13.090 11229-11229/de.blinkt.openvpn W/System.err: at com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi.engineLoad(BcKeyStoreSpi.java:799)
07-09 18:00:13.100 11229-11229/de.blinkt.openvpn W/System.err: at java.security.KeyStore.load(KeyStore.java:589)
However, if I clear my apps data, and try this again, I don't get the problem.
Is there something wrong with the way I am creating the file, such that when the app is re-loaded I get this EOF exception?
Edit:
The method where the file is actually created is within this function:
private void saveKeystore() {
try {
sslKeystore.store(new FileOutputStream(keystoreFile), KEYSTORE_PASSWORD.toCharArray());
} catch (Exception e) {
Log.d(TAG, "Unable to save keystore");
e.printStackTrace();
}
}
1 Answer 1
You need to close the FileOutputStream yourself. The KeyStore.store() won't do that for you. Ditto the FileInputStream and load(). See the examples in the Javadoc.
flush()/close()?