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;
}
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;
}
}
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();
}
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);
}
}
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;
}
Aggregations