1

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();
 }
}
asked Jul 9, 2014 at 22:03
4
  • How are you writing the file? could you be missing flush() / close()? Commented Jul 9, 2014 at 22:07
  • I'm having trouble following the sequence of events. Could you provide more details (maybe step-by-step) about your interaction with the file? When do you created it? When do you write it? When do you close it? When do you reopen it? When do you get the error? Commented Jul 9, 2014 at 22:08
  • 1
    new File(filePath); does not create a file. Show relevant code please. Commented Jul 9, 2014 at 23:47
  • Edited to clear up I meant big F file, and now have added the method where the Keystore.store() function is called. The saveKeystore() function is definitely called before any attempt to access using the InputStream is used. Commented Jul 10, 2014 at 14:50

1 Answer 1

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.

answered Jul 10, 2014 at 15:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.