Closeable
, AutoCloseable
public class JarInputStream extends ZipInputStream
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. 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.
JarInputStream
verifies the signatures of entries in a Signed JAR file when: Manifest
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
). Manifest
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.
JarEntry
is modified after the JAR file is signed, a SecurityException
will be thrown when the entry is read.Modifier and Type | Field | Description |
---|---|---|
static final int |
CENATT |
Central directory (CEN) header internal file attributes field offset. |
static final int |
CENATX |
Central directory (CEN) header external file attributes field offset. |
static final int |
CENCOM |
Central directory (CEN) header comment length field offset. |
static final int |
CENCRC |
Central directory (CEN) header uncompressed file crc-32 value field offset. |
static final int |
CENDSK |
Central directory (CEN) header disk number start field offset. |
static final int |
CENEXT |
Central directory (CEN) header extra field length field offset. |
static final int |
CENFLG |
Central directory (CEN) header encrypt, decrypt flags field offset. |
static final int |
CENHDR |
Central directory (CEN) header size in bytes (including signature). |
static final int |
CENHOW |
Central directory (CEN) header compression method field offset. |
static final int |
CENLEN |
Central directory (CEN) header uncompressed size field offset. |
static final int |
CENNAM |
Central directory (CEN) header filename length field offset. |
static final int |
CENOFF |
Central directory (CEN) header LOC header offset field offset. |
static final long |
CENSIG |
Central directory (CEN) header signature. |
static final int |
CENSIZ |
Central directory (CEN) header compressed size field offset. |
static final int |
CENTIM |
Central directory (CEN) header modification time field offset. |
static final int |
CENVEM |
Central directory (CEN) header version made by field offset. |
static final int |
CENVER |
Central directory (CEN) header version needed to extract field offset. |
static final int |
ENDCOM |
End of central directory (END) header zip file comment length field offset. |
static final int |
ENDHDR |
End of central directory (END) header size in bytes (including signature). |
static final int |
ENDOFF |
End of central directory (END) header offset for the first CEN header field offset. |
static final long |
ENDSIG |
End of central directory (END) header signature. |
static final int |
ENDSIZ |
End of central directory (END) header central directory size in bytes field offset. |
static final int |
ENDSUB |
End of central directory (END) header number of entries on this disk field offset. |
static final int |
ENDTOT |
End of central directory (END) header total number of entries field offset. |
static final int |
EXTCRC |
Extra local (EXT) header uncompressed file crc-32 value field offset. |
static final int |
EXTHDR |
Extra local (EXT) header size in bytes (including signature). |
static final int |
EXTLEN |
Extra local (EXT) header uncompressed size field offset. |
static final long |
EXTSIG |
Extra local (EXT) header signature. |
static final int |
EXTSIZ |
Extra local (EXT) header compressed size field offset. |
static final int |
LOCCRC |
Local file (LOC) header uncompressed file crc-32 value field offset. |
static final int |
LOCEXT |
Local file (LOC) header extra field length field offset. |
static final int |
LOCFLG |
Local file (LOC) header general purpose bit flag field offset. |
static final int |
LOCHDR |
Local file (LOC) header size in bytes (including signature). |
static final int |
LOCHOW |
Local file (LOC) header compression method field offset. |
static final int |
LOCLEN |
Local file (LOC) header uncompressed size field offset. |
static final int |
LOCNAM |
Local file (LOC) header filename length field offset. |
static final long |
LOCSIG |
Local file (LOC) header signature. |
static final int |
LOCSIZ |
Local file (LOC) header compressed size field offset. |
static final int |
LOCTIM |
Local file (LOC) header modification time field offset. |
static final int |
LOCVER |
Local file (LOC) header version needed to extract field offset. |
buf, inf, len
in
Constructor | Description |
---|---|
JarInputStream |
Creates a new JarInputStream and reads the optional manifest. |
JarInputStream |
Creates a new JarInputStream and reads the optional manifest. |
Modifier and Type | Method | Description |
---|---|---|
protected ZipEntry |
createZipEntry |
Creates a new JarEntry (ZipEntry ) for the specified JAR file entry name. |
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. |
ZipEntry |
getNextEntry() |
Reads the next ZIP file entry and positions the stream at the beginning of the entry data. |
JarEntry |
getNextJarEntry() |
Reads the next JAR file entry and positions the stream at the beginning of the entry data. |
int |
read |
Reads from the current JAR entry into an array of bytes, returning the number of inflated bytes. |
available, close, closeEntry, read, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
fill, mark, markSupported, reset
read
nullInputStream
static final long LOCSIG
static final long EXTSIG
static final long CENSIG
static final long ENDSIG
static final int LOCHDR
static final int EXTHDR
static final int CENHDR
static final int ENDHDR
static final int LOCVER
static final int LOCFLG
static final int LOCHOW
static final int LOCTIM
static final int LOCCRC
static final int LOCSIZ
static final int LOCLEN
static final int LOCNAM
static final int LOCEXT
static final int EXTCRC
static final int EXTSIZ
static final int EXTLEN
static final int CENVEM
static final int CENVER
static final int CENFLG
static final int CENHOW
static final int CENTIM
static final int CENCRC
static final int CENSIZ
static final int CENLEN
static final int CENNAM
static final int CENEXT
static final int CENCOM
static final int CENDSK
static final int CENATT
static final int CENATX
static final int CENOFF
static final int ENDSUB
static final int ENDTOT
static final int ENDSIZ
static final int ENDOFF
static final int ENDCOM
public JarInputStream(InputStream in) throws IOException
JarInputStream
and reads the optional manifest. If a manifest is present, also attempts to verify the signatures if the JarInputStream is signed.in
- the actual input streamIOException
- if an I/O error has occurredpublic JarInputStream(InputStream in, boolean verify) throws IOException
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.in
- the actual input streamverify
- whether or not to verify the JarInputStream if it is signed.IOException
- if an I/O error has occurredpublic Manifest getManifest()
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.Manifest
for this JAR file, or null
otherwise.public ZipEntry getNextEntry() throws IOException
getNextEntry
in class ZipInputStream
ZipException
- if a ZIP file error has occurredIOException
- if an I/O error has occurredSecurityException
- if any of the jar file entries are incorrectly signed.public JarEntry getNextJarEntry() throws IOException
ZipException
- if a ZIP file error has occurredIOException
- if an I/O error has occurredSecurityException
- if any of the jar file entries are incorrectly signed.public int read(byte[] b, int off, int len) throws IOException
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.
read
in class ZipInputStream
b
- the buffer into which the data is readoff
- the start offset in the destination array b
len
- the maximum number of bytes to readNullPointerException
- If b
is null
.IndexOutOfBoundsException
- If off
is negative, len
is negative, or len
is greater than b.length - off
ZipException
- if a ZIP file error has occurredIOException
- if an I/O error has occurredSecurityException
- if any of the jar file entries are incorrectly signed.protected ZipEntry createZipEntry(String name)
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
.createZipEntry
in class ZipInputStream
name
- the name of the JAR/ZIP file entryJarEntry
object just created
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/jar/JarInputStream.html