8321620: Optimize JImage decompressors

Reviewed-by: mchung, redestad
This commit is contained in:
Glavo 2024-01-15 16:10:35 +00:00 committed by Claes Redestad
parent f5b757ced6
commit a03eb6d3f6

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, 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
@ -25,6 +25,7 @@
package jdk.internal.jimage.decompressor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Inflater;
/**
@ -44,29 +45,33 @@ final class ZipDecompressor implements ResourceDecompressor {
return ZipDecompressorFactory.NAME;
}
static byte[] decompress(byte[] bytesIn, int offset) throws Exception {
static byte[] decompress(byte[] bytesIn, int offset, long originalSize) throws Exception {
if (originalSize > Integer.MAX_VALUE) {
throw new OutOfMemoryError("Required array size too large");
}
byte[] bytesOut = new byte[(int) originalSize];
Inflater inflater = new Inflater();
inflater.setInput(bytesIn, offset, bytesIn.length - offset);
ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length - offset);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
stream.write(buffer, 0, count);
int count = 0;
while (!inflater.finished() && count < originalSize) {
count += inflater.inflate(bytesOut, count, bytesOut.length - count);
}
stream.close();
byte[] bytesOut = stream.toByteArray();
inflater.end();
if (count != originalSize) {
throw new IOException("Resource content size mismatch");
}
return bytesOut;
}
@Override
public byte[] decompress(StringsProvider reader, byte[] content, int offset,
long originalSize) throws Exception {
byte[] decompressed = decompress(content, offset);
byte[] decompressed = decompress(content, offset, originalSize);
return decompressed;
}
}