[platform] Avoid excessive querying of the size of archives

GitOrigin-RevId: 553df0909fcbbfb6e2b343f535f71fec1cb356a2
This commit is contained in:
Konstantin.Nisht
2024-12-06 14:53:59 +00:00
committed by intellij-monorepo-bot
parent 02f5dabc31
commit 8a6703d5fa
2 changed files with 22 additions and 5 deletions
@@ -343,7 +343,7 @@ public class JBZipEntry {
long start = calcDataOffset();
long size = getCompressedSize();
myFile.ensureFlushed(start + size);
if (myFile.myArchive.size() < start + size) {
if (myFile.getSize() < start + size) {
throw new EOFException();
}
BoundedInputStream bis = new BoundedInputStream(start, size);
@@ -92,7 +92,8 @@ public class JBZipFile implements Closeable {
private JBZipOutputStream myOutputStream;
private long currentCfdOffset;
private final boolean myIsReadonly;
final boolean myIsReadonly;
private final long mySize;
/**
@@ -197,10 +198,17 @@ public class JBZipFile implements Closeable {
public JBZipFile(@NotNull SeekableByteChannel channel, @NotNull Charset encoding, boolean readonly, @NotNull ThreeState isZip64) throws IOException {
myEncoding = encoding;
myIsReadonly = readonly;
long channelSize = channel.size();
if (readonly) {
mySize = channelSize;
}
else {
mySize = -1;
}
myArchive = channel;
try {
if (myArchive.size() > 0) {
if (channelSize > 0) {
populateFromCentralDirectory(isZip64);
}
else {
@@ -326,7 +334,7 @@ public class JBZipFile implements Closeable {
instead gaining a performance boost.
*/
ByteBuffer centralDirectoryCached = ByteBuffer.allocate(
Math.min((int)(myArchive.size() - myArchive.position()),
Math.min((int)(getSize() - myArchive.position()),
// Sometimes the size of the central directory may be significant -- up to 3 megabytes.
64 * 1024) // seems enough
);
@@ -573,7 +581,7 @@ public class JBZipFile implements Closeable {
*/
private void positionAtCentralDirectory(@NotNull ThreeState isZip64) throws IOException {
boolean found = false;
long off = myArchive.size() - MIN_EOCD_SIZE;
long off = getSize() - MIN_EOCD_SIZE;
if (off >= 0) {
myArchive.position(off);
byte[] sig = JBZipOutputStream.EOCD_SIG;
@@ -720,4 +728,13 @@ public class JBZipFile implements Closeable {
void ensureFlushed(long end) throws IOException {
if (myOutputStream != null) myOutputStream.ensureFlushed(end);
}
long getSize() throws IOException {
if (mySize == -1) {
return myArchive.size();
}
else {
return mySize;
}
}
}