JarEntry.java 5.3 KB

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