Issue #4415 - Addressing Gzip Decoding of large files

+ Addressing PR review

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-12-12 11:58:44 -06:00
parent 2b58379f88
commit 1e810d370f
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 28 additions and 44 deletions

View File

@ -38,7 +38,7 @@ import org.eclipse.jetty.util.component.Destroyable;
public class GZIPContentDecoder implements Destroyable
{
// Unsigned Integer Max == 2^32
private static final long UINT_MAX = 0xffffffffL;
private static final long UINT_MAX = 0xFFFFFFFFL;
private final List<ByteBuffer> _inflateds = new ArrayList<>();
private final Inflater _inflater = new Inflater(true);

View File

@ -29,7 +29,6 @@ import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -363,7 +362,7 @@ public class GZIPContentDecoderTest
final long INT_MAX = Integer.MAX_VALUE;
// Unsigned Integer Max == 2^32
final long UINT_MAX = 0xffffffffL;
final long UINT_MAX = 0xFFFFFFFFL;
@ParameterizedTest
@ValueSource(longs = {INT_MAX, INT_MAX + 1, UINT_MAX, UINT_MAX + 1})
@ -383,10 +382,9 @@ public class GZIPContentDecoderTest
GZIPContentDecoder decoder = new GZIPContentDecoder(BUFSIZE);
try (GZIPDecoderOutputStream out = new GZIPDecoderOutputStream(decoder);
GZIPOutputStream outputStream = new GZIPOutputStream(out, BUFSIZE))
{
long writtenBytes = 0L;
GZIPDecoderOutputStream out = new GZIPDecoderOutputStream(decoder);
GZIPOutputStream outputStream = new GZIPOutputStream(out, BUFSIZE);
for (long bytesLeft = origSize; bytesLeft > 0; )
{
int len = buf.length;
@ -396,14 +394,15 @@ public class GZIPContentDecoderTest
}
outputStream.write(buf, 0, len);
bytesLeft -= len;
writtenBytes += len;
}
// Close GZIPOutputStream to have it generate gzip trailer.
// This can cause more writes of unflushed gzip buffers
outputStream.close();
assertThat("Written byte count", writtenBytes, is(origSize));
// out.decodedByteCount is only valid after close
assertThat("Decoded byte count", out.decodedByteCount, is(origSize));
}
}
public static class GZIPDecoderOutputStream extends OutputStream
{
@ -419,30 +418,9 @@ public class GZIPContentDecoderTest
public void write(byte[] b, int off, int len) throws IOException
{
ByteBuffer buf = ByteBuffer.wrap(b, off, len);
decode(buf);
}
@Override
public void write(byte[] b) throws IOException
while (buf.hasRemaining())
{
ByteBuffer buf = ByteBuffer.wrap(b);
decode(buf);
}
@Override
public void write(int b) throws IOException
{
ByteBuffer buf = BufferUtil.allocate(32);
buf.put((byte)(b & 0xFF));
buf.flip();
decode(buf);
}
private void decode(ByteBuffer buffer)
{
while (buffer.hasRemaining())
{
ByteBuffer decoded = decoder.decode(buffer);
ByteBuffer decoded = decoder.decode(buf);
if (decoded.hasRemaining())
{
decodedByteCount += decoded.remaining();
@ -450,5 +428,11 @@ public class GZIPContentDecoderTest
decoder.release(decoded);
}
}
@Override
public void write(int b) throws IOException
{
write(new byte[]{(byte)(b & 0xFF)}, 0, 1);
}
}
}