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

Burkhard
  • 14,596
  • 22
  • 87
  • 108
Waypoint
  • 17,283
  • 39
  • 116
  • 170

3 Answers3

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.

dmon
  • 30,048
  • 8
  • 87
  • 96
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).

aromero
  • 25,681
  • 6
  • 57
  • 79
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?

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • 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. – fangster Dec 22 '14 at 14:10
  • 1
    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. – Lukas Knuth Dec 22 '14 at 14:45
  • 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(); – fangster Jan 05 '15 at 15:22