use of java.util.jar.JarInputStream in project hbase by apache.
the class ClassFinder method findClassesFromJar.
private Set<Class<?>> findClassesFromJar(String jarFileName, String packageName, boolean proceedOnExceptions) throws IOException, ClassNotFoundException, LinkageError {
JarInputStream jarFile = null;
try {
jarFile = new JarInputStream(new FileInputStream(jarFileName));
} catch (IOException ioEx) {
LOG.warn("Failed to look for classes in " + jarFileName + ": " + ioEx);
throw ioEx;
}
Set<Class<?>> classes = new HashSet<>();
JarEntry entry = null;
try {
while (true) {
try {
entry = jarFile.getNextJarEntry();
} catch (IOException ioEx) {
if (!proceedOnExceptions) {
throw ioEx;
}
LOG.warn("Failed to get next entry from " + jarFileName + ": " + ioEx);
break;
}
if (entry == null) {
// loop termination condition
break;
}
String className = entry.getName();
if (!className.endsWith(CLASS_EXT)) {
continue;
}
int ix = className.lastIndexOf('/');
String fileName = (ix >= 0) ? className.substring(ix + 1) : className;
if (null != this.fileNameFilter && !this.fileNameFilter.isCandidateFile(fileName, className)) {
continue;
}
className = className.substring(0, className.length() - CLASS_EXT.length()).replace('/', '.');
if (!className.startsWith(packageName)) {
continue;
}
Class<?> c = makeClass(className, proceedOnExceptions);
if (c != null) {
if (!classes.add(c)) {
LOG.warn("Ignoring duplicate class " + className);
}
}
}
return classes;
} finally {
jarFile.close();
}
}
use of java.util.jar.JarInputStream in project MinecraftForge by MinecraftForge.
the class ClassPatchManager method setup.
public void setup(Side side) {
Pattern binpatchMatcher = Pattern.compile(String.format("binpatch/%s/.*.binpatch", side.toString().toLowerCase(Locale.ENGLISH)));
JarInputStream jis;
try {
InputStream binpatchesCompressed = getClass().getResourceAsStream("/binpatches.pack.lzma");
if (binpatchesCompressed == null) {
FMLRelaunchLog.log(Level.ERROR, "The binary patch set is missing. Either you are in a development environment, or things are not going to work!");
return;
}
LzmaInputStream binpatchesDecompressed = new LzmaInputStream(binpatchesCompressed);
ByteArrayOutputStream jarBytes = new ByteArrayOutputStream();
JarOutputStream jos = new JarOutputStream(jarBytes);
Pack200.newUnpacker().unpack(binpatchesDecompressed, jos);
jis = new JarInputStream(new ByteArrayInputStream(jarBytes.toByteArray()));
} catch (Exception e) {
FMLRelaunchLog.log(Level.ERROR, e, "Error occurred reading binary patches. Expect severe problems!");
throw Throwables.propagate(e);
}
patches = ArrayListMultimap.create();
do {
try {
JarEntry entry = jis.getNextJarEntry();
if (entry == null) {
break;
}
if (binpatchMatcher.matcher(entry.getName()).matches()) {
ClassPatch cp = readPatch(entry, jis);
if (cp != null) {
patches.put(cp.sourceClassName, cp);
}
} else {
jis.closeEntry();
}
} catch (IOException e) {
}
} while (true);
FMLRelaunchLog.fine("Read %d binary patches", patches.size());
if (DEBUG)
FMLRelaunchLog.fine("Patch list :\n\t%s", Joiner.on("\t\n").join(patches.asMap().entrySet()));
patchedClasses.clear();
}
use of java.util.jar.JarInputStream in project buck by facebook.
the class HashingDeterministicJarWriterTest method entriesAreWrittenAsTheyAreEncounteredWithManifestLast.
@Test
public void entriesAreWrittenAsTheyAreEncounteredWithManifestLast() throws IOException {
writer.writeEntry("Z", new ByteArrayInputStream("Z's contents".getBytes(StandardCharsets.UTF_8)));
writer.writeEntry("A", new ByteArrayInputStream("A's contents".getBytes(StandardCharsets.UTF_8)));
writer.close();
try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
JarEntry entry;
entry = jar.getNextJarEntry();
assertEquals("Z", entry.getName());
entry = jar.getNextJarEntry();
assertEquals("A", entry.getName());
entry = jar.getNextJarEntry();
assertEquals(JarFile.MANIFEST_NAME, entry.getName());
}
}
use of java.util.jar.JarInputStream in project buck by facebook.
the class FakeProjectFilesystem method getJarManifest.
@Override
public Manifest getJarManifest(Path path) throws IOException {
try (JarInputStream jar = new JarInputStream(newFileInputStream(path))) {
Manifest result = jar.getManifest();
if (result != null) {
return result;
}
// JarInputStream will only find the manifest if it's the first entry, but we have code that
// puts it elsewhere. We must search. Fortunately, this is test code! So we can be slow!
JarEntry entry;
while ((entry = jar.getNextJarEntry()) != null) {
if (JarFile.MANIFEST_NAME.equals(entry.getName())) {
result = new Manifest();
result.read(jar);
return result;
}
}
}
return null;
}
use of java.util.jar.JarInputStream in project mybatis-3 by mybatis.
the class DefaultVFS method list.
@Override
public List<String> list(URL url, String path) throws IOException {
InputStream is = null;
try {
List<String> resources = new ArrayList<String>();
// First, try to find the URL of a JAR file containing the requested resource. If a JAR
// file is found, then we'll list child resources by reading the JAR.
URL jarUrl = findJarForResource(url);
if (jarUrl != null) {
is = jarUrl.openStream();
if (log.isDebugEnabled()) {
log.debug("Listing " + url);
}
resources = listResources(new JarInputStream(is), path);
} else {
List<String> children = new ArrayList<String>();
try {
if (isJar(url)) {
// Some versions of JBoss VFS might give a JAR stream even if the resource
// referenced by the URL isn't actually a JAR
is = url.openStream();
JarInputStream jarInput = new JarInputStream(is);
if (log.isDebugEnabled()) {
log.debug("Listing " + url);
}
for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null; ) {
if (log.isDebugEnabled()) {
log.debug("Jar entry: " + entry.getName());
}
children.add(entry.getName());
}
jarInput.close();
} else {
/*
* Some servlet containers allow reading from directory resources like a
* text file, listing the child resources one per line. However, there is no
* way to differentiate between directory and file resources just by reading
* them. To work around that, as each line is read, try to look it up via
* the class loader as a child of the current resource. If any line fails
* then we assume the current resource is not a directory.
*/
is = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
List<String> lines = new ArrayList<String>();
for (String line; (line = reader.readLine()) != null; ) {
if (log.isDebugEnabled()) {
log.debug("Reader entry: " + line);
}
lines.add(line);
if (getResources(path + "/" + line).isEmpty()) {
lines.clear();
break;
}
}
if (!lines.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Listing " + url);
}
children.addAll(lines);
}
}
} catch (FileNotFoundException e) {
/*
* For file URLs the openStream() call might fail, depending on the servlet
* container, because directories can't be opened for reading. If that happens,
* then list the directory directly instead.
*/
if ("file".equals(url.getProtocol())) {
File file = new File(url.getFile());
if (log.isDebugEnabled()) {
log.debug("Listing directory " + file.getAbsolutePath());
}
if (file.isDirectory()) {
if (log.isDebugEnabled()) {
log.debug("Listing " + url);
}
children = Arrays.asList(file.list());
}
} else {
// No idea where the exception came from so rethrow it
throw e;
}
}
// The URL prefix to use when recursively listing child resources
String prefix = url.toExternalForm();
if (!prefix.endsWith("/")) {
prefix = prefix + "/";
}
// Iterate over immediate children, adding files and recursing into directories
for (String child : children) {
String resourcePath = path + "/" + child;
resources.add(resourcePath);
URL childUrl = new URL(prefix + child);
resources.addAll(list(childUrl, resourcePath));
}
}
return resources;
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
// Ignore
}
}
}
}
Aggregations