Changeset 4255 in josm


Ignore:
Timestamp:
Jul 17, 2011 11:39:15 AM (23 months ago)
Author:
bastiK
Message:

minor refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r3874 r4255  
    134134            return null; 
    135135        if (name.startsWith("http://")) { 
    136             ImageWrapper iw = cache.get(name); 
    137             if (iw == null) { 
    138                 try { 
    139                     MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(), 
    140                     "images").toString()); 
    141                     Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
    142                     iw = new ImageWrapper(img, false); 
    143                     cache.put(name, iw); 
    144                 } catch (IOException e) { 
    145                 } 
    146             } 
    147             return iw; 
     136            return getIfAvailableHttp(name); 
    148137        } 
    149138        if (subdir == null) { 
     
    165154        ImageWrapper iw = cache.get(cache_name); 
    166155        if (iw == null) { 
    167             if(archive != null) 
    168             { 
    169                 ZipFile zipFile = null; 
    170                 try 
    171                 { 
    172                     zipFile = new ZipFile(archive); 
    173                     ZipEntry entry = zipFile.getEntry(full_name); 
    174                     if(entry != null) 
    175                     { 
    176                         int size = (int)entry.getSize(); 
    177                         int offs = 0; 
    178                         byte[] buf = new byte[size]; 
    179                         InputStream is = null; 
    180                         try { 
    181                             is = zipFile.getInputStream(entry); 
    182                             while(size > 0) 
    183                             { 
    184                                 int l = is.read(buf, offs, size); 
    185                                 offs += l; 
    186                                 size -= l; 
    187                             } 
    188                             Image img = Toolkit.getDefaultToolkit().createImage(buf); 
    189                             iw = new ImageWrapper(img, false); 
    190                         } finally { 
    191                             if (is != null) { 
    192                                 is.close(); 
    193                             } 
    194                         } 
    195                     } 
    196                 } catch (Exception e) { 
    197                     System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString())); 
    198                 } finally { 
    199                     if (zipFile != null) { 
    200                         try { 
    201                             zipFile.close(); 
    202                         } catch (IOException ex) { 
    203                         } 
    204                     } 
    205                 } 
     156            if (archive != null) { 
     157                iw = getIfAvailableZip(full_name, archive); 
    206158            } 
    207159            // getImageUrl() does a ton of "stat()" calls and gets expensive 
     
    210162            // and don't bother to create a URL unless we're actually 
    211163            // creating the image. 
    212             if(iw == null) 
    213             { 
     164            if (iw == null) { 
    214165                URL path = getImageUrl(full_name, dirs); 
    215166                if (path == null) 
     
    222173 
    223174        return iw; 
     175    } 
     176 
     177    private static ImageWrapper getIfAvailableHttp(String url) { 
     178        ImageWrapper iw = cache.get(url); 
     179        if (iw == null) { 
     180            try { 
     181                MirroredInputStream is = new MirroredInputStream(url, new File(Main.pref.getPreferencesDir(), 
     182                "images").toString()); 
     183                Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
     184                iw = new ImageWrapper(img, false); 
     185                cache.put(url, iw); 
     186            } catch (IOException e) { 
     187            } 
     188        } 
     189        return iw; 
     190    } 
     191 
     192    private static ImageWrapper getIfAvailableZip(String full_name, File archive) { 
     193        ZipFile zipFile = null; 
     194        Image img = null; 
     195        try 
     196        { 
     197            zipFile = new ZipFile(archive); 
     198            ZipEntry entry = zipFile.getEntry(full_name); 
     199            if(entry != null) 
     200            { 
     201                int size = (int)entry.getSize(); 
     202                int offs = 0; 
     203                byte[] buf = new byte[size]; 
     204                InputStream is = null; 
     205                try { 
     206                    is = zipFile.getInputStream(entry); 
     207                    while(size > 0) 
     208                    { 
     209                        int l = is.read(buf, offs, size); 
     210                        offs += l; 
     211                        size -= l; 
     212                    } 
     213                    img = Toolkit.getDefaultToolkit().createImage(buf); 
     214                } finally { 
     215                    if (is != null) { 
     216                        is.close(); 
     217                    } 
     218                } 
     219            } 
     220        } catch (Exception e) { 
     221            System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString())); 
     222        } finally { 
     223            if (zipFile != null) { 
     224                try { 
     225                    zipFile.close(); 
     226                } catch (IOException ex) { 
     227                } 
     228            } 
     229        } 
     230        return img == null ? null : new ImageWrapper(img, false); 
    224231    } 
    225232 
Note: See TracChangeset for help on using the changeset viewer.