Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the getInputStream method on EmbeddedJars to just use the Jar… #1422

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

package com.newrelic.agent.service.module;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public class EmbeddedJars {
private static final Map<String,String> EMBEDDED_FORMAT_TO_EXTENSION = getEmbeddedFormatToExtension("ear","war","jar");

Expand Down Expand Up @@ -46,36 +47,16 @@ public static InputStream getInputStream(URL url) throws IOException {
String path = url.toExternalForm().substring(index + entry.getKey().length());
// add 1 to skip past the `.` and the value length, which is the length of the file extension
url = new URL(url.toExternalForm().substring(0, index + 1 + entry.getValue().length()));
InputStream inputStream = url.openStream();
JarInputStream jarStream = new JarInputStream(inputStream);

if (!readToEntry(jarStream, path)) {
inputStream.close();
throw new IOException("Unable to open stream for " + path + " in " + url.toExternalForm());
}
return jarStream;
// For some reason, some JAR files cannot be read properly by JarInputStream, at least the getNextJatEntry methodI
meiao marked this conversation as resolved.
Show resolved Hide resolved
// perhaps related to entry order (https://bugs.openjdk.org/browse/JDK-8031748)
JarFile jarFile = new JarFile(url.getFile());
JarEntry innerEntry = jarFile.getJarEntry(path);
return jarFile.getInputStream(innerEntry);
}
}

return url.openStream();
}

/**
* Read a jar input stream until a given path is found.
*
* @param jarStream
* @param path
* @return true if the path was found
* @throws IOException
*/
private static boolean readToEntry(JarInputStream jarStream, String path) throws IOException {
for (JarEntry jarEntry = null; (jarEntry = jarStream.getNextJarEntry()) != null;) {
if (path.equals(jarEntry.getName())) {
return true;
}
}
return false;
}

static JarInputStream getJarInputStream(URL url) throws IOException {
boolean isEmbedded = isEmbedded(url);
Expand Down