1 /*
   2  * Copyright (c) 1996, 1998, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.tools.jar;
  28 
  29 import sun.awt.image.URLImageSource;
  30 import sun.awt.image.ImageDecoder;
  31 import java.net.URL;
  32 import java.net.JarURLConnection;
  33 import java.util.jar.JarFile;
  34 import java.util.jar.JarEntry;
  35 import java.io.InputStream;
  36 import java.io.IOException;
  37 
  38 
  39 public class JarImageSource extends URLImageSource {
  40     String mimeType;
  41     String entryName = null;
  42     URL url;
  43 
  44     /**
  45      * Create an image source from a Jar entry URL with the specified
  46      * mime type.
  47      */
  48     public JarImageSource(URL u, String type) {
  49         super(u);
  50         url = u;
  51         mimeType = type;
  52     }
  53 
  54     /**
  55      * Create an image source from a Jar file/entry URL
  56      * with the specified entry name and mime type.
  57      */
  58     public JarImageSource(URL u, String name, String type) {
  59         this(u, type);
  60         this.entryName = name;
  61     }
  62 
  63     protected ImageDecoder getDecoder() {
  64         InputStream is = null;
  65         try {
  66             JarURLConnection c = (JarURLConnection)url.openConnection();
  67             JarFile f = c.getJarFile();
  68             JarEntry e = c.getJarEntry();
  69 
  70             if (entryName != null && e == null) {
  71                 e = f.getJarEntry(entryName);
  72             }
  73             if (e == null || (e != null && entryName != null
  74                               && (!(entryName.equals(e.getName()))))) {
  75                 return null;
  76             }
  77             is = f.getInputStream(e);
  78         } catch (IOException e) {
  79             return null;
  80         }
  81 
  82         ImageDecoder id = decoderForType(is, mimeType);
  83         if (id == null) {
  84             id = getDecoder(is);
  85         }
  86         return id;
  87     }
  88 }