FileDocCategorySizeDatePackage
JarInputStream.javaAPI DocAndroid 1.5 API7571Wed May 06 22:41:02 BST 2009java.util.jar

JarInputStream

public class JarInputStream extends ZipInputStream
The input stream from which the JAR file to be read may be fetched. It is used like the {@code ZipInputStream}.
see
ZipInputStream
since
Android 1.0

Fields Summary
private Manifest
manifest
private boolean
eos
private JarEntry
mEntry
private JarEntry
jarEntry
private boolean
isMeta
private JarVerifier
verifier
private OutputStream
verStream
Constructors Summary
public JarInputStream(InputStream stream, boolean verify)
Constructs a new {@code JarInputStream} from an input stream.

param
stream the input stream containing the JAR file.
param
verify if the file should be verified with a {@code JarVerifier}.
throws
IOException If an error occurs reading entries from the input stream.
see
ZipInputStream#ZipInputStream(InputStream)
since
Android 1.0


                                                                                           
        
              
        super(stream);
        if (verify) {
            verifier = new JarVerifier("JarInputStream"); //$NON-NLS-1$
        }
        if ((mEntry = getNextJarEntry()) == null) {
            return;
        }
        String name = Util.toASCIIUpperCase(mEntry.getName());
        if (name.equals(JarFile.META_DIR)) {
            mEntry = null; // modifies behavior of getNextJarEntry()
            closeEntry();
            mEntry = getNextJarEntry();
            name = mEntry.getName().toUpperCase();
        }
        if (name.equals(JarFile.MANIFEST_NAME)) {
            mEntry = null;
            manifest = new Manifest(this, verify);
            closeEntry();
            if (verify) {
                verifier.setManifest(manifest);
                if (manifest != null) {
                    verifier.mainAttributesChunk = manifest
                            .getMainAttributesChunk();
                }
            }

        } else {
            Attributes temp = new Attributes(3);
            temp.map.put("hidden", null); //$NON-NLS-1$
            mEntry.setAttributes(temp);
            /*
             * if not from the first entry, we will not get enough
             * information,so no verify will be taken out.
             */
            verifier = null;
        }
    
public JarInputStream(InputStream stream)
Constructs a new {@code JarInputStream} from an input stream.

param
stream the input stream containing the JAR file.
throws
IOException If an error occurs reading entries from the input stream.
see
ZipInputStream#ZipInputStream(InputStream)
since
Android 1.0

        this(stream, true);
    
Methods Summary
protected java.util.zip.ZipEntrycreateZipEntry(java.lang.String name)

        JarEntry entry = new JarEntry(name);
        if (manifest != null) {
            entry.setAttributes(manifest.getAttributes(name));
        }
        return entry;
    
public java.util.jar.ManifestgetManifest()
Returns the {@code Manifest} object associated with this {@code JarInputStream} or {@code null} if no manifest entry exists.

return
the MANIFEST specifying the contents of the JAR file.
since
Android 1.0

        return manifest;
    
public java.util.zip.ZipEntrygetNextEntry()
Returns the next {@code ZipEntry} contained in this stream or {@code null} if no more entries are present.

return
the next extracted ZIP entry.
throws
IOException if an error occurs while reading the entry.
since
Android 1.0

        if (mEntry != null) {
            jarEntry = mEntry;
            mEntry = null;
            jarEntry.setAttributes(null);
        } else {
            jarEntry = (JarEntry) super.getNextEntry();
            if (jarEntry == null) {
                return null;
            }
            if (verifier != null) {
                isMeta = Util.toASCIIUpperCase(jarEntry.getName()).startsWith(
                        JarFile.META_DIR);
                if (isMeta) {
                    verStream = new ByteArrayOutputStream();
                } else {
                    verStream = verifier.initEntry(jarEntry.getName());
                }
            }
        }
        eos = false;
        return jarEntry;
    
public java.util.jar.JarEntrygetNextJarEntry()
Returns the next {@code JarEntry} contained in this stream or {@code null} if no more entries are present.

return
the next JAR entry.
throws
IOException if an error occurs while reading the entry.
since
Android 1.0

        return (JarEntry) getNextEntry();
    
public intread(byte[] buffer, int offset, int length)
Reads up to {@code length} of decompressed data and stores it in {@code buffer} starting at {@code offset}.

param
buffer Buffer to store into
param
offset offset in buffer to store at
param
length number of bytes to store
return
Number of uncompressed bytes read
throws
IOException if an IOException occurs.
since
Android 1.0

        if (mEntry != null) {
            return -1;
        }
        int r = super.read(buffer, offset, length);
        if (verStream != null && !eos) {
            if (r == -1) {
                eos = true;
                if (verifier != null) {
                    if (isMeta) {
                        verifier.addMetaEntry(jarEntry.getName(),
                                ((ByteArrayOutputStream) verStream)
                                        .toByteArray());
                        try {
                            verifier.readCertificates();
                        } catch (SecurityException e) {
                            verifier = null;
                            throw e;
                        }
                    } else {
                        verifier.verifySignatures(
                                (JarVerifier.VerifierEntry) verStream,
                                jarEntry);
                    }
                }
            } else {
                verStream.write(buffer, offset, r);
            }
        }
        return r;