2

I am having a data in bytes, and i want to write it into a memory location (not in a file at a location). To do this i am using RandomAccessFile and MappedByteBuffer based on this link

The code looks good and worked fine only when i give a absolute path (say /mnt/sdcard/a.txt) while creating a RandomAccessFile object and also after i run the code i could see a.txt is getting created at the mentioned location.But i need that to be not happening, i want that bytes to be there in the Memory not in the physical location (in the sense, internal, external and application location).

NOTE:

After i written that in memory i need that to be read it again from memory.

Thanks in Advance

Sudarshan
  • 1,291
  • 3
  • 26
  • 35
  • 2
    A file is by definition somehow in a filesystem. What is the problem you are trying to solve with such a thing? If you want to store something purely in memory use a simple `byte[]` array or `ByteBuffer`. – zapl Dec 18 '13 at 09:49
  • If you want it to be written only to be memory, not a physical stroage you need to use a file system like `tmpfs` I don't know if Android supports this but even if it does, it doesn't have much memory so you have to use as little as possible. – Peter Lawrey Dec 18 '13 at 09:49
  • I've no experience with Memory Mapped Files, but I've read the link and looks like it is meant to cache a file (or a part of it) in RAM, modify it there, and commit back to disk. What you are trying, memory mapped files W/O actual files, is not possible using that approach, just stick to byte buffer variables in heap. – Mister Smith Dec 18 '13 at 09:51
  • @zapl is it possible to have a huge file data in the byte[] (say a 10MB file)? For this case only i am going for this MemoryMap functionality. – Sudarshan Dec 18 '13 at 09:52
  • Memory mapped files allow efficient random access to files. They won't solve the problem that you have limited memory (you can modify the file as if it was memory). 10MB should work on most newer phones but older ones might not (apps has as low as 16MB in total). The limit of `byte[]` is in theory the `int` used as index, i.e. 2GB. Consider using a real file in your app's [cache directoy](http://developer.android.com/reference/android/content/Context.html#getCacheDir%28%29) – zapl Dec 18 '13 at 10:01
  • @Deepak I'd say 10 MB is doable, but might be device dependent. Have a look at the [`largeHeap`](http://developer.android.com/guide/topics/manifest/application-element.html#largeHeap) manifest attribute. Also [here](http://stackoverflow.com/q/4351678/813951) and [here](http://stackoverflow.com/questions/3590443/what-is-the-maximum-memory-limits-per-application-for-android-2-2) – Mister Smith Dec 18 '13 at 10:01
  • @zapl I know about this large heap and the support in the newer phones, but i am just finding a way to sort this problem down.We have a way to have in a cache of our application so trying to work around this – Sudarshan Dec 18 '13 at 10:11
  • Whatever you use in memory is going to use the memory. That means that if some kind of "memory file" works so would a `byte[]`. And if you don't have that much memory you simply can't use memory to cache the data. You can probably try to use memory and write it to a file if there is not enough [Related info here](http://developer.android.com/training/articles/memory.html) – zapl Dec 18 '13 at 10:21
  • you might want to look into this http://developer.android.com/reference/android/os/MemoryFile.html but you can not use RandomAccessFile/MappedByteBuffer as they read from the file-system only – Dexter Nov 09 '14 at 07:46
  • Can this help with MediaPlayer class, to play audio inputSteam instead of first putting it into a real storage file? – android developer Apr 12 '16 at 07:03

0 Answers0