Search in sources :

Example 31 with JarOutputStream

use of java.util.jar.JarOutputStream in project hadoop by apache.

the class TestLocalJobSubmission method makeJar.

private Path makeJar(Path p) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(p.toString()));
    JarOutputStream jos = new JarOutputStream(fos);
    ZipEntry ze = new ZipEntry("test.jar.inside");
    jos.putNextEntry(ze);
    jos.write(("inside the jar!").getBytes());
    jos.closeEntry();
    jos.close();
    return p;
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) File(java.io.File)

Example 32 with JarOutputStream

use of java.util.jar.JarOutputStream in project hbase by apache.

the class ClassLoaderTestHelper method createJarArchive.

/**
   * Jar a list of files into a jar archive.
   *
   * @param archiveFile the target jar archive
   * @param tobejared a list of files to be jared
   */
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        // Open archive file
        FileOutputStream stream = new FileOutputStream(archiveFile);
        JarOutputStream out = new JarOutputStream(stream, new Manifest());
        for (int i = 0; i < tobeJared.length; i++) {
            if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) {
                continue;
            }
            // Add archive entry
            JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
            jarAdd.setTime(tobeJared[i].lastModified());
            out.putNextEntry(jarAdd);
            // Write file to archive
            FileInputStream in = new FileInputStream(tobeJared[i]);
            while (true) {
                int nRead = in.read(buffer, 0, buffer.length);
                if (nRead <= 0)
                    break;
                out.write(buffer, 0, nRead);
            }
            in.close();
        }
        out.close();
        stream.close();
        LOG.info("Adding classes to jar file completed");
        return true;
    } catch (Exception ex) {
        LOG.error("Error: " + ex.getMessage());
        return false;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream)

Example 33 with JarOutputStream

use of java.util.jar.JarOutputStream in project hbase by apache.

the class TestClassFinder method packageAndLoadJar.

/**
   * Makes a jar out of some class files. Unfortunately it's very tedious.
   * @param filesInJar Files created via compileTestClass.
   * @return path to the resulting jar file.
   */
private static String packageAndLoadJar(FileAndPath... filesInJar) throws Exception {
    // First, write the bogus jar file.
    String path = basePath + "jar" + jarCounter.incrementAndGet() + ".jar";
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    FileOutputStream fos = new FileOutputStream(path);
    JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest);
    // Directory entries for all packages have to be added explicitly for
    // resources to be findable via ClassLoader. Directory entries must end
    // with "/"; the initial one is expected to, also.
    Set<String> pathsInJar = new HashSet<>();
    for (FileAndPath fileAndPath : filesInJar) {
        String pathToAdd = fileAndPath.path;
        while (pathsInJar.add(pathToAdd)) {
            int ix = pathToAdd.lastIndexOf('/', pathToAdd.length() - 2);
            if (ix < 0) {
                break;
            }
            pathToAdd = pathToAdd.substring(0, ix);
        }
    }
    for (String pathInJar : pathsInJar) {
        jarOutputStream.putNextEntry(new JarEntry(pathInJar));
        jarOutputStream.closeEntry();
    }
    for (FileAndPath fileAndPath : filesInJar) {
        File file = fileAndPath.file;
        jarOutputStream.putNextEntry(new JarEntry(fileAndPath.path + file.getName()));
        byte[] allBytes = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(allBytes);
        fis.close();
        jarOutputStream.write(allBytes);
        jarOutputStream.closeEntry();
    }
    jarOutputStream.close();
    fos.close();
    // Add the file to classpath.
    File jarFile = new File(path);
    assertTrue(jarFile.exists());
    URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(urlClassLoader, new Object[] { jarFile.toURI().toURL() });
    return jarFile.getAbsolutePath();
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) Method(java.lang.reflect.Method) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) File(java.io.File) HashSet(java.util.HashSet)

Example 34 with JarOutputStream

use of java.util.jar.JarOutputStream in project hbase by apache.

the class JarFinder method createJar.

private static void createJar(File dir, File jarFile) throws IOException {
    Preconditions.checkNotNull(dir, "dir");
    Preconditions.checkNotNull(jarFile, "jarFile");
    File jarDir = jarFile.getParentFile();
    if (!jarDir.exists()) {
        if (!jarDir.mkdirs()) {
            throw new IOException(MessageFormat.format("could not create dir [{0}]", jarDir));
        }
    }
    try (FileOutputStream fos = new FileOutputStream(jarFile);
        JarOutputStream jos = new JarOutputStream(fos)) {
        jarDir(dir, "", jos);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 35 with JarOutputStream

use of java.util.jar.JarOutputStream in project hadoop by apache.

the class TestFSDownload method createJarFile.

static LocalResource createJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);
    File archiveFile = new File(p.toUri().getPath() + ".jar");
    archiveFile.createNewFile();
    JarOutputStream out = new JarOutputStream(new FileOutputStream(archiveFile));
    out.putNextEntry(new JarEntry(p.getName()));
    out.write(bytes);
    out.closeEntry();
    out.close();
    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".jar")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".jar")).getModificationTime());
    return ret;
}
Also used : Path(org.apache.hadoop.fs.Path) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) LocalResource(org.apache.hadoop.yarn.api.records.LocalResource)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)273 FileOutputStream (java.io.FileOutputStream)171 File (java.io.File)145 JarEntry (java.util.jar.JarEntry)109 Manifest (java.util.jar.Manifest)80 IOException (java.io.IOException)60 ZipEntry (java.util.zip.ZipEntry)60 JarFile (java.util.jar.JarFile)53 Test (org.junit.Test)43 FileInputStream (java.io.FileInputStream)42 InputStream (java.io.InputStream)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)38 ByteArrayInputStream (java.io.ByteArrayInputStream)30 JarInputStream (java.util.jar.JarInputStream)28 BufferedOutputStream (java.io.BufferedOutputStream)25 ArrayList (java.util.ArrayList)23 OutputStream (java.io.OutputStream)22 Path (java.nio.file.Path)21 Map (java.util.Map)19 HashMap (java.util.HashMap)16