001/* JarInputStream.java - InputStream for reading jar files 002 Copyright (C) 2000, 2004 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.io.InputStream; 042import java.util.zip.ZipEntry; 043import java.util.zip.ZipInputStream; 044 045/** 046 * InputStream for reading jar files. 047 * XXX - verification of the signatures in the Manifest file is not yet 048 * implemented. 049 * 050 * @since 1.2 051 * @author Mark Wielaard (mark@klomp.org) 052 */ 053 054public class JarInputStream extends ZipInputStream 055{ 056 // Fields 057 058 /** The manifest for this file or null when there was no manifest. */ 059 private Manifest manifest; 060 061 /** The first real JarEntry for this file. Used by readManifest() to store 062 an entry that isn't the manifest but that should be returned by 063 getNextEntry next time it is called. Null when no firstEntry was read 064 while searching for the manifest entry, or when it has already been 065 returned by getNextEntry(). */ 066 private JarEntry firstEntry; 067 068 // Constructors 069 070 /** 071 * Creates a new JarInputStream and tries to read the manifest. 072 * If such a manifest is present the JarInputStream tries to verify all 073 * the entry signatures while reading. 074 * 075 * @param in InputStream to read the jar from 076 * @exception IOException when an error occurs when opening or reading 077 */ 078 public JarInputStream(InputStream in) throws IOException 079 { 080 this(in, true); 081 } 082 083 /** 084 * Creates a new JarInputStream and tries to read the manifest. 085 * If such a manifest is present and verify is true, the JarInputStream 086 * tries to verify all the entry signatures while reading. 087 * 088 * @param in InputStream to read the jar from 089 * @param verify whether or not to verify the manifest entries 090 * @exception IOException when an error occurs when opening or reading 091 */ 092 public JarInputStream(InputStream in, boolean verify) throws IOException 093 { 094 super(in); 095 readManifest(verify); 096 } 097 098 // Methods 099 100 /** 101 * Set the manifest if found. Skips all entries that start with "META-INF/" 102 * 103 * @param verify when true (and a Manifest is found) checks the Manifest, 104 * when false no check is performed 105 * @exception IOException if an error occurs while reading 106 */ 107 private void readManifest(boolean verify) throws IOException 108 { 109 firstEntry = (JarEntry) super.getNextEntry(); 110 while ((firstEntry != null) && 111 firstEntry.getName().startsWith("META-INF/")) 112 { 113 if (firstEntry.getName().equals(JarFile.MANIFEST_NAME)) 114 { 115 manifest = new Manifest(this); 116 } 117 firstEntry = (JarEntry) super.getNextEntry(); 118 } 119 120 if (verify) 121 { 122 // XXX 123 } 124 } 125 126 /** 127 * Creates a JarEntry for a particular name and consults the manifest 128 * for the Attributes of the entry. 129 * Used by <code>ZipEntry.getNextEntry()</code> 130 * 131 * @param name the name of the new entry 132 */ 133 protected ZipEntry createZipEntry(String name) 134 { 135 ZipEntry zipEntry = super.createZipEntry(name); 136 JarEntry jarEntry = new JarEntry(zipEntry); 137 if (manifest != null) 138 { 139 jarEntry.attr = manifest.getAttributes(name); 140 } 141 return jarEntry; 142 } 143 144 /** 145 * Returns the Manifest for the jar file or null if there was no Manifest. 146 */ 147 public Manifest getManifest() 148 { 149 return manifest; 150 } 151 152 /** 153 * Returns the next entry or null when there are no more entries. 154 * Does actually return a JarEntry, if you don't want to cast it yourself 155 * use <code>getNextJarEntry()</code>. Does not return any entries found 156 * at the beginning of the ZipFile that are special 157 * (those that start with "META-INF/"). 158 * 159 * @exception IOException if an IO error occurs when reading the entry 160 */ 161 public ZipEntry getNextEntry() throws IOException 162 { 163 ZipEntry entry; 164 if (firstEntry != null) 165 { 166 entry = firstEntry; 167 firstEntry = null; 168 } 169 else 170 { 171 entry = super.getNextEntry(); 172 } 173 return entry; 174 } 175 176 /** 177 * Returns the next jar entry or null when there are no more entries. 178 * 179 * @exception IOException if an IO error occurs when reading the entry 180 */ 181 public JarEntry getNextJarEntry() throws IOException 182 { 183 return (JarEntry) getNextEntry(); 184 } 185 186 /** 187 * XXX 188 * 189 * @param buf XXX 190 * @param off XXX 191 * @param len XXX 192 * @return XXX 193 * @exception IOException XXX 194 */ 195 public int read(byte[]buf, int off, int len) throws IOException 196 { 197 // XXX if (verify) {} 198 return super.read(buf, off, len); 199 } 200}