IOException when opening JFileChooser
Asked Answered
G

2

7

Ok this one is really weird. Every first time my application opens a JFileChooser it throws a IOException then some icons don't show properly.

java.io.IOException
    at sun.awt.image.GifImageDecoder.readHeader(GifImageDecoder.java:265)
    at sun.awt.image.GifImageDecoder.produceImage(GifImageDecoder.java:102)
    at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

Now when I dig into the error, it seems like on one icon when it tries to read the header, it retrieve only the first 8 bytes which is not enough. I've have checked the icons files and they all seem OK. I've tried to override the icon file with another one that loads properly before this error but same thing.

Here is my stack when breaking on this error :

Daemon Thread [Image Fetcher 0] (Suspended (exception IOException)) 
    GifImageDecoder.readHeader() line: 265 [local variables unavailable]    
    GifImageDecoder.produceImage() line: 102 [local variables unavailable]  
    ByteArrayImageSource(InputStreamImageSource).doFetch() line: 246    
    ImageFetcher.fetchloop() line: 172  
    ImageFetcher.run() line: 136 [local variables unavailable]

Here is my variable value when digging into GifImageDecoder instance.

source  ByteArrayImageSource  (id=272)  
awaitingFetch   false   
consumers   null    
decoder GifImageDecoder  (id=271)   
decoders    GifImageDecoder  (id=271)   
imagedata    (id=307)   
    [0] 71  
    [1] 73  
    [2] 70  
    [3] 56  
    [4] 57  
    [5] 97  
    [6] 16  
    [7] 13  
    [8] 10  
imagelength 9   
imageoffset 0   

Normally, this imagedata should be way bigger. First 10 bytes is header but it only retrieve 8 bytes as you can see. After this exception, every other icon from JFileChooser doesn't load properly.

This is a proper call to readHeader() :

source  ByteArrayImageSource  (id=208)  
awaitingFetch   false   
consumers   null    
decoder GifImageDecoder  (id=207)   
decoders    GifImageDecoder  (id=207)   
imagedata    (id=223)   
    [0...99]    
    [100...199] 
    [200...299] 
    [300...399] 
    [400...499] 
    [500...599] 
    [600...699] 
    [700...799] 
    [800...899] 
    [900...979] 
imagelength 980 
imageoffset 0   

The buffer is fully loaded with an icon right before the icon that throws an error.

Here is an exemple of where it could crash (it happens in several part of my code, whenever I first load my system icons) :

public class DirectoryBrowser extends JFileChooser{

private String suffixAccepted = null;

public DirectoryBrowser(File file, String chooserTitle, String approveOpenBtnText, String suffixAccepted)
{
    super(file);
    this.suffixAccepted = suffixAccepted;
    init(chooserTitle, approveOpenBtnText);
}

when it enters in super(file) it goes there :

Thread [AWT-EventQueue-0] (Suspended)   
    Object.wait(long) line: not available [native method]   
    MediaTracker.waitForID(int, long) line: 651 
    ImageIcon.loadImage(Image) line: 234    
    ImageIcon.<init>(byte[]) line: 215  
    SwingUtilities2$2.createValue(UIDefaults) line: 1105    
    UIDefaults.getFromHashtable(Object) line: 185   
    UIDefaults.get(Object) line: 130    
    MultiUIDefaults.get(Object) line: 44    
    MultiUIDefaults(UIDefaults).getIcon(Object) line: 411   
    UIManager.getIcon(Object) line: 613 
    IronFileChooserUI(BasicFileChooserUI).installIcons(JFileChooser) line: 233  
    IronFileChooserUI(BasicFileChooserUI).installDefaults(JFileChooser) line: 219   
    IronFileChooserUI(BasicFileChooserUI).installUI(JComponent) line: 135   
    IronFileChooserUI(MetalFileChooserUI).installUI(JComponent) line: 139   
    DirectoryBrowser(JComponent).setUI(ComponentUI) line: 653   
    DirectoryBrowser(JFileChooser).updateUI() line: 1757    
    DirectoryBrowser(JFileChooser).setup(FileSystemView) line: 366  
    DirectoryBrowser(JFileChooser).<init>(File, FileSystemView) line: 332   
    DirectoryBrowser(JFileChooser).<init>(File) line: 315   
    DirectoryBrowser.<init>(File, String, String, String) line: 33  
    PackToIntegratePanel.choosePackPathToIntegrateFile() line: 522  
    PackToIntegratePanel$1.actionPerformed(ActionEvent) line: 104   
    JButton(AbstractButton).fireActionPerformed(ActionEvent) line: 1849 
    AbstractButton$Handler.actionPerformed(ActionEvent) line: 2169  
    DefaultButtonModel.fireActionPerformed(ActionEvent) line: 420   
    DefaultButtonModel.setPressed(boolean) line: 258    
    BasicButtonListener.mouseReleased(MouseEvent) line: 236 
    JButton(Component).processMouseEvent(MouseEvent) line: 5517 
    JButton(JComponent).processMouseEvent(MouseEvent) line: 3135    
    JButton(Component).processEvent(AWTEvent) line: 5282    
    JButton(Container).processEvent(AWTEvent) line: 1966    
    JButton(Component).dispatchEventImpl(AWTEvent) line: 3984   
    JButton(Container).dispatchEventImpl(AWTEvent) line: 2024   
    JButton(Component).dispatchEvent(AWTEvent) line: 3819   
    LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: 4212 
    LightweightDispatcher.processMouseEvent(MouseEvent) line: 3892  
    LightweightDispatcher.dispatchEvent(AWTEvent) line: 3822    
    WorkbenchFrame(Container).dispatchEventImpl(AWTEvent) line: 2010    
    WorkbenchFrame(Window).dispatchEventImpl(AWTEvent) line: 1791   
    WorkbenchFrame(Component).dispatchEvent(AWTEvent) line: 3819    
    EventQueue.dispatchEvent(AWTEvent) line: 463    
    EventDispatchThread.pumpOneEventForHierarchy(int, Component) line: 242  
    EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 163   
    EventDispatchThread.pumpEvents(int, Conditional) line: 157  
    EventDispatchThread.pumpEvents(Conditional) line: 149   
    EventDispatchThread.run() line: 110 

then other thread mentioned above retrieves the icons (cf 2nd stack of this post)

Gauleiter answered 27/10, 2011 at 8:15 Comment(7)
Are you working on the event queue?Mashie
Are you working on the event queue? What do you mean? I don't manipulate any queue.Gauleiter
Where are stored the icons? Are the "default" JFileChooser icons or are yours icons?. Please, add some code, it could help.Fouquet
Catalina Island meant the Event Dispatch Thread. If the code is excecuted in the EDT.Brimstone
I use the default lookAndFeel from java so my icons are in the jdk libs. And yes apparently it runs on EDT, I will try to move it to another exclusive thread. Or maybe at first start of my application I could load the look and feel so it load the images on a separate thread then it won't crash on loading in EDT. will try someday, thxGauleiter
@Gauleiter If you can provide us with a simple and independently working code where this error occurs, it'l be easy for us to help you..Ladykiller
I cannot sorry, I can't reproduce the problem outside my software and I can't possibly publish part of the source code it's heavily copyrighted. Sorry I know it's harder but I've done the research and provided stack traces. @CatalinaIsland asked about EDT which is imo a good start.Gauleiter
M
1

What do you mean? I don't manipulate any queue.

Sorry. Like @kai said, I meant the event dispatch thread (EDT). Make sure you create all your components, including the JFileChooser, on the EDT like they show in the tutorial. If you don't, it'll be a race between some file system and your display.

I just noticed: Does it still happen without that IronFileChooser thing?

Mashie answered 10/8, 2012 at 13:19 Comment(3)
I've tried using invokeLater when i go into the action but it's still buggy. But I think you are right with the thread, I need to rework the whole thing and make sure it's in EDT.Gauleiter
IronFileChooser is just the lookandfeel, I've tried without it and it's still the same. Anyway you were the one the closest of the answer, I just need to fix it now. thxGauleiter
Ok; it was worth a try. You might try this ThreadCheckingRepaintManager to look for EDT problems.Mashie
D
0

I had a very similar problem. It seems that form one 1.6.X version onward this is a JFileChooser bug. Sometimes it throws this internal exception for no apparent reason. I'm not sure if it has been fixed or even properly documented. Also, i'm not sure if this applies to your situation but it does remind me of this issue.

Deadfall answered 24/11, 2011 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.