Just An Application

August 11, 2014

And Another One: Part Seven — JarFile.getInputStream

File

    $(ANDROID_SRC)/libcore/luni/src/main/java/java/util/jar/JarFile.java

Source

    ...
    
    @Override
    public InputStream getInputStream(ZipEntry ze) throws IOException {
        if (manifestEntry != null) {
            getManifest();
        }
        if (verifier != null) {
            verifier.setManifest(getManifest());
            if (manifest != null) {
                verifier.mainAttributesEnd = manifest.getMainAttributesEnd();
            }
            if (verifier.readCertificates()) {
                verifier.removeMetaEntries();
                if (manifest != null) {
                    manifest.removeChunks();
                }
                if (!verifier.isSignedJar()) {
                    verifier = null;
                }
            }
        }
        InputStream in = super.getInputStream(ze);
        if (in == null) {
            return null;
        }
        if (verifier == null || ze.getSize() == -1) {
            return in;
        }
        JarVerifier.VerifierEntry entry = verifier.initEntry(ze.getName());
        if (entry == null) {
            return in;
        }
        return new JarFileInputStream(in, ze, entry);
    }

    ...

The getInputStream is a method of two halves so to speak.

First Half: One Off Initialization

The first two if statements perform initialization tasks which are effectively only performed the very first time the getInputStream method is called on a JarFile instance.

In addition the second if statement is only performed if the instance variable verifier is not null.

When invoked by the PackageParser method loadCertificates for the first time we know that verifier is not null since the JarFile instance was created by the PackageParser method collectCertificates using the single-argument JarFile constructor which will result in verifier being initialized with an instance of JarVerifier.

Although the second if statement can also result in verifier being set to null

    ...
            if (!verifier.isSignedJar()) {
                verifier = null;
            }
    ...

we are only interested in the case when the JAR is signed so verifier so this particular if statement will not get executed.

Second Half: InputStream Construction

The second half of the method begins by calling its super method ZipFile.getInputStream to obtain an instance of
InputStream.

Assuming that the super method does not return null and that the getSize method of the ZipEntry argument does not return -1 then the JarVerifier method initEntry is invoked to obtain a JarVerifier.VerifierEntry instance.

Assuming that JarVerifier.initEntry does not return null the method returns a new instance of JarFile.JarFileInputStream constructed with the ZipEntry, the InputStream and JarVerifier.VerifierEntry.

It is this instance of InputStream that the PackageParser.loadCertificates method will use to read the contents of the file.


Copyright (c) 2014 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.

Advertisements

3 Comments »

  1. […] we have seen the JarFile method getInputStream invokes this after a successful call to the readCertificates method thereby ensuring that it […]

    Pingback by And Another One: Part Eight – JarVerifier.readCertificates | Just An Application — August 11, 2014 @ 3:35 pm

  2. […] At the point the initEntry method is called the JarFile.getInputStream has already executed all the initialization code in its first half […]

    Pingback by And Another One: Part Twelve — JarVerifier.initEntry | Just An Application — August 14, 2014 @ 8:51 am

  3. […] method creates a BufferedInputStream passing it the InputStream returned by the JarFile.getInputStream […]

    Pingback by And Another One: Part Thirteen — JarFile.JarFileInputStream.read | Just An Application — August 14, 2014 @ 6:08 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: