home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / jar / JarInputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.2 KB  |  183 lines

  1. /*
  2.  * @(#)JarInputStream.java    1.15 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util.jar;
  16.  
  17. import java.util.zip.*;
  18. import java.io.*;
  19. import java.security.Identity;
  20.  
  21. /**
  22.  * The <code>JarInputStream</code> class is used to read the contents of
  23.  * a JAR file from any input stream. It extends the class
  24.  * <code>java.util.zip.ZipInputStream</code> with support for reading
  25.  * an optional <code>Manifest</code> entry. The <code>Manifest</code>
  26.  * can be used to store meta-information about the JAR file and its entries.
  27.  *
  28.  * @author  David Connelly
  29.  * @version 1.15, 03/18/98
  30.  * @see        Manifest
  31.  * @see        java.util.zip.ZipInputStream
  32.  * @since   JDK1.2
  33.  */
  34. public
  35. class JarInputStream extends ZipInputStream {
  36.     private Manifest man;
  37.     private JarEntry manEntry;
  38.     private JarEntry first;
  39.     private JarVerifier jv;
  40.  
  41.     /**
  42.      * Creates a new <code>JarInputStream</code> and reads the optional
  43.      * manifest. If a manifest is present, also attempts to verify
  44.      * the signatures if the JarInputStream is signed.
  45.      * @param in the actual input stream
  46.      * @exception IOException if an I/O error has occurred
  47.      */
  48.     public JarInputStream(InputStream in) throws IOException {
  49.     this(in, true);
  50.     }
  51.  
  52.     /**
  53.      * Creates a new <code>JarInputStream</code> and reads the optional
  54.      * manifest. If a manifest is present and verify is true, also attempts 
  55.      * to verify the signatures if the JarInputStream is signed.
  56.      *
  57.      * @param in the actual input stream
  58.      * @param verify whether or not to verify the JarInputStream if
  59.      * it is signed.
  60.      * @exception IOException if an I/O error has occurred
  61.      */
  62.     public JarInputStream(InputStream in, boolean verify) throws IOException {
  63.     super(in);
  64.     JarEntry e = (JarEntry)super.getNextEntry();
  65.  
  66.     if (e.getName().equalsIgnoreCase("META-INF/"))
  67.         e = (JarEntry)super.getNextEntry();
  68.  
  69.     if (JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) {
  70.         man = new Manifest();
  71.         byte bytes[] = getBytes(new BufferedInputStream(this));
  72.         man.read(new ByteArrayInputStream(bytes));
  73.          //man.read(new BufferedInputStream(this));
  74.         closeEntry();
  75.         manEntry = e;
  76.         if (verify) {
  77.         jv = new JarVerifier(man, bytes);
  78.         }
  79.         first = getNextJarEntry();
  80.     } else {
  81.         first = e;
  82.     }
  83.     }
  84.  
  85.     private byte[] getBytes(InputStream is)
  86.     throws IOException
  87.     {
  88.     byte[] buffer = new byte[8192];
  89.     ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
  90.  
  91.     int n;
  92.  
  93.     baos.reset();
  94.     while ((n = is.read(buffer, 0, buffer.length)) != -1) {
  95.         baos.write(buffer, 0, n);
  96.     }
  97.     return baos.toByteArray();
  98.     }
  99.  
  100.     /**
  101.      * Returns the <code>Manifest</code> for this JAR file, or
  102.      * <code>null</code> if none.
  103.      */
  104.     public Manifest getManifest() {
  105.     return man;
  106.     }
  107.  
  108.     /**
  109.      * Returns the JarEntry for the manifest, or <code>null</code> if none.
  110.      */
  111.     public JarEntry getManifestEntry() {
  112.     return manEntry;
  113.     }
  114.  
  115.     /**
  116.      * Reads the next ZIP file entry and positions stream at the beginning
  117.      * of the entry data.
  118.      * @exception ZipException if a ZIP file error has occurred
  119.      * @exception IOException if an I/O error has occurred
  120.      */
  121.     public ZipEntry getNextEntry() throws IOException {
  122.     JarEntry e;
  123.     if (first == null) {
  124.         e = (JarEntry)super.getNextEntry();
  125.     } else {
  126.         e = first;
  127.         first = null;
  128.     }
  129.     if (jv != null && e != null) {
  130.         jv.beginEntry(e);
  131.     }
  132.     return e;
  133.     }
  134.  
  135.     /**
  136.      * Reads the next JAR file entry and positions the stream at the
  137.      * beginning of the entry data.
  138.      * @exception ZipException if a ZIP file error has occurred
  139.      * @exception IOException if an I/O error has occurred
  140.      */
  141.     public JarEntry getNextJarEntry() throws IOException {
  142.     return (JarEntry)getNextEntry();
  143.     }
  144.  
  145.     /**
  146.      * Reads from the current JAR file entry into an array of bytes.
  147.      * Blocks until some input is available.
  148.      * @param b the buffer into which the data is read
  149.      * @param off the start offset of the data
  150.      * @param len the maximum number of bytes to read
  151.      * @return the actual number of bytes read, or -1 if the end of the
  152.      *         entry is reached
  153.      * @exception ZipException if a ZIP file error has occurred
  154.      * @exception IOException if an I/O error has occurred
  155.      */
  156.     public int read(byte[] b, int off, int len) throws IOException {
  157.     int n;
  158.     if (first == null) {
  159.         n = super.read(b, off, len);
  160.     } else {
  161.         n = -1;
  162.     }
  163.     if (jv != null) {
  164.         jv.update(n, b, off, len);
  165.     }
  166.     return n;
  167.     }
  168.  
  169.     /**
  170.      * Creates a new <code>JarEntry</code> (<code>ZipEntry</code>) for the
  171.      * specified JAR file entry name.
  172.      *
  173.      * @param name the name of the JAR/ZIP file entry
  174.      */
  175.     protected ZipEntry createZipEntry(String name) {
  176.     JarEntry e = new JarEntry(name);
  177.     if (man != null) {
  178.         e.attr = man.getAttributes(name);
  179.     }
  180.     return e;
  181.     }
  182. }
  183.