6

I am in a problem - i need to read SecretKey from Android APK (e.g. key.keystore), for my app it has to be read as a FileInputStream, but from assets I am getting only inputStream. How to convert inputStream to FileInputStream? Or is there any other way, how to acces file from e.g. resources as a fileInputStream?

Thanks

3 Answers 3

8

Why do you need a FileInputStream specifically? In general, you don't have to care about the underlying implementation of the InputStream, you just read from it. Maybe your implementation should be InputStream agnostic.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, 16hours of programming in a row make me mad :-)
4

I think you are referring to AssetManager.open() that returns an InputStream. There is no need to "convert" it to a FileInputStream, just get the reference to the InputStream and use it (wrap it in a BufferedInputStream if you want).

Comments

3

I don't think this is possible because a FileInputStream is simply a InputStream for Files. It does the job of getting an InputStream from the defined file for you, after that you're working with a normal InputStream.

Android already did this Job for you. So why do you need a FileInputStream?

3 Comments

You may want a FileInputStream as you want to know the length of the file unless there is a way I haven't yet found.
I don't see any getLength()-method on FileInputStream, so not sure what you're getting at. Also, if it's an asset that you put into the APK, you know it's file size. The FileInputStream is just a convenient way of getting a InputStream (to read the files content) from a "physical" file.
Sorry @Lukas-Knuth, I was confusing myself! However I am using the FileInputStream of an uncompressed resource in order to read in its data in full: AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.myresource); FileInputStream fis = afd.createInputStream(); data = new byte[(int)afd.getLength()]; DataInputStream dis = new DataInputStream(fis); dis.readFully(data); dis.close();

Your Answer

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.