APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.jar

public Class JarInputStream

extends ZipInputStream
Class Inheritance
Imports
java.util.zip.*, java.io.*, sun.security.util.ManifestEntryVerifier

The JarInputStream class, which extends ZipInputStream, is used to read the contents of a JAR file from an input stream. It provides support for reading an optional Manifest entry. The Manifest can be used to store meta-information about the JAR file and its entries.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Accessing the Manifest

The getManifest method is used to return the Manifest from the entry META-INF/MANIFEST.MF when it is the first entry in the stream (or the second entry if the first entry in the stream is META-INF/ and the second entry is META-INF/MANIFEST.MF).

The getNextJarEntry() and getNextEntry() methods are used to read JAR file entries from the stream. These methods skip over the Manifest (META-INF/MANIFEST.MF) when it is at the beginning of the stream. In other words, these methods do not return an entry for the Manifest when the Manifest is the first entry in the stream. If the first entry is META-INF/ and the second entry is the Manifest then both are skipped over by these methods. Whether these methods skip over the Manifest when it appears later in the stream is not specified.

Signed JAR Files

A JarInputStream verifies the signatures of entries in a Signed JAR file when:

Once the JarEntry has been completely verified, which is done by reading until the end of the entry's input stream, JarEntry#getCertificates() may be called to obtain the certificates for this entry and JarEntry#getCodeSigners() may be called to obtain the signers.

It is important to note that the verification process does not include validating the signer's certificate. A caller should inspect the return value of JarEntry#getCodeSigners() to further determine if the signature can be trusted.

API Note

If a JarEntry is modified after the JAR file is signed, a SecurityException will be thrown when the entry is read.

Author
David Connelly
Since
1.2
See Also
Manifest, java.util.zip.ZipInputStream

Field Summary

Modifier and TypeField and Description
private final boolean
private JarEntry
private JarVerifier
private Manifest
private ManifestEntryVerifier
private boolean

Constructor Summary

AccessConstructor and Description
public
JarInputStream(InputStream
the actual input stream
in
)

Creates a new JarInputStream and reads the optional manifest.

public
JarInputStream(InputStream
the actual input stream
in
,
boolean
whether or not to verify the JarInputStream if it is signed.
verify
)

Creates a new JarInputStream and reads the optional manifest.

Method Summary

Modifier and TypeMethod and Description
private JarEntry
protected ZipEntry

Returns:

the JarEntry object just created
createZipEntry
(String
the name of the JAR/ZIP file entry
name
)

Overrides java.util.zip.ZipInputStream.createZipEntry.

Creates a new JarEntry (ZipEntry) for the specified JAR file entry name.

public Manifest

Returns:

the Manifest for this JAR file, or null otherwise.
getManifest
()

Returns the Manifest for this JAR file when it is the first entry in the stream (or the second entry if the first entry in the stream is META-INF/ and the second entry is META-INF/MANIFEST.MF), or null otherwise.

public ZipEntry
getNextEntry()

Overrides java.util.zip.ZipInputStream.getNextEntry.

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

public JarEntry

Returns:

the next JAR file entry, or null if there are no more entries
getNextJarEntry
()

Reads the next JAR file entry and positions the stream at the beginning of the entry data.

public int

Returns:

the actual number of bytes read, or -1 if the end of the entry is reached
read
(byte[]
the buffer into which the data is read
b
,
int
the start offset in the destination array b
off
,
int
the maximum number of bytes to read
len
)

Overrides java.util.zip.ZipInputStream.read.

Reads from the current JAR entry into an array of bytes, returning the number of inflated bytes.

Inherited from java.util.zip.ZipInputStream:
availableclosecloseEntryreadreadAllBytesreadNBytesreadNBytesskipskipNBytestransferTo

Field Detail

doVerifyback to summary
private final boolean doVerify
firstback to summary
private JarEntry first
jvback to summary
private JarVerifier jv
manback to summary
private Manifest man
mevback to summary
private ManifestEntryVerifier mev
tryManifestback to summary
private boolean tryManifest

Constructor Detail

JarInputStreamback to summary
public JarInputStream(InputStream in) throws IOException

Creates a new JarInputStream and reads the optional manifest. If a manifest is present, also attempts to verify the signatures if the JarInputStream is signed.

Parameters
in:InputStream

the actual input stream

Exceptions
IOException:
if an I/O error has occurred
JarInputStreamback to summary
public JarInputStream(InputStream in, boolean verify) throws IOException

Creates a new JarInputStream and reads the optional manifest. If a manifest is present and verify is true, also attempts to verify the signatures if the JarInputStream is signed.

Parameters
in:InputStream

the actual input stream

verify:boolean

whether or not to verify the JarInputStream if it is signed.

Annotations
@SuppressWarnings:this-escape
Exceptions
IOException:
if an I/O error has occurred

Method Detail

checkManifestback to summary
private JarEntry checkManifest(JarEntry e) throws IOException
createZipEntryback to summary
protected ZipEntry createZipEntry(String name)

Overrides java.util.zip.ZipInputStream.createZipEntry.

Creates a new JarEntry (ZipEntry) for the specified JAR file entry name. The manifest attributes of the specified JAR file entry name will be copied to the new JarEntry.

Parameters
name:String

the name of the JAR/ZIP file entry

Returns:ZipEntry

the JarEntry object just created

getManifestback to summary
public Manifest getManifest()

Returns the Manifest for this JAR file when it is the first entry in the stream (or the second entry if the first entry in the stream is META-INF/ and the second entry is META-INF/MANIFEST.MF), or null otherwise.

Returns:Manifest

the Manifest for this JAR file, or null otherwise.

getNextEntryback to summary
public ZipEntry getNextEntry() throws IOException

Overrides java.util.zip.ZipInputStream.getNextEntry.

Reads the next ZIP file entry and positions the stream at the beginning of the entry data. If verification has been enabled, any invalid signature detected while positioning the stream for the next entry will result in an exception.

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP file error has occurred
SecurityException:
if any of the jar file entries are incorrectly signed.
getNextJarEntryback to summary
public JarEntry getNextJarEntry() throws IOException

Reads the next JAR file entry and positions the stream at the beginning of the entry data. If verification has been enabled, any invalid signature detected while positioning the stream for the next entry will result in an exception.

Returns:JarEntry

the next JAR file entry, or null if there are no more entries

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP file error has occurred
SecurityException:
if any of the jar file entries are incorrectly signed.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.util.zip.ZipInputStream.read.

Reads from the current JAR entry into an array of bytes, returning the number of inflated bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

If the current entry is compressed and this method returns a nonzero integer n then buf[off] through buf[off+n-1] contain the uncompressed data. The content of elements buf[off+n] through buf[off+len-1] is undefined, contrary to the specification of the InputStream superclass, so an implementation is free to modify these elements during the inflate operation. If this method returns -1 or throws an exception then the content of buf[off] through buf[off+len-1] is undefined.

If verification has been enabled, any invalid signature on the current entry will be reported at some point before the end of the entry is reached.

Parameters
b:byte[]

the buffer into which the data is read

off:int

the start offset in the destination array b

len:int

the maximum number of bytes to read

Returns:int

the actual number of bytes read, or -1 if the end of the entry is reached

Exceptions
IOException:
if an I/O error has occurred
IndexOutOfBoundsException:
If off is negative, len is negative, or len is greater than b.length - off
ZipException:
if a ZIP file error has occurred
SecurityException:
if any of the jar file entries are incorrectly signed.