JarInputStream.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* JarInputStream.java - InputStream for reading jar files
  2. Copyright (C) 2000, 2004 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.io.InputStream;
  34. import java.util.zip.ZipEntry;
  35. import java.util.zip.ZipInputStream;
  36. /**
  37. * InputStream for reading jar files.
  38. * XXX - verification of the signatures in the Manifest file is not yet
  39. * implemented.
  40. *
  41. * @since 1.2
  42. * @author Mark Wielaard (mark@klomp.org)
  43. */
  44. public class JarInputStream extends ZipInputStream
  45. {
  46. // Fields
  47. /** The manifest for this file or null when there was no manifest. */
  48. private Manifest manifest;
  49. /** The first real JarEntry for this file. Used by readManifest() to store
  50. an entry that isn't the manifest but that should be returned by
  51. getNextEntry next time it is called. Null when no firstEntry was read
  52. while searching for the manifest entry, or when it has already been
  53. returned by getNextEntry(). */
  54. private JarEntry firstEntry;
  55. // Constructors
  56. /**
  57. * Creates a new JarInputStream and tries to read the manifest.
  58. * If such a manifest is present the JarInputStream tries to verify all
  59. * the entry signatures while reading.
  60. *
  61. * @param in InputStream to read the jar from
  62. * @exception IOException when an error occurs when opening or reading
  63. */
  64. public JarInputStream(InputStream in) throws IOException
  65. {
  66. this(in, true);
  67. }
  68. /**
  69. * Creates a new JarInputStream and tries to read the manifest.
  70. * If such a manifest is present and verify is true, the JarInputStream
  71. * tries to verify all the entry signatures while reading.
  72. *
  73. * @param in InputStream to read the jar from
  74. * @param verify whether or not to verify the manifest entries
  75. * @exception IOException when an error occurs when opening or reading
  76. */
  77. public JarInputStream(InputStream in, boolean verify) throws IOException
  78. {
  79. super(in);
  80. readManifest(verify);
  81. }
  82. // Methods
  83. /**
  84. * Set the manifest if found. Skips all entries that start with "META-INF/"
  85. *
  86. * @param verify when true (and a Manifest is found) checks the Manifest,
  87. * when false no check is performed
  88. * @exception IOException if an error occurs while reading
  89. */
  90. private void readManifest(boolean verify) throws IOException
  91. {
  92. firstEntry = (JarEntry) super.getNextEntry();
  93. while ((firstEntry != null) &&
  94. firstEntry.getName().startsWith("META-INF/"))
  95. {
  96. if (firstEntry.getName().equals(JarFile.MANIFEST_NAME))
  97. {
  98. manifest = new Manifest(this);
  99. }
  100. firstEntry = (JarEntry) super.getNextEntry();
  101. }
  102. if (verify)
  103. {
  104. // XXX
  105. }
  106. }
  107. /**
  108. * Creates a JarEntry for a particular name and consults the manifest
  109. * for the Attributes of the entry.
  110. * Used by <code>ZipEntry.getNextEntry()</code>
  111. *
  112. * @param name the name of the new entry
  113. */
  114. protected ZipEntry createZipEntry(String name)
  115. {
  116. ZipEntry zipEntry = super.createZipEntry(name);
  117. JarEntry jarEntry = new JarEntry(zipEntry);
  118. if (manifest != null)
  119. {
  120. jarEntry.attr = manifest.getAttributes(name);
  121. }
  122. return jarEntry;
  123. }
  124. /**
  125. * Returns the Manifest for the jar file or null if there was no Manifest.
  126. */
  127. public Manifest getManifest()
  128. {
  129. return manifest;
  130. }
  131. /**
  132. * Returns the next entry or null when there are no more entries.
  133. * Does actually return a JarEntry, if you don't want to cast it yourself
  134. * use <code>getNextJarEntry()</code>. Does not return any entries found
  135. * at the beginning of the ZipFile that are special
  136. * (those that start with "META-INF/").
  137. *
  138. * @exception IOException if an IO error occurs when reading the entry
  139. */
  140. public ZipEntry getNextEntry() throws IOException
  141. {
  142. ZipEntry entry;
  143. if (firstEntry != null)
  144. {
  145. entry = firstEntry;
  146. firstEntry = null;
  147. }
  148. else
  149. {
  150. entry = super.getNextEntry();
  151. }
  152. return entry;
  153. }
  154. /**
  155. * Returns the next jar entry or null when there are no more entries.
  156. *
  157. * @exception IOException if an IO error occurs when reading the entry
  158. */
  159. public JarEntry getNextJarEntry() throws IOException
  160. {
  161. return (JarEntry) getNextEntry();
  162. }
  163. /**
  164. * XXX
  165. *
  166. * @param buf XXX
  167. * @param off XXX
  168. * @param len XXX
  169. * @return XXX
  170. * @exception IOException XXX
  171. */
  172. public int read(byte[]buf, int off, int len) throws IOException
  173. {
  174. // XXX if (verify) {}
  175. return super.read(buf, off, len);
  176. }
  177. }