Skip to content

Commit

Permalink
too aggressive on gzip guaging
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Jan 11, 2025
1 parent abd508d commit b9f3479
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/cedarsoftware/util/ByteUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static char convertDigit(final int value) {
* @return true if bytes are gzip compressed, false otherwise.
*/
public static boolean isGzipped(byte[] bytes) {
if (ArrayUtilities.size(bytes) < 18) { // minimum valid GZIP size
if (ArrayUtilities.size(bytes) < 2) { // minimum valid GZIP size
return false;
}
return bytes[0] == (byte) 0x1f && bytes[1] == (byte) 0x8b;
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/com/cedarsoftware/util/IOUtilitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Paths;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipException;

import org.junit.jupiter.api.Test;

Expand All @@ -30,6 +31,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -193,12 +195,9 @@ public void testUncompressBytesThatDontNeedUncompressed() throws Exception

@Test
public void testUncompressBytesWithException() throws Exception {
// Since there is less than 18 bytes, it is not a valid gzip file, so it will return the same bytes passed in.
byte[] bytes = IOUtilities.uncompressBytes(new byte[] {(byte)0x1f, (byte)0x8b, (byte)0x01});
assert bytes.length == 3;
assert bytes[0] == (byte) 0x1f;
assert bytes[1] == (byte) 0x8b;
assert bytes[2] == (byte) 0x01;
// Not a valid gzip byte stream, but starts with correct signature
Throwable t = assertThrows(RuntimeException.class, () -> IOUtilities.uncompressBytes(new byte[] {(byte)0x1f, (byte)0x8b, (byte)0x01}));
assert t.getCause() instanceof ZipException;
}

private ByteArrayOutputStream getUncompressedByteArray() throws IOException
Expand Down

0 comments on commit b9f3479

Please sign in to comment.