001/* JarEntry.java - Represents an entry in a jar file
002   Copyright (C) 2000, 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package java.util.jar;
039
040import java.io.IOException;
041import java.security.cert.Certificate;
042import java.util.Set;
043import java.util.zip.ZipEntry;
044
045/**
046 * Extension to a ZipEntry that contains manifest attributes and certificates.
047 * Both the Atrributes and the Certificates can be null when not set.
048 * Note that the <code>getCertificates()</code> method only returns a
049 * valid value after all of the data of the entry has been read.
050 * <p>
051 * There are no public methods to set the attributes or certificate of an
052 * Entru. Only JarEntries created by the classes in <code>java.util.jar</code>
053 * will have these properties set.
054 *
055 * @since 1.2
056 * @author Mark Wielaard (mark@klomp.org)
057 */
058
059public class JarEntry extends ZipEntry
060{
061  // (Package local) fields
062
063  Attributes attr;
064  JarFile jarfile;
065
066  // Constructors
067
068  /**
069   * Creates a new JarEntry with the specified name and no attributes or
070   * or certificates. Calls <code>super(name)</code> so all other (zip)entry
071   * fields are null or -1.
072   *
073   * @param name the name of the new jar entry
074   * @exception NullPointerException when the supplied name is null
075   * @exception IllegalArgumentException when the supplied name is longer
076   * than 65535 bytes
077   */
078  public JarEntry(String name) throws NullPointerException,
079    IllegalArgumentException
080  {
081    super(name);
082    attr = null;
083    jarfile = null;
084  }
085
086  /**
087   * Creates a new JarEntry with the specified ZipEntry as template for
088   * all properties of the entry. Both attributes and certificates will be
089   * null.
090   *
091   * @param entry the ZipEntry whose fields should be copied
092   */
093  public JarEntry(ZipEntry entry)
094  {
095    super(entry);
096    attr = null;
097    jarfile = null;
098  }
099
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   */
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   */
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   */
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}