use of java.util.jar.JarEntry in project yuicompressor by yui.
the class JarClassLoader method getJarPath.
private static String getJarPath() {
if (jarPath != null) {
return jarPath;
}
String classname = JarClassLoader.class.getName().replace('.', '/') + ".class";
String classpath = System.getProperty("java.class.path");
String[] classpaths = classpath.split(System.getProperty("path.separator"));
for (int i = 0; i < classpaths.length; i++) {
String path = classpaths[i];
JarFile jarFile = null;
JarEntry jarEntry = null;
try {
jarFile = new JarFile(path);
jarEntry = findJarEntry(jarFile, classname);
} catch (IOException ioe) {
/* ignore */
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
if (jarEntry != null) {
jarPath = path;
break;
}
}
return jarPath;
}
use of java.util.jar.JarEntry in project yuicompressor by yui.
the class JarClassLoader method loadClassData.
private Class loadClassData(JarFile jarFile, String className) {
String entryName = className.replace('.', '/') + ".class";
JarEntry jarEntry = findJarEntry(jarFile, entryName);
if (jarEntry == null) {
return null;
}
// Create the necessary package if needed...
int index = className.lastIndexOf('.');
if (index >= 0) {
String packageName = className.substring(0, index);
if (getPackage(packageName) == null) {
definePackage(packageName, "", "", "", "", "", "", null);
}
}
// Read the Jar File entry and define the class...
Class c = null;
InputStream is = null;
try {
is = jarFile.getInputStream(jarEntry);
ByteArrayOutputStream os = new ByteArrayOutputStream();
copy(is, os);
byte[] bytes = os.toByteArray();
c = defineClass(className, bytes, 0, bytes.length);
} catch (IOException ioe) {
/* ignore */
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
return c;
}
use of java.util.jar.JarEntry in project XobotOS by xamarin.
the class ZipInputStream method closeEntry.
/**
* Closes the current ZIP entry and positions to read the next entry.
*
* @throws IOException
* if an {@code IOException} occurs.
*/
public void closeEntry() throws IOException {
checkClosed();
if (currentEntry == null) {
return;
}
if (currentEntry instanceof java.util.jar.JarEntry) {
Attributes temp = ((JarEntry) currentEntry).getAttributes();
if (temp != null && temp.containsKey("hidden")) {
return;
}
}
/*
* The following code is careful to leave the ZipInputStream in a
* consistent state, even when close() results in an exception. It does
* so by:
* - pushing bytes back into the source stream
* - reading a data descriptor footer from the source stream
* - resetting fields that manage the entry being closed
*/
// Ensure all entry bytes are read
Exception failure = null;
try {
Streams.skipAll(this);
} catch (Exception e) {
failure = e;
}
int inB, out;
if (currentEntry.compressionMethod == ZipEntry.DEFLATED) {
inB = inf.getTotalIn();
out = inf.getTotalOut();
} else {
inB = inRead;
out = inRead;
}
int diff = entryIn - inB;
// Pushback any required bytes
if (diff != 0) {
((PushbackInputStream) in).unread(buf, len - diff, diff);
}
try {
readAndVerifyDataDescriptor(inB, out);
} catch (Exception e) {
if (failure == null) {
// otherwise we're already going to throw
failure = e;
}
}
inf.reset();
lastRead = inRead = entryIn = len = 0;
crc.reset();
currentEntry = null;
if (failure != null) {
if (failure instanceof IOException) {
throw (IOException) failure;
} else if (failure instanceof RuntimeException) {
throw (RuntimeException) failure;
}
AssertionError error = new AssertionError();
error.initCause(failure);
throw error;
}
}
use of java.util.jar.JarEntry in project error-prone by google.
the class ErrorProneCompilerIntegrationTest method plugin.
@Test
public void plugin() throws Exception {
Path base = tmpFolder.newFolder().toPath();
Path source = base.resolve("test/Test.java");
Files.createDirectories(source.getParent());
Files.write(source, Arrays.asList(//
"package test;", "public class Test {", " int f() { return 42; }", "}"), UTF_8);
Path jar = base.resolve("libproc.jar");
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
jos.putNextEntry(new JarEntry("META-INF/services/" + BugChecker.class.getName()));
jos.write((CPSChecker.class.getName() + "\n").getBytes(UTF_8));
String classFile = CPSChecker.class.getName().replace('.', '/') + ".class";
jos.putNextEntry(new JarEntry(classFile));
ByteStreams.copy(getClass().getClassLoader().getResourceAsStream(classFile), jos);
}
// no plugins
{
List<String> args = ImmutableList.of(source.toAbsolutePath().toString(), "-processorpath", File.pathSeparator);
StringWriter out = new StringWriter();
Result result = ErrorProneCompiler.compile(args.toArray(new String[0]), new PrintWriter(out, true));
assertThat(result).isEqualTo(Result.OK);
}
// with plugins
{
List<String> args = ImmutableList.of(source.toAbsolutePath().toString(), "-processorpath", jar.toAbsolutePath().toString());
StringWriter out = new StringWriter();
Result result = ErrorProneCompiler.compile(args.toArray(new String[0]), new PrintWriter(out, true));
assertThat(out.toString()).contains("Using 'return' is considered harmful");
assertThat(result).isEqualTo(Result.ERROR);
}
}
use of java.util.jar.JarEntry in project error-prone by google.
the class BoxedPrimitiveConstructorTest method addClassToJar.
// TODO(b/30478325): create a better way to write this style of test
static void addClassToJar(JarOutputStream jos, Class<?> clazz) throws IOException {
String entryPath = clazz.getName().replace('.', '/') + ".class";
try (InputStream is = clazz.getClassLoader().getResourceAsStream(entryPath)) {
jos.putNextEntry(new JarEntry(entryPath));
ByteStreams.copy(is, jos);
}
}
Aggregations