1 7 8 package java.util.jar; 9 10 import java.util.zip.*; 11 import java.io.*; 12 import sun.security.util.ManifestEntryVerifier; 13 14 27 public 28 class JarInputStream extends ZipInputStream { 29 private Manifest man; 30 private JarEntry first; 31 private JarVerifier jv; 32 private ManifestEntryVerifier mev; 33 34 35 42 public JarInputStream(InputStream in) throws IOException { 43 this(in, true); 44 } 45 46 56 public JarInputStream(InputStream in, boolean verify) throws IOException { 57 super(in); 58 JarEntry e = (JarEntry )super.getNextEntry(); 59 60 if (e != null && e.getName().equalsIgnoreCase("META-INF/")) 61 e = (JarEntry )super.getNextEntry(); 62 63 if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) { 64 man = new Manifest (); 65 byte bytes[] = getBytes(new BufferedInputStream(this)); 66 man.read(new ByteArrayInputStream(bytes)); 67 closeEntry(); 69 if (verify) { 70 jv = new JarVerifier (bytes); 71 mev = new ManifestEntryVerifier(man); 72 } 73 first = getNextJarEntry(); 74 } else { 75 first = e; 76 } 77 } 78 79 private byte[] getBytes(InputStream is) 80 throws IOException 81 { 82 byte[] buffer = new byte[8192]; 83 ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); 84 85 int n; 86 87 baos.reset(); 88 while ((n = is.read(buffer, 0, buffer.length)) != -1) { 89 baos.write(buffer, 0, n); 90 } 91 return baos.toByteArray(); 92 } 93 94 101 public Manifest getManifest() { 102 return man; 103 } 104 105 115 public ZipEntry getNextEntry() throws IOException { 116 JarEntry e; 117 if (first == null) { 118 e = (JarEntry )super.getNextEntry(); 119 } else { 120 e = first; 121 first = null; 122 } 123 if (jv != null && e != null) { 124 if (jv.nothingToVerify() == true) { 128 jv = null; 129 mev = null; 130 } else { 131 jv.beginEntry(e, mev); 132 } 133 } 134 return e; 135 } 136 137 148 public JarEntry getNextJarEntry() throws IOException { 149 return (JarEntry )getNextEntry(); 150 } 151 152 168 public int read(byte[] b, int off, int len) throws IOException { 169 int n; 170 if (first == null) { 171 n = super.read(b, off, len); 172 } else { 173 n = -1; 174 } 175 if (jv != null) { 176 jv.update(n, b, off, len, mev); 177 } 178 return n; 179 } 180 181 190 protected ZipEntry createZipEntry(String name) { 191 JarEntry e = new JarEntry (name); 192 if (man != null) { 193 e.attr = man.getAttributes(name); 194 } 195 return e; 196 } 197 } 198 | Popular Tags |