Source Code Cross Referenced for JarInputStream.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » jar » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.jar 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util.jar;
027
028        import java.util.zip.*;
029        import java.io.*;
030        import sun.security.util.ManifestEntryVerifier;
031
032        /**
033         * The <code>JarInputStream</code> class is used to read the contents of
034         * a JAR file from any input stream. It extends the class
035         * <code>java.util.zip.ZipInputStream</code> with support for reading
036         * an optional <code>Manifest</code> entry. The <code>Manifest</code>
037         * can be used to store meta-information about the JAR file and its entries.
038         *
039         * @author  David Connelly
040         * @version 1.43, 05/05/07
041         * @see	    Manifest
042         * @see	    java.util.zip.ZipInputStream
043         * @since   1.2
044         */
045        public class JarInputStream extends ZipInputStream {
046            private Manifest man;
047            private JarEntry first;
048            private JarVerifier jv;
049            private ManifestEntryVerifier mev;
050
051            /**
052             * Creates a new <code>JarInputStream</code> and reads the optional
053             * manifest. If a manifest is present, also attempts to verify
054             * the signatures if the JarInputStream is signed.
055             * @param in the actual input stream
056             * @exception IOException if an I/O error has occurred
057             */
058            public JarInputStream(InputStream in) throws IOException {
059                this (in, true);
060            }
061
062            /**
063             * Creates a new <code>JarInputStream</code> and reads the optional
064             * manifest. If a manifest is present and verify is true, also attempts 
065             * to verify the signatures if the JarInputStream is signed.
066             *
067             * @param in the actual input stream
068             * @param verify whether or not to verify the JarInputStream if
069             * it is signed.
070             * @exception IOException if an I/O error has occurred
071             */
072            public JarInputStream(InputStream in, boolean verify)
073                    throws IOException {
074                super (in);
075                JarEntry e = (JarEntry) super .getNextEntry();
076
077                if (e != null && e.getName().equalsIgnoreCase("META-INF/"))
078                    e = (JarEntry) super .getNextEntry();
079
080                if (e != null
081                        && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) {
082                    man = new Manifest();
083                    byte bytes[] = getBytes(new BufferedInputStream(this ));
084                    man.read(new ByteArrayInputStream(bytes));
085                    //man.read(new BufferedInputStream(this));
086                    closeEntry();
087                    if (verify) {
088                        jv = new JarVerifier(bytes);
089                        mev = new ManifestEntryVerifier(man);
090                    }
091                    first = getNextJarEntry();
092                } else {
093                    first = e;
094                }
095            }
096
097            private byte[] getBytes(InputStream is) throws IOException {
098                byte[] buffer = new byte[8192];
099                ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
100
101                int n;
102
103                baos.reset();
104                while ((n = is.read(buffer, 0, buffer.length)) != -1) {
105                    baos.write(buffer, 0, n);
106                }
107                return baos.toByteArray();
108            }
109
110            /**
111             * Returns the <code>Manifest</code> for this JAR file, or
112             * <code>null</code> if none.
113             *
114             * @return the <code>Manifest</code> for this JAR file, or
115             *         <code>null</code> if none.
116             */
117            public Manifest getManifest() {
118                return man;
119            }
120
121            /**
122             * Reads the next ZIP file entry and positions the stream at the
123             * beginning of the entry data. If verification has been enabled,
124             * any invalid signature detected while positioning the stream for
125             * the next entry will result in an exception.
126             * @exception ZipException if a ZIP file error has occurred
127             * @exception IOException if an I/O error has occurred
128             * @exception SecurityException if any of the jar file entries
129             *         are incorrectly signed.
130             */
131            public ZipEntry getNextEntry() throws IOException {
132                JarEntry e;
133                if (first == null) {
134                    e = (JarEntry) super .getNextEntry();
135                } else {
136                    e = first;
137                    first = null;
138                }
139                if (jv != null && e != null) {
140                    // At this point, we might have parsed all the meta-inf
141                    // entries and have nothing to verify. If we have
142                    // nothing to verify, get rid of the JarVerifier object.
143                    if (jv.nothingToVerify() == true) {
144                        jv = null;
145                        mev = null;
146                    } else {
147                        jv.beginEntry(e, mev);
148                    }
149                }
150                return e;
151            }
152
153            /**
154             * Reads the next JAR file entry and positions the stream at the
155             * beginning of the entry data. If verification has been enabled,
156             * any invalid signature detected while positioning the stream for
157             * the next entry will result in an exception.
158             * @return the next JAR file entry, or null if there are no more entries
159             * @exception ZipException if a ZIP file error has occurred
160             * @exception IOException if an I/O error has occurred
161             * @exception SecurityException if any of the jar file entries
162             *         are incorrectly signed.
163             */
164            public JarEntry getNextJarEntry() throws IOException {
165                return (JarEntry) getNextEntry();
166            }
167
168            /**
169             * Reads from the current JAR file entry into an array of bytes.
170             * If <code>len</code> is not zero, the method
171             * blocks until some input is available; otherwise, no
172             * bytes are read and <code>0</code> is returned.
173             * If verification has been enabled, any invalid signature
174             * on the current entry will be reported at some point before the
175             * end of the entry is reached.
176             * @param b the buffer into which the data is read
177             * @param off the start offset in the destination array <code>b</code>
178             * @param len the maximum number of bytes to read
179             * @return the actual number of bytes read, or -1 if the end of the
180             *         entry is reached
181             * @exception  NullPointerException If <code>b</code> is <code>null</code>.
182             * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
183             * <code>len</code> is negative, or <code>len</code> is greater than 
184             * <code>b.length - off</code>
185             * @exception ZipException if a ZIP file error has occurred
186             * @exception IOException if an I/O error has occurred
187             * @exception SecurityException if any of the jar file entries
188             *         are incorrectly signed.
189             */
190            public int read(byte[] b, int off, int len) throws IOException {
191                int n;
192                if (first == null) {
193                    n = super .read(b, off, len);
194                } else {
195                    n = -1;
196                }
197                if (jv != null) {
198                    jv.update(n, b, off, len, mev);
199                }
200                return n;
201            }
202
203            /**
204             * Creates a new <code>JarEntry</code> (<code>ZipEntry</code>) for the
205             * specified JAR file entry name. The manifest attributes of
206             * the specified JAR file entry name will be copied to the new 
207             * <CODE>JarEntry</CODE>.
208             *
209             * @param name the name of the JAR/ZIP file entry
210             * @return the <code>JarEntry</code> object just created
211             */
212            protected ZipEntry createZipEntry(String name) {
213                JarEntry e = new JarEntry(name);
214                if (man != null) {
215                    e.attr = man.getAttributes(name);
216                }
217                return e;
218            }
219        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.