Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom 'dummy' byte addition for the odd inflater contract wi… #60

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,11 +18,13 @@
package org.apache.commons.compress.archivers.zip;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
Expand Down Expand Up @@ -474,28 +476,32 @@ public InputStream getInputStream(final ZipArchiveEntry ze)
if (!(ze instanceof Entry)) {
return null;
}
// cast valididty is checked just above
// cast validity is checked just above
ZipUtil.checkRequestedFeatures(ze);
final long start = ze.getDataOffset();

// doesn't get closed if the method is not supported - which
// should never happen because of the checkRequestedFeatures
// call above
final BoundedInputStream bis =
createBoundedInputStream(start, ze.getCompressedSize()); //NOSONAR
final InputStream buf = new BufferedInputStream(bis); //NOSONAR
final InputStream is =
new BufferedInputStream(createBoundedInputStream(start, ze.getCompressedSize())); //NOSONAR

switch (ZipMethod.getMethodByCode(ze.getMethod())) {
case STORED:
return bis;
return is;
case UNSHRINKING:
return new UnshrinkingInputStream(buf);
return new UnshrinkingInputStream(is);
case IMPLODING:
return new ExplodingInputStream(ze.getGeneralPurposeBit().getSlidingDictionarySize(),
ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), buf);
ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), is);
case DEFLATED:
bis.addDummy();
// Inflater with nowrap=true has this odd contract for a zero padding
// byte following the data stream; this used to be zlib's requirement
// and has been fixed a long time ago, but the contract persists so
// we comply.
final Inflater inflater = new Inflater(true);
return new InflaterInputStream(buf, inflater) {
final InputStream withDummy = new SequenceInputStream(is, new ByteArrayInputStream(new byte [] { 0 }));
return new InflaterInputStream(withDummy, inflater) {
@Override
public void close() throws IOException {
try {
Expand All @@ -506,9 +512,9 @@ public void close() throws IOException {
}
};
case BZIP2:
return new BZip2CompressorInputStream(buf);
return new BZip2CompressorInputStream(is);
case ENHANCED_DEFLATED:
return new Deflate64CompressorInputStream(buf);
return new Deflate64CompressorInputStream(is);
case AES_ENCRYPTED:
case EXPANDING_LEVEL_1:
case EXPANDING_LEVEL_2:
Expand Down Expand Up @@ -1100,7 +1106,6 @@ private class BoundedInputStream extends InputStream {
private ByteBuffer singleByteBuffer;
private final long end;
private long loc;
private boolean addDummy = false;

BoundedInputStream(final long start, final long remaining) {
this.end = start+remaining;
Expand All @@ -1114,10 +1119,6 @@ private class BoundedInputStream extends InputStream {
@Override
public synchronized int read() throws IOException {
if (loc >= end) {
if (loc == end && addDummy) {
addDummy = false;
return 0;
}
return -1;
}
if (singleByteBuffer == null) {
Expand All @@ -1142,11 +1143,6 @@ public synchronized int read(final byte[] b, final int off, int len) throws IOEx

if (len > end-loc) {
if (loc >= end) {
if (loc == end && addDummy) {
addDummy = false;
b[off] = 0;
return 1;
}
return -1;
}
len = (int)(end-loc);
Expand All @@ -1171,10 +1167,6 @@ protected int read(long pos, ByteBuffer buf) throws IOException {
buf.flip();
return read;
}

synchronized void addDummy() {
this.addDummy = true;
}
}

/**
Expand Down