提交 45f1d9f4 编写于 作者: S sherman

7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)

Summary: The returned InflatedInputStream object should be kept in map streams
Reviewed-by: alanb
上级 4856d8f9
...@@ -315,7 +315,7 @@ class ZipFile implements ZipConstants, Closeable { ...@@ -315,7 +315,7 @@ class ZipFile implements ZipConstants, Closeable {
private static native void freeEntry(long jzfile, long jzentry); private static native void freeEntry(long jzfile, long jzentry);
// the outstanding inputstreams that need to be closed. // the outstanding inputstreams that need to be closed.
private Set<ZipFileInputStream> streams = new HashSet<ZipFileInputStream>(); private Set<InputStream> streams = new HashSet<>();
/** /**
* Returns an input stream for reading the contents of the specified * Returns an input stream for reading the contents of the specified
...@@ -348,55 +348,58 @@ class ZipFile implements ZipConstants, Closeable { ...@@ -348,55 +348,58 @@ class ZipFile implements ZipConstants, Closeable {
return null; return null;
} }
in = new ZipFileInputStream(jzentry); in = new ZipFileInputStream(jzentry);
streams.add(in);
} switch (getEntryMethod(jzentry)) {
final ZipFileInputStream zfin = in; case STORED:
switch (getEntryMethod(jzentry)) { streams.add(in);
case STORED: return in;
return zfin; case DEFLATED:
case DEFLATED: final ZipFileInputStream zfin = in;
// MORE: Compute good size for inflater stream: // MORE: Compute good size for inflater stream:
long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
if (size > 65536) size = 8192; if (size > 65536) size = 8192;
if (size <= 0) size = 4096; if (size <= 0) size = 4096;
return new InflaterInputStream(zfin, getInflater(), (int)size) { InputStream is = new InflaterInputStream(zfin, getInflater(), (int)size) {
private boolean isClosed = false; private boolean isClosed = false;
public void close() throws IOException { public void close() throws IOException {
if (!isClosed) { if (!isClosed) {
releaseInflater(inf); super.close();
this.in.close(); releaseInflater(inf);
isClosed = true; isClosed = true;
}
} }
} // Override fill() method to provide an extra "dummy" byte
// Override fill() method to provide an extra "dummy" byte // at the end of the input stream. This is required when
// at the end of the input stream. This is required when // using the "nowrap" Inflater option.
// using the "nowrap" Inflater option. protected void fill() throws IOException {
protected void fill() throws IOException { if (eof) {
if (eof) { throw new EOFException(
throw new EOFException( "Unexpected end of ZLIB input stream");
"Unexpected end of ZLIB input stream"); }
len = this.in.read(buf, 0, buf.length);
if (len == -1) {
buf[0] = 0;
len = 1;
eof = true;
}
inf.setInput(buf, 0, len);
} }
len = this.in.read(buf, 0, buf.length); private boolean eof;
if (len == -1) {
buf[0] = 0; public int available() throws IOException {
len = 1; if (isClosed)
eof = true; return 0;
long avail = zfin.size() - inf.getBytesWritten();
return avail > (long) Integer.MAX_VALUE ?
Integer.MAX_VALUE : (int) avail;
} }
inf.setInput(buf, 0, len); };
} streams.add(is);
private boolean eof; return is;
default:
public int available() throws IOException { throw new ZipException("invalid compression method");
if (isClosed) }
return 0;
long avail = zfin.size() - inf.getBytesWritten();
return avail > (long) Integer.MAX_VALUE ?
Integer.MAX_VALUE : (int) avail;
}
};
default:
throw new ZipException("invalid compression method");
} }
} }
...@@ -539,9 +542,9 @@ class ZipFile implements ZipConstants, Closeable { ...@@ -539,9 +542,9 @@ class ZipFile implements ZipConstants, Closeable {
closeRequested = true; closeRequested = true;
if (streams.size() !=0) { if (streams.size() !=0) {
Set<ZipFileInputStream> copy = streams; Set<InputStream> copy = streams;
streams = new HashSet<ZipFileInputStream>(); streams = new HashSet<InputStream>();
for (ZipFileInputStream is: copy) for (InputStream is: copy)
is.close(); is.close();
} }
......
/*
* Copyright (c) 2010, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 7003462
@summary Make sure cached Inflater does not get finalized.
*/
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FinalizeInflater {
public static void main(String[] args) throws Throwable {
try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), "input.zip")))
{
ZipEntry ze = zf.getEntry("ReadZip.java");
read(zf.getInputStream(ze));
System.gc();
System.runFinalization();
System.gc();
// read again
read(zf.getInputStream(ze));
}
}
private static void read(InputStream is)
throws IOException
{
Wrapper wrapper = new Wrapper(is);
byte[] buffer = new byte[32];
try {
while(is.read(buffer)>0){}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
static class Wrapper{
InputStream is;
public Wrapper(InputStream is) {
this.is = is;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
is.close();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册