1 /* JarEntry.java - Represents an entry in a jar file
2    Copyright (C) 2000, 2006 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 package java.util.jar;
39 
40 import java.io.IOException;
41 import java.security.cert.Certificate;
42 import java.util.Set;
43 import java.util.zip.ZipEntry;
44 
45 /**
46  * Extension to a ZipEntry that contains manifest attributes and certificates.
47  * Both the Atrributes and the Certificates can be null when not set.
48  * Note that the <code>getCertificates()</code> method only returns a
49  * valid value after all of the data of the entry has been read.
50  * <p>
51  * There are no public methods to set the attributes or certificate of an
52  * Entru. Only JarEntries created by the classes in <code>java.util.jar</code>
53  * will have these properties set.
54  *
55  * @since 1.2
56  * @author Mark Wielaard (mark@klomp.org)
57  */
58 
59 public class JarEntry extends ZipEntry
60 {
61   // (Package local) fields
62 
63   Attributes attr;
64   JarFile jarfile;
65 
66   // Constructors
67 
68   /**
69    * Creates a new JarEntry with the specified name and no attributes or
70    * or certificates. Calls <code>super(name)</code> so all other (zip)entry
71    * fields are null or -1.
72    *
73    * @param name the name of the new jar entry
74    * @exception NullPointerException when the supplied name is null
75    * @exception IllegalArgumentException when the supplied name is longer
76    * than 65535 bytes
77    */
JarEntry(String name)78   public JarEntry(String name) throws NullPointerException,
79     IllegalArgumentException
80   {
81     super(name);
82     attr = null;
83     jarfile = null;
84   }
85 
86   /**
87    * Creates a new JarEntry with the specified ZipEntry as template for
88    * all properties of the entry. Both attributes and certificates will be
89    * null.
90    *
91    * @param entry the ZipEntry whose fields should be copied
92    */
JarEntry(ZipEntry entry)93   public JarEntry(ZipEntry entry)
94   {
95     super(entry);
96     attr = null;
97     jarfile = null;
98   }
99 
100   /**
101    * Creates a new JarEntry with the specified JarEntry as template for
102    * all properties of the entry.
103    *
104    * @param entry the jarEntry whose fields should be copied
105    */
JarEntry(JarEntry entry)106   public JarEntry(JarEntry entry)
107   {
108     super(entry);
109     try
110       {
111         attr = entry.getAttributes();
112       }
113     catch (IOException _)
114       {
115       }
116     jarfile = entry.jarfile;
117   }
118 
119   // Methods
120 
121   /**
122    * Returns a copy of the Attributes set for this entry.
123    * When no Attributes are set in the manifest null is returned.
124    *
125    * @return a copy of the Attributes set for this entry
126    * @exception IOException This will never be thrown. It is here for
127    * binary compatibility.
128    */
getAttributes()129   public Attributes getAttributes() throws IOException
130   {
131     if (attr != null)
132       {
133         return (Attributes) attr.clone();
134       }
135     else
136       {
137         return null;
138       }
139   }
140 
141   /**
142    * Returns a copy of the certificates set for this entry.
143    * When no certificates are set or when not all data of this entry has
144    * been read null is returned.
145    * <p>
146    * To make sure that this call returns a valid value you must read all
147    * data from the JarInputStream for this entry.
148    * When you don't need the data for an entry but want to know the
149    * certificates that are set for the entry then you can skip all data by
150    * calling <code>skip(entry.getSize())</code> on the JarInputStream for
151    * the entry.
152    *
153    * @return a copy of the certificates set for this entry
154    */
getCertificates()155   public Certificate[] getCertificates()
156   {
157     if (jarfile != null)
158       {
159         synchronized (jarfile)
160           {
161             if (jarfile.entryCerts != null)
162               {
163                 Set certs = (Set) jarfile.entryCerts.get(getName());
164                 if (certs != null
165                     && jarfile.verified.get(getName()) == Boolean.TRUE)
166                   return (Certificate[]) certs.toArray(new Certificate[certs.size()]);
167               }
168           }
169       }
170     return null;
171   }
172 }
173