Skip to content

Commit

Permalink
8282648: Weaken the InflaterInputStream specification in order to all…
Browse files Browse the repository at this point in the history
…ow faster Zip implementations

Reviewed-by: lancea, alanb, jpai, mr, darcy
  • Loading branch information
simonis committed Sep 5, 2022
1 parent e31c537 commit 2c61efe
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/java.base/share/classes/java/net/URLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,12 @@ public Permission getPermission() throws IOException {
* returned input stream if the read timeout expires before data
* is available for read.
*
* @apiNote The {@code InputStream} returned by this method can wrap an
* {@link java.util.zip.InflaterInputStream InflaterInputStream}, whose
* {@link java.util.zip.InflaterInputStream#read(byte[], int, int)
* read(byte[], int, int)} method can modify any element of the output
* buffer.
*
* @return an input stream that reads from this open connection.
* @throws IOException if an I/O error occurs while
* creating the input stream.
Expand Down
7 changes: 7 additions & 0 deletions src/java.base/share/classes/java/util/jar/JarFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,13 @@ private byte[] getBytes(ZipEntry ze) throws IOException {
/**
* Returns an input stream for reading the contents of the specified
* zip file entry.
*
* @apiNote The {@code InputStream} returned by this method can wrap an
* {@link java.util.zip.InflaterInputStream InflaterInputStream}, whose
* {@link java.util.zip.InflaterInputStream#read(byte[], int, int)
* read(byte[], int, int)} method can modify any element of the output
* buffer.
*
* @param ze the zip file entry
* @return an input stream for reading the contents of the specified
* zip file entry or null if the zip file entry does not exist
Expand Down
19 changes: 15 additions & 4 deletions src/java.base/share/classes/java/util/jar/JarInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,21 @@ public JarEntry getNextJarEntry() throws IOException {
}

/**
* Reads from the current JAR file entry into an array of bytes.
* If {@code len} is not zero, the method
* blocks until some input is available; otherwise, no
* bytes are read and {@code 0} is returned.
* Reads from the current JAR entry into an array of bytes, returning the number of
* inflated bytes. If {@code len} is not zero, the method blocks until some input is
* available; otherwise, no bytes are read and {@code 0} is returned.
* <p>
* If the current entry is compressed and this method returns a nonzero
* integer <i>n</i> then {@code buf[off]}
* through {@code buf[off+}<i>n</i>{@code -1]} contain the uncompressed
* data. The content of elements {@code buf[off+}<i>n</i>{@code ]} through
* {@code buf[off+}<i>len</i>{@code -1]} is undefined, contrary to the
* specification of the {@link java.io.InputStream InputStream} superclass,
* so an implementation is free to modify these elements during the inflate
* operation. If this method returns {@code -1} or throws an exception then
* the content of {@code buf[off]} through {@code buf[off+}<i>len</i>{@code
* -1]} is undefined.
* <p>
* 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.
Expand Down
23 changes: 17 additions & 6 deletions src/java.base/share/classes/java/util/zip/GZIPInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -92,13 +92,24 @@ public GZIPInputStream(InputStream in) throws IOException {
}

/**
* Reads uncompressed data into an array of bytes. If {@code len} is not
* zero, the method will block until some input can be decompressed; otherwise,
* no bytes are read and {@code 0} is returned.
* Reads uncompressed data into an array of bytes, returning the number of inflated
* bytes. If {@code len} is not zero, the method will block until some input can be
* decompressed; otherwise, no bytes are read and {@code 0} is returned.
* <p>
* If this method returns a nonzero integer <i>n</i> then {@code buf[off]}
* through {@code buf[off+}<i>n</i>{@code -1]} contain the uncompressed
* data. The content of elements {@code buf[off+}<i>n</i>{@code ]} through
* {@code buf[off+}<i>len</i>{@code -1]} is undefined, contrary to the
* specification of the {@link java.io.InputStream InputStream} superclass,
* so an implementation is free to modify these elements during the inflate
* operation. If this method returns {@code -1} or throws an exception then
* the content of {@code buf[off]} through {@code buf[off+}<i>len</i>{@code
* -1]} is undefined.
*
* @param buf the buffer into which the data is read
* @param off the start offset in the destination array {@code b}
* @param off the start offset in the destination array {@code buf}
* @param len the maximum number of bytes read
* @return the actual number of bytes read, or -1 if the end of the
* @return the actual number of bytes inflated, or -1 if the end of the
* compressed input stream is reached
*
* @throws NullPointerException If {@code buf} is {@code null}.
Expand Down
19 changes: 15 additions & 4 deletions src/java.base/share/classes/java/util/zip/InflaterInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,24 @@ public int read() throws IOException {
}

/**
* Reads uncompressed data into an array of bytes. If {@code len} is not
* zero, the method will block until some input can be decompressed; otherwise,
* no bytes are read and {@code 0} is returned.
* Reads uncompressed data into an array of bytes, returning the number of inflated
* bytes. If {@code len} is not zero, the method will block until some input can be
* decompressed; otherwise, no bytes are read and {@code 0} is returned.
* <p>
* If this method returns a nonzero integer <i>n</i> then {@code buf[off]}
* through {@code buf[off+}<i>n</i>{@code -1]} contain the uncompressed
* data. The content of elements {@code buf[off+}<i>n</i>{@code ]} through
* {@code buf[off+}<i>len</i>{@code -1]} is undefined, contrary to the
* specification of the {@link java.io.InputStream InputStream} superclass,
* so an implementation is free to modify these elements during the inflate
* operation. If this method returns {@code -1} or throws an exception then
* the content of {@code buf[off]} through {@code buf[off+}<i>len</i>{@code
* -1]} is undefined.
*
* @param b the buffer into which the data is read
* @param off the start offset in the destination array {@code b}
* @param len the maximum number of bytes read
* @return the actual number of bytes read, or -1 if the end of the
* @return the actual number of bytes inflated, or -1 if the end of the
* compressed input is reached or a preset dictionary is needed
* @throws NullPointerException If {@code b} is {@code null}.
* @throws IndexOutOfBoundsException If {@code off} is negative,
Expand Down
6 changes: 6 additions & 0 deletions src/java.base/share/classes/java/util/zip/ZipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ public ZipEntry getEntry(String name) {
* Closing this ZIP file will, in turn, close all input streams that
* have been returned by invocations of this method.
*
* @apiNote The {@code InputStream} returned by this method can wrap an
* {@link java.util.zip.InflaterInputStream InflaterInputStream}, whose
* {@link java.util.zip.InflaterInputStream#read(byte[], int, int)
* read(byte[], int, int)} method can modify any element of the output
* buffer.
*
* @param entry the zip file entry
* @return the input stream for reading the contents of the specified
* zip file entry or null if the zip file entry does not exist
Expand Down
21 changes: 16 additions & 5 deletions src/java.base/share/classes/java/util/zip/ZipInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -165,10 +165,21 @@ public int available() throws IOException {
}

/**
* Reads from the current ZIP entry into an array of bytes.
* If {@code len} is not zero, the method
* blocks until some input is available; otherwise, no
* bytes are read and {@code 0} is returned.
* Reads from the current ZIP entry into an array of bytes, returning the number of
* inflated bytes. If {@code len} is not zero, the method blocks until some input is
* available; otherwise, no bytes are read and {@code 0} is returned.
* <p>
* If the current entry is compressed and this method returns a nonzero
* integer <i>n</i> then {@code buf[off]}
* through {@code buf[off+}<i>n</i>{@code -1]} contain the uncompressed
* data. The content of elements {@code buf[off+}<i>n</i>{@code ]} through
* {@code buf[off+}<i>len</i>{@code -1]} is undefined, contrary to the
* specification of the {@link java.io.InputStream InputStream} superclass,
* so an implementation is free to modify these elements during the inflate
* operation. If this method returns {@code -1} or throws an exception then
* the content of {@code buf[off]} through {@code buf[off+}<i>len</i>{@code
* -1]} is undefined.
*
* @param b the buffer into which the data is read
* @param off the start offset in the destination array {@code b}
* @param len the maximum number of bytes read
Expand Down

1 comment on commit 2c61efe

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.