Example usage for java.util.jar JarFile getClass

List of usage examples for java.util.jar JarFile getClass

Introduction

In this page you can find the example usage for java.util.jar JarFile getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:UnpackedJarFile.java

public static void copyToPackedJar(JarFile inputJar, File outputFile) throws IOException {
    if (inputJar.getClass() == JarFile.class) {
        // this is a plain old jar... nothign special
        copyFile(new File(inputJar.getName()), outputFile);
    } else if (inputJar instanceof NestedJarFile && ((NestedJarFile) inputJar).isPacked()) {
        NestedJarFile nestedJarFile = (NestedJarFile) inputJar;
        JarFile baseJar = nestedJarFile.getBaseJar();
        String basePath = nestedJarFile.getBasePath();
        if (baseJar instanceof UnpackedJarFile) {
            // our target jar is just a file in upacked jar (a plain old directory)... now
            // we just need to find where it is and copy it to the outptu
            copyFile(((UnpackedJarFile) baseJar).getFile(basePath), outputFile);
        } else {/*  w  ww .  jav a 2 s .  c  om*/
            // out target is just a plain old jar file directly accessabel from the file system
            copyFile(new File(baseJar.getName()), outputFile);
        }
    } else {
        // copy out the module contents to a standalone jar file (entry by entry)
        JarOutputStream out = null;
        try {
            out = new JarOutputStream(new FileOutputStream(outputFile));
            byte[] buffer = new byte[4096];
            Enumeration entries = inputJar.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                InputStream in = inputJar.getInputStream(entry);
                try {
                    out.putNextEntry(new ZipEntry(entry.getName()));
                    try {
                        int count;
                        while ((count = in.read(buffer)) > 0) {
                            out.write(buffer, 0, count);
                        }
                    } finally {
                        out.closeEntry();
                    }
                } finally {
                    close(in);
                }
            }
        } finally {
            close(out);
        }
    }
}