Reputation: 33
I'm developing an application that necessitates the use of sound files (through the JLayer audio library at the moment) and am wondering if it is possible to cache a file in memory so that it doesn't have to be read off of the hard disk every time it's needed.
For example, I can store an image in a BufferedImage object instead of calling ImageIO.read every time I need an image; is there a similar way to approach this using JLayer? Should I use a different audio library? I just don't want to be hitting the disk every time the user triggers a sound (potentially many times per second).
If it makes a difference, the audio is in MP3 format.
Upvotes: 1
Views: 738
Reputation: 262794
You can read any file (up to a certain size) into a byte[] and process it from there.
Java's abstraction of InputStreams (where the consumer need not know where the data is from) should make this very straightforward (assuming your audio library is not hard-coded to read from files).
Start by looking up ByteArrayOutputStream and ByteArrayInputStream.
Upvotes: 2