Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ImageIO / Java Web Start bug: Workaround  (Read 9636 times)
0 Members and 1 Guest are viewing this topic.
Offline mudman

Junior Devvie




Here we go again...


« Posted 2004-12-17 06:46:42 »

Hi, I was using ImageIO for my project, and it all worked fine until I webstarted it. Then suddenly gif images started to look weird, while 24 bit pngs didn't load at all, but instead caused an exception to be thrown.
That bothered me a lot, since I didn't wanna go back to the toolkit/mediatracker combo because I was copying the image into a compatible bufferimage for hardware acceleration and wanted to do this without any dependencies to Components (Need an ImageListener for MediaTracker)
Finally last day bracke*CoB* from devshed.com posted the same problem, and his conclusion was that the images weren't fully loaded.

So I tried the following workaround, that seems to do the trick:

Instead of
1  
2  
BufferedImage src = javax.imageio.ImageIO.read(ImageLoader.class.getClassLoader().getResourceAsStream(resource));
                  

I wrap the ressource stream in a BufferedInputStream:
1  
2  
3  
BufferedImage src = javax.imageio.ImageIO.read(
                        new BufferedInputStream(ImageLoader.class.getClassLoader().getResourceAsStream(resource)));
                  


The problem seems to be that ImageIO doesn't keep reading if it can't get all of the data in one read().




Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #1 - Posted 2004-12-18 01:01:37 »

I believe this is a known bug with java.io (not imageio, actually). I don't have the bug id handy, though.
Offline Mark Thornton

Senior Devvie





« Reply #2 - Posted 2005-02-14 12:47:15 »

Quote
I believe this is a known bug with java.io (not imageio, actually). I don't have the bug id handy, though.

Only the readFully method guarantees to read as much as you might expect. If you are using the normal read method, then it is permitted to return less than the number of bytes requested. Nothing in the documentation guarantees it to return the amount given by the 'available' method.

There is an RFE requesting that a ZIPInputStream should return all the data it can (and not just as much as is convenient for the implementation).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dronus

Senior Newbie




"Der CRC32-Hashwert dieses Satzes ist BA5D7983."


« Reply #3 - Posted 2005-02-20 12:28:07 »

Simple workaround:

Store images uncompressed
gif /png are already compressed and will get euqal or maybe even bigger in size if zip'ped.
So I JARed my application without gfx, and used a zip packer to add the gfx with compression factor = zero. this worked well for all my apps.
(and its no "dirty" fix as JAR gets smaller most times...)
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #4 - Posted 2005-04-22 19:20:48 »

I found the same thing & solved it in much the same way.  Your solution is neater then mine, so i'm going to pinch yours  Grin

Time flies like a bird. Fruit flies like a banana.
Offline broumbroum

Junior Devvie





« Reply #5 - Posted 2007-04-17 16:27:53 »

Then simply reimplement your MEdiaTracker:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
// override your common paint method//add src to MediaTracker with the desired display sizes, that is important if it is worth to fit to component size (see scale Transform)!
public void paintComponent(Graphics g) {
  MediaTracker mt = new MEdiaTracker(this);
  mt.addImage((Image)src, this.hashCode(), src.getWidth(this), src.getHeight(this));
  // simply wait for loading and then display
  try{ mt.waitForID(this.hashCode()); g.drawImage(src, 0,0, this); } catch(Exception e) {}
  finally{
  g.dispose();
  }
}
Roll Eyes Roll Eyes Roll Eyes Roll Eyes
(...)
That bothered me a lot, since I didn't wanna go back to the toolkit/mediatracker combo because I was copying the image into a compatible bufferimage for hardware acceleration and wanted to do this without any dependencies to Components (Need an ImageListener for MediaTracker)
Finally last day bracke*CoB* from devshed.com posted the same problem, and his conclusion was that the images weren't fully loaded.

So I tried the following workaround, that seems to do the trick:

Instead of
1  
2  
BufferedImage src = javax.imageio.ImageIO.read(ImageLoader.class.getClassLoader().getResourceAsStream(resource));
                 

I wrap the ressource stream in a BufferedInputStream:
1  
2  
3  
BufferedImage src = javax.imageio.ImageIO.read(
                        new BufferedInputStream(ImageLoader.class.getClassLoader().getResourceAsStream(resource)));
                 


The problem seems to be that ImageIO doesn't keep reading if it can't get all of the data in one read().






::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #6 - Posted 2007-04-17 21:04:50 »

Thanks for necroing the thread Roll Eyes

It's a known bug. It was fixed. Use the BufferedInputStream wrap for backward compatibility.

MediaTracker... lol.

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (67 views)
2014-11-03 18:54:52

moogie (82 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!