Skip to content

Commit

Permalink
8321620: Optimize JImage decompressors
Browse files Browse the repository at this point in the history
Reviewed-by: mchung, redestad
  • Loading branch information
Glavo authored and cl4es committed Jan 15, 2024
1 parent f5b757c commit a03eb6d
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,6 +25,7 @@
package jdk.internal.jimage.decompressor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Inflater;

/**
Expand All @@ -44,29 +45,33 @@ public String getName() {
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;
}
}

1 comment on commit a03eb6d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.