Length of a resource

I

ICJC

Is there any way to find the length of a resource (as retrieved by
Class.getResourceAsStream())? This seems like an obvious piece of
functionality, but I can't find any API call that does it.

Cheers,

Ian.
 
T

Thomas Weidenfeller

Is there any way to find the length of a resource (as retrieved by
Class.getResourceAsStream())?

No, there is non.
This seems like an obvious piece of
functionality,

No, it is not obvious. If the data is generated on the fly, the length
might not be known at all in advance.

/Thomas
 
J

John C. Bollinger

ICJC said:
Is there any way to find the length of a resource (as retrieved by
Class.getResourceAsStream())?

Since getResourceAsStream() returns an InputStream, your question
becomes "Is there any way to find the length of an InputStream?" The
answer to this question is no, as an InputStream need not have a well
defined length.
This seems like an obvious piece of
functionality, but I can't find any API call that does it.

No, it's not obvious at all. You are incorporating an assumption that a
"resource" is necessarilly a fixed-length local file, or part of one.
That may frequently or even typically be the case, but it is in no way
required. If you are willing to make that assumption, however, then you
can try parsing the URL obtained via Class.getResource() and analyzing
the specified resource appropriately. You need to be prepared to handle
both whole files and zip / jar entries, at the very least. (Actually,
I'm not sure what the URL for a file in a jar or zip looks like, come to
think of it.)


John Bollinger
(e-mail address removed)
 
M

Mats Kindahl

[snip]
(Actually, I'm not sure what the URL for a file in a jar or
zip looks like, come to think of it.)

Something along the lines of

jar:file://D:/Some/Path/MyJar.jar!/images/theFile.png
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
any URL

Actually quite useful on occations...

Mats Kindahl
--
IAR Systems in Uppsala, Sweden.

Any opinions expressed are my own and not those of my company.

Spam prevention: contact me at (e-mail address removed) or
(e-mail address removed), removing the *NO SPAM* from the address.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top