简体   繁体   English

打开JFileChooser时出现IOException

[英]IOException when opening JFileChooser

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. 每次我的应用程序第一次打开JFileChooser时,它都会抛出IOException,然后某些图标无法正常显示。

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. 现在,当我深入研究错误时,它似乎在尝试读取标题时在一个图标上,它只检索前8个字节,这是不够的。 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. 这是挖掘GifImageDecoder实例时的变量值。

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. 前10个字节是标头,但它只能检索8个字节,如您所见。 After this exception, every other icon from JFileChooser doesn't load properly. 在此异常之后,JFileChooser中的每个其他图标都无法正确加载。

This is a proper call to readHeader() : 这是对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) 然后上面提到的其他线程检索图标(参见本文的第2个堆栈)

What do you mean? 你什么意思? I don't manipulate any queue. 我不操纵任何队列。

Sorry. 抱歉。 Like @kai said, I meant the event dispatch thread (EDT) . 就像@kai说的那样,我的意思是事件派遣线程(EDT) Make sure you create all your components, including the JFileChooser , on the EDT like they show in the tutorial . 确保在EDT上创建所有组件,包括JFileChooser ,就像它们在教程中所示 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? 我刚刚注意到:没有IronFileChooser事情,它仍然会发生吗?

I had a very similar problem. 我有一个非常相似的问题。 It seems that form one 1.6.X version onward this is a JFileChooser bug. 似乎从一个1.6.X版本开始,这是一个JFileChooser错误。 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. 此外,我不确定这是否适用于您的情况,但它确实提醒我这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM