Search in sources :

Example 36 with JarEntry

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;
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 37 with JarEntry

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;
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException)

Example 38 with JarEntry

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;
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException)

Example 39 with JarEntry

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);
    }
}
Also used : Path(java.nio.file.Path) StringWriter(java.io.StringWriter) BugChecker(com.google.errorprone.bugpatterns.BugChecker) JarOutputStream(java.util.jar.JarOutputStream) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Matchers.containsString(org.hamcrest.Matchers.containsString) JarEntry(java.util.jar.JarEntry) PrintWriter(java.io.PrintWriter) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 40 with JarEntry

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);
    }
}
Also used : InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26