use of java.util.jar.JarOutputStream in project tika by apache.
the class ForkClient method fillBootstrapJar.
/**
* Fills in the jar file used to bootstrap the forked server process.
* All the required <code>.class</code> files and a manifest with a
* <code>Main-Class</code> entry are written into the archive.
*
* @param file file to hold the bootstrap archive
* @throws IOException if the bootstrap archive could not be created
*/
private static void fillBootstrapJar(File file) throws IOException {
try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) {
String manifest = "Main-Class: " + ForkServer.class.getName() + "\n";
jar.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
jar.write(manifest.getBytes(UTF_8));
Class<?>[] bootstrap = { ForkServer.class, ForkObjectInputStream.class, ForkProxy.class, ClassLoaderProxy.class, MemoryURLConnection.class, MemoryURLStreamHandler.class, MemoryURLStreamHandlerFactory.class, MemoryURLStreamRecord.class };
ClassLoader loader = ForkServer.class.getClassLoader();
for (Class<?> klass : bootstrap) {
String path = klass.getName().replace('.', '/') + ".class";
try (InputStream input = loader.getResourceAsStream(path)) {
jar.putNextEntry(new JarEntry(path));
IOUtils.copy(input, jar);
}
}
}
}
use of java.util.jar.JarOutputStream in project sling by apache.
the class ModelArchiveWriter method write.
/**
* Create a model archive.
* The output stream will not be closed by this method. The caller
* must call {@link JarOutputStream#close()} or {@link JarOutputStream#finish()}
* on the return output stream. The caller can add additional files through
* the return stream.
*
* In order to create an archive for a model, each feature in the model must
* have a name and a version and the model must be valid, therefore {@link ModelUtility#validateIncludingVersion(Model)}
* is called first. If the model is invalid an {@code IOException} is thrown.
*
* @param out The output stream to write to
* @param model The model to write
* @param baseManifest Optional base manifest used for creating the manifest.
* @param provider The artifact provider
* @return The jar output stream.
* @throws IOException If anything goes wrong
*/
public static JarOutputStream write(final OutputStream out, final Model model, final Manifest baseManifest, final ArtifactProvider provider) throws IOException {
// check model
final Map<Traceable, String> errors = ModelUtility.validate(model);
if (errors != null) {
throw new IOException("Model is not valid: " + errors);
}
// create manifest
final Manifest manifest = (baseManifest == null ? new Manifest() : new Manifest(baseManifest));
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
manifest.getMainAttributes().putValue(MANIFEST_HEADER, String.valueOf(ARCHIVE_VERSION));
// create archive
final JarOutputStream jos = new JarOutputStream(out, manifest);
// write model first
final JarEntry entry = new JarEntry(MODEL_NAME);
jos.putNextEntry(entry);
final Writer writer = new OutputStreamWriter(jos, "UTF-8");
ModelWriter.write(writer, model);
writer.flush();
jos.closeEntry();
final byte[] buffer = new byte[1024 * 1024 * 256];
for (final Feature f : model.getFeatures()) {
for (final RunMode rm : f.getRunModes()) {
for (final ArtifactGroup g : rm.getArtifactGroups()) {
for (final Artifact a : g) {
final JarEntry artifactEntry = new JarEntry(ARTIFACTS_PREFIX + a.getRepositoryPath());
jos.putNextEntry(artifactEntry);
try (final InputStream is = provider.getInputStream(a)) {
int l = 0;
while ((l = is.read(buffer)) > 0) {
jos.write(buffer, 0, l);
}
}
jos.closeEntry();
}
}
}
}
return jos;
}
use of java.util.jar.JarOutputStream in project sling by apache.
the class InstallServlet method createJar.
private static void createJar(final File sourceDir, final File jarFile, final Manifest mf) throws IOException {
final JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
try {
zos.setLevel(Deflater.NO_COMPRESSION);
// manifest first
final ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
zos.putNextEntry(anEntry);
mf.write(zos);
zos.closeEntry();
zipDir(sourceDir, zos, "");
} finally {
try {
zos.close();
} catch (final IOException ignore) {
// ignore
}
}
}
use of java.util.jar.JarOutputStream in project jdk8u_jdk by JetBrains.
the class TestExceptions method unpack200Test2.
// test the Pack200.unpack(File, OutputStream);
static void unpack200Test2() {
List<UnpackTestFileInput> tlist = new ArrayList<UnpackTestFileInput>();
try {
// setup the test scenarios
try {
tlist.add(new UnpackTestFileInput((File) null, null));
tlist.add(new UnpackTestFileInput(testPackFile, null));
tlist.add(new UnpackTestFileInput((File) null, new JarOutputStream(new ByteArrayOutputStream())));
} catch (Exception e) {
throw new Error("Initialization error", e);
}
// test the scenarios
for (UnpackTestFileInput ti : tlist) {
System.out.println(ti);
try {
Pack200.Unpacker unpacker = Pack200.newUnpacker();
unpacker.unpack(ti.getInputFile(), ti.getJarOutputStream());
} catch (Exception e) {
ti.checkException(e);
}
}
} finally {
// keep jprt happy
for (TestInput ti : tlist) {
if (ti != null) {
ti.close();
}
}
}
}
use of java.util.jar.JarOutputStream in project jdk8u_jdk by JetBrains.
the class TestExceptions method unpack200Test1.
// test the Pack200.unpack(InputStream, OutputStream);
static void unpack200Test1() {
List<UnpackTestInput> tlist = new ArrayList<UnpackTestInput>();
try {
// setup the test scenarios
try {
tlist.add(new UnpackTestInput((InputStream) null, null));
tlist.add(new UnpackTestInput(new FileInputStream(testPackFile), null));
tlist.add(new UnpackTestInput((InputStream) null, new JarOutputStream(new ByteArrayOutputStream())));
} catch (Exception e) {
throw new Error("Initialization error", e);
}
// test the scenarios
for (UnpackTestInput ti : tlist) {
System.out.println(ti);
try {
Pack200.Unpacker unpacker = Pack200.newUnpacker();
unpacker.unpack(ti.getInputStream(), ti.getJarOutputStream());
} catch (Exception e) {
ti.checkException(e);
}
}
} finally {
// keep jprt happy
for (TestInput ti : tlist) {
if (ti != null) {
ti.close();
}
}
}
}
Aggregations