use of java.util.jar.JarOutputStream in project byte-buddy by raphw.
the class AndroidClassLoadingStrategy method load.
@Override
public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
DexProcessor.Conversion conversion = dexProcessor.create();
for (Map.Entry<TypeDescription, byte[]> entry : types.entrySet()) {
conversion.register(entry.getKey().getName(), entry.getValue());
}
File jar = new File(privateDirectory, randomString.nextString() + JAR_FILE_EXTENSION);
try {
if (!jar.createNewFile()) {
throw new IllegalStateException("Cannot create " + jar);
}
JarOutputStream zipOutputStream = new JarOutputStream(new FileOutputStream(jar));
try {
zipOutputStream.putNextEntry(new JarEntry(DEX_CLASS_FILE));
conversion.drainTo(zipOutputStream);
zipOutputStream.closeEntry();
} finally {
zipOutputStream.close();
}
return doLoad(classLoader, types.keySet(), jar);
} catch (IOException exception) {
throw new IllegalStateException("Cannot write to zip file " + jar, exception);
} finally {
if (!jar.delete()) {
Logger.getLogger("net.bytebuddy").warning("Could not delete " + jar);
}
}
}
use of java.util.jar.JarOutputStream in project robovm by robovm.
the class DalvikExecTest method test_execCreatedJarWithManifest.
/**
* This test does quite the same as test_execCreatedJar, but includes a manifest.
* Note however that the Dalvik JAR format does not require this manifest.
* We just test whether the manifest is placed correctly within the JAR by
* dumping its contents read as a simple text resource.
* No! We can't do that so easily either, as there are other (parent) JARs
* with a manifest inside, taken with precedence.
* So we will reopen the JAR as a JarFile and check the manifest
* with a top level end-to-end approach.
*/
public void test_execCreatedJarWithManifest() throws IOException, InterruptedException {
File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar");
jarFile.deleteOnExit();
// Create the manifest:
Manifest manifest = new Manifest();
Attributes attrs = manifest.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "3.1415962");
attrs.put(Attributes.Name.MAIN_CLASS, "dalvikExecTest.HelloWorld");
attrs.put(Attributes.Name.CLASS_PATH, jarFile.getName());
// Create a JAR output stream on the temp file using the manifest:
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile), manifest);
// Define the entry for the classes.dex:
jarOut.putNextEntry(new JarEntry("classes.dex"));
// Fill in the classes.dex contents, i.e. the Dalvik executable code:
// (See below for the detailed source code contents.)
Streams.copy(Support_Resources.getResourceStream("cts_dalvikExecTest_classes.dex"), jarOut);
// Now add a resource file:
//
jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource"));
jarOut.write("This Resource contains some text.".getBytes());
// Close the stream to the completed JAR file.
jarOut.close();
// The resulting JAR file contains the classes listed at the end of this text,
// like the 'cts_dalvikExecTest.jar' as part of the resources, too.
String res;
res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld");
assertEquals("Hello Android World!", "Hello Android World!\n", res);
res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper");
assertTrue("Android Resource Dumper started", res.contains("Android Resource Dumper started"));
assertTrue("This Resource contains some text.", res.contains("This Resource contains some text."));
// And now reread the manifest:
//
JarFile jarIn = new JarFile(jarFile);
manifest = jarIn.getManifest();
attrs = manifest.getMainAttributes();
assertEquals("MANIFEST_VERSION must match!", "3.1415962", attrs.get(Attributes.Name.MANIFEST_VERSION));
assertEquals("MAIN_CLASS must match!", "dalvikExecTest.HelloWorld", attrs.get(Attributes.Name.MAIN_CLASS));
assertEquals("CLASS_PATH must match!", jarFile.getName(), attrs.get(Attributes.Name.CLASS_PATH));
}
use of java.util.jar.JarOutputStream in project robovm by robovm.
the class DalvikExecTest method test_execCreatedJar.
// Create a temp file, fill it with contents according to Dalvik JAR format, and execute it on dalvikvm using -classpath option.",
public void test_execCreatedJar() throws IOException, InterruptedException {
File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar");
jarFile.deleteOnExit();
// Create a JAR output stream on the temp file:
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile));
// Define the entry for the classes.dex:
jarOut.putNextEntry(new JarEntry("classes.dex"));
// Fill in the classes.dex contents, i.e. the Dalvik executable code:
// (See below for the detailed source code contents.)
Streams.copy(Support_Resources.getResourceStream("cts_dalvikExecTest_classes.dex"), jarOut);
// Now add a resource file:
//
jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource"));
jarOut.write("This Resource contains some text.".getBytes());
// Close the stream to the completed JAR file.
jarOut.close();
// The resulting JAR file contains the classes listed at the end of this text,
// like the 'cts_dalvikExecTest.jar' as part of the resources, too.
String res;
res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld");
assertEquals("Hello Android World!", "Hello Android World!\n", res);
res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper");
assertTrue("Android Resource Dumper started", res.contains("Android Resource Dumper started"));
assertTrue("This Resource contains some text.", res.contains("This Resource contains some text."));
}
use of java.util.jar.JarOutputStream in project robovm by robovm.
the class OldJarURLConnectionTest method test_getInputStream_DeleteJarFileUsingURLConnection.
public void test_getInputStream_DeleteJarFileUsingURLConnection() throws Exception {
String entry = "text.txt";
String cts = System.getProperty("java.io.tmpdir");
File tmpDir = new File(cts);
File jarFile = File.createTempFile("file", ".jar", tmpDir);
String jarFileName = jarFile.getPath();
FileOutputStream jarFileOutputStream = new FileOutputStream(jarFileName);
JarOutputStream out = new JarOutputStream(new BufferedOutputStream(jarFileOutputStream));
JarEntry jarEntry = new JarEntry(entry);
out.putNextEntry(jarEntry);
out.write(new byte[] { 'a', 'b', 'c' });
out.close();
URL url = new URL("jar:file:" + jarFileName + "!/" + entry);
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
InputStream is = conn.getInputStream();
is.close();
assertTrue(jarFile.delete());
}
use of java.util.jar.JarOutputStream in project spring-boot by spring-projects.
the class AbstractExecutableArchiveLauncherTests method createJarArchive.
protected File createJarArchive(String name, String entryPrefix) throws IOException {
File archive = this.temp.newFile(name);
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(archive));
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/"));
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/classes/"));
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/lib/"));
JarEntry libFoo = new JarEntry(entryPrefix + "/lib/foo.jar");
libFoo.setMethod(ZipEntry.STORED);
ByteArrayOutputStream fooJarStream = new ByteArrayOutputStream();
new JarOutputStream(fooJarStream).close();
libFoo.setSize(fooJarStream.size());
CRC32 crc32 = new CRC32();
crc32.update(fooJarStream.toByteArray());
libFoo.setCrc(crc32.getValue());
jarOutputStream.putNextEntry(libFoo);
jarOutputStream.write(fooJarStream.toByteArray());
jarOutputStream.close();
return archive;
}
Aggregations