RandomAccessFile opening of a file inside an executable jar?

  • Thread starter Christopher Fuhrman
  • Start date
C

Christopher Fuhrman

Hello,

Anyone know if this is possible? The call to open() doesn't seem to be happy
with the "!" in the path name.

I only want to do read-only operations on the file (which is a .wav file).

Thanks,

Cris
 
L

Lee Fesperman

Christopher said:
Anyone know if this is possible? The call to open() doesn't seem to be happy
with the "!" in the path name.

No, it is not possible. The "!" is only recognized in URLs, not in java.io.File.
RandomAccessFile only works with java.io.File.

Note: Please don't be 'cute'. Repeat your subject in the body of the article.
I only want to do read-only operations on the file (which is a .wav file).

Can you process it as an InputStream (sequentially)? Otherwise, you will have to
'extract' it from the Jar ... to do random access.
 
F

Frank

Hello,

Anyone know if this is possible? The call to open() doesn't seem to be
happy
with the "!" in the path name.

I only want to do read-only operations on the file (which is a .wav
file).

Thanks,

Cris

Not with RAF. However, you can open an InputStream to the file via
ClassLoader.getResource (I think). From there you can either
A) use File.createTempFile, extract your .wav, then open a RAF on the
temp file. (Good for lots of backwards and forwards reading)
or
B) each time you need to jump to a location, open a new InputStream and
use it's .skip method (the default implementation actually reads through n
bytes, but for a .jar file, I think this method is overloaded to roughly
jump to the correct position in a single operation).

HTH

Frank
 
C

Chris Uppal

Frank said:
B) each time you need to jump to a location, open a new InputStream and
use it's .skip method (the default implementation actually reads through n
bytes, but for a .jar file, I think this method is overloaded to roughly
jump to the correct position in a single operation).

I wouldn't bet on that, not without testing it and/or reading the source (to
the JVM and library that your code will run on).

The JAR file format is basically the same as the ZIP file format, and random
access within a ZIP file entry is not necessarily constant-time since
compressed formats don't naturally support jumping around. It /may/ be fast if
the .WAV entry is not compressed (which would make sense, but I'm not sure
it'll happen by default), /and/ the implementor thought it worthwhile
recognising and handling that case, but there's no reason to suppose it /must/
be. (People don't generally do random access in ZIP files, so why bother
writing code to optimise a case that is unlikely to occur ?)

That said, if you aren't doing /lots/ of random access (only when the user asks
for it, say) then starting again at the beginning each time may well be plenty
fast enough, even if the ZIP code doesn't do real random access underneath.

-- chris
 
C

Cris Fuhrman

Lee Fesperman said:
No, it is not possible. The "!" is only recognized in URLs, not in java.io.File.
RandomAccessFile only works with java.io.File.

Thanks -- I understand now the difference. I was surprised that my
code 'broke' once I put it into an executable .jar. This problem is
obviously a caveat of distributing a java application in a jar,
without extracting on the fly the randomaccessfiles.
Note: Please don't be 'cute'. Repeat your subject in the body of the article.

Sorry -- you mistook my terseness (laziness?) for trying to be cute
;-) I shall respect the netiquette.

Cris

Is it possible to open a RandomAccessFile that's inside an executable
jar?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top