Skip to content

Commit

Permalink
Improve handling of classpath index file
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Nov 19, 2020
1 parent ef6114f commit e8638a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.bootstrap.runner;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -35,17 +36,18 @@ private static void doRun(Object args) throws IOException, ClassNotFoundExceptio
} else if (Boolean.getBoolean("quarkus.launch.rebuild")) {
doReaugment(appRoot);
} else {
SerializedApplication app = null;
try (InputStream in = Files.newInputStream(appRoot.resolve(QUARKUS_APPLICATION_DAT))) {
SerializedApplication app;
// the magic number here is close to the smallest possible dat file
try (InputStream in = new BufferedInputStream(Files.newInputStream(appRoot.resolve(QUARKUS_APPLICATION_DAT)),
24_576)) {
app = SerializedApplication.read(in, appRoot);
}
try {
Thread.currentThread().setContextClassLoader(app.getRunnerClassLoader());
Class<?> mainClass = app.getRunnerClassLoader().loadClass(app.getMainClass());
mainClass.getMethod("main", String[].class).invoke(null, args);

} finally {
if (app != null) {
app.getRunnerClassLoader().close();
}
app.getRunnerClassLoader().close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void write(OutputStream outputStream, String mainClass, Path appli
data.writeInt(MAGIC);
data.writeInt(VERSION);
data.writeUTF(mainClass);
data.writeInt(classPath.size());
data.writeShort(classPath.size());
Map<String, List<Integer>> directlyIndexedResourcesToCPJarIndex = new HashMap<>();
for (int i = 0; i < classPath.size(); i++) {
Path jar = classPath.get(i);
Expand All @@ -79,20 +79,20 @@ public static void write(OutputStream outputStream, String mainClass, Path appli
for (Path jar : parentFirst) {
collectPackages(jar, parentFirstPackages);
}
data.writeInt(parentFirstPackages.size());
data.writeShort(parentFirstPackages.size());
for (String p : parentFirstPackages) {
data.writeUTF(p.replace("/", ".").replace("\\", "."));
}
data.writeInt(nonExistentResources.size());
data.writeShort(nonExistentResources.size());
for (String nonExistentResource : nonExistentResources) {
data.writeUTF(nonExistentResource);
}
data.writeInt(directlyIndexedResourcesToCPJarIndex.size());
data.writeShort(directlyIndexedResourcesToCPJarIndex.size());
for (Map.Entry<String, List<Integer>> entry : directlyIndexedResourcesToCPJarIndex.entrySet()) {
data.writeUTF(entry.getKey());
data.writeInt(entry.getValue().size());
data.writeShort(entry.getValue().size());
for (Integer index : entry.getValue()) {
data.writeInt(index);
data.writeShort(index);
}
}
data.flush();
Expand All @@ -110,7 +110,7 @@ public static SerializedApplication read(InputStream inputStream, Path appRoot)
String mainClass = in.readUTF();
Map<String, ClassLoadingResource[]> resourceDirectoryMap = new HashMap<>();
Set<String> parentFirstPackages = new HashSet<>();
int numPaths = in.readInt();
int numPaths = in.readUnsignedShort();
ClassLoadingResource[] allClassLoadingResources = new ClassLoadingResource[numPaths];
for (int pathCount = 0; pathCount < numPaths; pathCount++) {
String path = in.readUTF();
Expand All @@ -122,7 +122,7 @@ public static SerializedApplication read(InputStream inputStream, Path appRoot)
}
JarResource resource = new JarResource(info, appRoot.resolve(path));
allClassLoadingResources[pathCount] = resource;
int numDirs = in.readInt();
int numDirs = in.readUnsignedShort();
for (int i = 0; i < numDirs; ++i) {
String dir = in.readUTF();
ClassLoadingResource[] existing = resourceDirectoryMap.get(dir);
Expand All @@ -136,25 +136,25 @@ public static SerializedApplication read(InputStream inputStream, Path appRoot)
}
}
}
int packages = in.readInt();
int packages = in.readUnsignedShort();
for (int i = 0; i < packages; ++i) {
parentFirstPackages.add(in.readUTF());
}
Set<String> nonExistentResources = new HashSet<>();
int nonExistentResourcesSize = in.readInt();
int nonExistentResourcesSize = in.readUnsignedShort();
for (int i = 0; i < nonExistentResourcesSize; i++) {
nonExistentResources.add(in.readUTF());
}
// this map is populated correctly because the JarResource entries are added to allClassLoadingResources
// in the same order as the classpath was written during the writing of the index
Map<String, ClassLoadingResource[]> directlyIndexedResourcesIndexMap = new HashMap<>();
int directlyIndexedSize = in.readInt();
int directlyIndexedSize = in.readUnsignedShort();
for (int i = 0; i < directlyIndexedSize; i++) {
String resource = in.readUTF();
int indexesSize = in.readInt();
int indexesSize = in.readUnsignedShort();
ClassLoadingResource[] matchingResources = new ClassLoadingResource[indexesSize];
for (int j = 0; j < indexesSize; j++) {
matchingResources[j] = allClassLoadingResources[in.readInt()];
matchingResources[j] = allClassLoadingResources[in.readUnsignedShort()];
}
directlyIndexedResourcesIndexMap.put(resource, matchingResources);
}
Expand Down Expand Up @@ -242,7 +242,7 @@ private static List<String> writeJar(DataOutputStream out, Path jar) throws IOEx
if (hasDefaultPackage) {
dirs.add("");
}
out.writeInt(dirs.size());
out.writeShort(dirs.size());
for (String i : dirs) {
out.writeUTF(i);
}
Expand Down

0 comments on commit e8638a8

Please sign in to comment.