Reading image files with embedded spaces in file name from jar

R

Rhino

I am working on a program that will get icon files from a jar. I have the
code to get and read those files working satisfactorily except for one
thing: if the file name (of the image file, as opposed to the file name of
the jar) contains an imbedded blank, my reading logic fails.

For example, if the jar is called My.jar and the icon file I want is called
Foo.gif, everything works fine. But if the icon file is called Foo Bar.gif,
my logic fails. I would like my code to work even if embedded blanks occur
in the file name but I'm not sure how to accomplish it. I've googled but
haven't found anything that solves the problem.

Here is the relevant fragment of my code:

try {
URL url = this.getClass().getResource( gifName );
if ( url == null ) {
System.err.println("File " + gifName + " not found.");
}
Image image =
Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent() );
// wait till it is loaded
MediaTracker tracker;
try {
tracker = new MediaTracker( this );
tracker.addImage( image, 0 );
tracker.waitForID(0);
}
catch ( InterruptedException i_excp ) {
System.err.println("Oops, failed to load image into Media
Tracker.");
}
return image ;
}
catch ( IOException io_excp )
{
System.err.println("Oops, got I/O error.");
}

If gifName contains an embedded blank, I set the URL okay (it is NOT null),
but url.getContent() blows up; I get:

java.io.FileNotFoundException: JAR entry Images/Checkered%20Flag.jpg not
found in E:\eclipse\3.0.1\eclipse\workspace\Project1\jarProject1Media.jar

I'm certain that the jar contains a file called Images/Checkered Flag.jpg.

How do I improve this code so that it can read an image file when the file
name contains embedded spaces? (I assume that I will also have problems if
the jar file name contains embedded blanks so if you know now to make my
code handle THAT situation properly too, I'd appreciate hearing the
solution!)

I am using J2SE 1.5 on Windows XP.

--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
 
R

Rhino

What about substituting %20 for spaces before you try to read the image?
I'm not sure exactly what you mean.

I tried this but it failed; I got a null on the URL:

String encodedFileName = null;
try {
encodedFileName = URLEncoder.encode(gifName, "UTF-8");
} catch (UnsupportedEncodingException ue_excp) {
System.err.println("Failed to encode file name " + gifName + ".
Cannot continue.");
return null;
}

try {
URL url = this.getClass().getResource(encodedFileName);
if (url == null) {
System.err.println("File " + gifName + " not found.");
return null;
}

Image image =
Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent());
MediaTracker tracker;
try {
tracker = new MediaTracker(this);
tracker.addImage(image, 0);
tracker.waitForID(0);
} catch (InterruptedException i_excp) {
System.err.println("Oops, failed to load image into Media
Tracker.");
}
return image;
} catch (IOException io_excp) {
System.err.println("Oops, got I/O error.");
return null;
}

Did you mean something else??

Rhino
 
R

Roland

I am working on a program that will get icon files from a jar. I have the
code to get and read those files working satisfactorily except for one
thing: if the file name (of the image file, as opposed to the file name of
the jar) contains an imbedded blank, my reading logic fails.

For example, if the jar is called My.jar and the icon file I want is called
Foo.gif, everything works fine. But if the icon file is called Foo Bar.gif,
my logic fails. I would like my code to work even if embedded blanks occur
in the file name but I'm not sure how to accomplish it. I've googled but
haven't found anything that solves the problem.

Here is the relevant fragment of my code:

try {
URL url = this.getClass().getResource( gifName );
if ( url == null ) {
System.err.println("File " + gifName + " not found.");
}
Image image =
Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent() );
// wait till it is loaded
MediaTracker tracker;
try {
tracker = new MediaTracker( this );
tracker.addImage( image, 0 );
tracker.waitForID(0);
}
catch ( InterruptedException i_excp ) {
System.err.println("Oops, failed to load image into Media
Tracker.");
}
return image ;
}
catch ( IOException io_excp )
{
System.err.println("Oops, got I/O error.");
}

If gifName contains an embedded blank, I set the URL okay (it is NOT null),
but url.getContent() blows up; I get:

java.io.FileNotFoundException: JAR entry Images/Checkered%20Flag.jpg not
found in E:\eclipse\3.0.1\eclipse\workspace\Project1\jarProject1Media.jar

I'm certain that the jar contains a file called Images/Checkered Flag.jpg.

How do I improve this code so that it can read an image file when the file
name contains embedded spaces? (I assume that I will also have problems if
the jar file name contains embedded blanks so if you know now to make my
code handle THAT situation properly too, I'd appreciate hearing the
solution!)

I am using J2SE 1.5 on Windows XP.
Rhino,

There seems to be a bug in J2SE 1.5 for accessing entries in a jar file
which contain a space character.

I created a jar with your TempAbout app and an image whose name contains
two spaces ('duke on swing.gif').

When I run this Jar with J2SE 1.4, the image is displayed, and the
printed image url shows:
<jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/duke
on swing.gif>

However, when I run it with J2SE 1.5, the image does not show up; the
image url prints as:
<jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/duke%20on%20swing.gif>

It might be worthwhile to search the Java bugdatabase for this problem,
or submit the bug yourself.

The apparent work-around is to use files whose name don't contain space
characters (or any other characters that get URL escaped as %xx).

HTH,
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
R

Rhino

Roland said:
Rhino,

There seems to be a bug in J2SE 1.5 for accessing entries in a jar file
which contain a space character.

I created a jar with your TempAbout app and an image whose name contains
two spaces ('duke on swing.gif').

When I run this Jar with J2SE 1.4, the image is displayed, and the
printed image url shows:
<jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/du
ke
on swing.gif>

However, when I run it with J2SE 1.5, the image does not show up; the
image url prints as:
<jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/du
ke%20on%20swing.gif>

It might be worthwhile to search the Java bugdatabase for this problem,
or submit the bug yourself.

The apparent work-around is to use files whose name don't contain space
characters (or any other characters that get URL escaped as %xx).
Ahhh! That theory would explain why I haven't had any luck reading a file
that has embedded blanks in its name if the file is in a jar despite many
different attempts. The possibility that this is a new bug in J2SE 1.5 may
also explain why I haven't seen any posts mentioning this problem; if not
many people are using 1.5 yet and most of them don't use jars whose files
have embedded blanks, maybe I'm the first to come across this problem....

I'll have a look on the bug database and see what I can find.

Thanks, Roland!

Rhino
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top