Cleanup (formatting; code reuse)

This commit is contained in:
Roman Shevchenko
2015-10-27 19:19:11 +01:00
parent 2656dddf65
commit 44b8257cfd
@@ -18,17 +18,15 @@ package com.intellij.util.lang;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.reference.SoftReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
@@ -47,7 +45,7 @@ class JarLoader extends Loader {
pair(Resource.Attribute.IMPL_VENDOR, Attributes.Name.IMPLEMENTATION_VENDOR));
private final File myCanonicalFile;
private final boolean myCanLockJar; // true implies that the zipfile will not be modified in the lifetime of the JarLoader
private final boolean myCanLockJar; // true implies that the .jar file will not be modified in the lifetime of the JarLoader
private SoftReference<JarMemoryLoader> myMemoryLoader;
private volatile SoftReference<ZipFile> myZipFileSoftReference; // Used only when myCanLockJar==true
private final Map<Resource.Attribute, String> myAttributes;
@@ -119,10 +117,6 @@ class JarLoader extends Loader {
}
}
private static final AtomicInteger myGetResourceRequests = new AtomicInteger();
private static final AtomicLong myOpenTime = new AtomicLong();
private static final AtomicLong myCloseTime = new AtomicLong();
@Override
@Nullable
Resource getResource(String name, boolean flag) {
@@ -152,60 +146,38 @@ class JarLoader extends Loader {
}
protected void error(String message, Throwable t) {
//Logger.getLogger(JarLoader.class.getName()).log(Level.SEVERE, message, t);
Logger.getInstance(JarLoader.class).error(message, t);
}
private void releaseZipFile(ZipFile zipFile) throws IOException {
long started = System.nanoTime();
try {
// Closing of zip file when myCanLockJar=true happens in ZipFile.finalize
if (!myCanLockJar) {
zipFile.close();
}
} finally {
myCloseTime.addAndGet(System.nanoTime() - started);
}
}
private static final Object ourLock = new Object();
@NotNull
private ZipFile getZipFile() throws IOException {
@SuppressWarnings("unused") int requests = myGetResourceRequests.incrementAndGet();
// This code is executed at least 100K times (O(number of classes needed to load)) and it takes considerable time to open ZipFile's
// such number of times so we store reference to ZipFile if we allowed to lock the file (assume it isn't changed)
if (myCanLockJar) {
ZipFile zipFile = SoftReference.dereference(myZipFileSoftReference);
if (zipFile != null) return zipFile;
long started = System.nanoTime();
try {
synchronized (ourLock) {
zipFile = SoftReference.dereference(myZipFileSoftReference);
if (zipFile != null) return zipFile;
// This code is executed at least 100K times (O(number of classes needed to load)) and it takes considerable time to open ZipFile's
// such number of times so we store reference to ZipFile if we allowed to lock the file (assume it isn't changed)
if (myCanLockJar) {
SoftReference<ZipFile> zipFileSoftReference = myZipFileSoftReference;
if (zipFileSoftReference != null) {
ZipFile existingZipFile = zipFileSoftReference.get();
if (existingZipFile != null) return existingZipFile;
}
synchronized (ourLock) {
zipFileSoftReference = myZipFileSoftReference;
if (zipFileSoftReference != null) {
ZipFile existingZipFile = zipFileSoftReference.get();
if (existingZipFile != null) return existingZipFile;
}
// ZipFile's native implementation (ZipFile.c, zip_util.c) has path -> file descriptor cache
ZipFile zipFile = new ZipFile(myCanonicalFile);
myZipFileSoftReference = new SoftReference<ZipFile>(zipFile);
return zipFile;
}
// ZipFile's native implementation (ZipFile.c, zip_util.c) has path -> file descriptor cache
zipFile = new ZipFile(myCanonicalFile);
myZipFileSoftReference = new SoftReference<ZipFile>(zipFile);
return zipFile;
}
else {
return new ZipFile(myCanonicalFile);
}
} finally {
myOpenTime.addAndGet(System.nanoTime() - started);
//if (requests % 1000 == 0) {
// int factor = 1000000;
// System.out.println(
// "Jar loading :" + getClass().getClassLoader() + "," + requests + ", ot:" + (myOpenTime.get() / factor) + ", ct:" +
// (myCloseTime.get() / factor));
//}
}
else {
return new ZipFile(myCanonicalFile);
}
}
private void releaseZipFile(ZipFile zipFile) throws IOException {
// Closing of zip file when myCanLockJar=true happens in ZipFile.finalize
if (!myCanLockJar) {
zipFile.close();
}
}
@@ -213,6 +185,4 @@ class JarLoader extends Loader {
public String toString() {
return "JarLoader [" + myCanonicalFile + "]";
}
private static final Object ourLock = new Object();
}