下面列出了java.util.jar.JarOutputStream#finish ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void createContributionsJar(File jarFile, String classpath) throws IOException {
//need to create zip file
OutputStream os = new BufferedOutputStream(new FileOutputStream(jarFile));
try {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0"); // NOI18N
manifest.getMainAttributes().put(Name.CLASS_PATH, classpath);
JarOutputStream dest = new JarOutputStream(new BufferedOutputStream(os), manifest);
try {
dest.closeEntry();
dest.finish();
} finally {
dest.close();
}
} finally {
os.close();
}
}
/**
* The minimum requirement to have a "valid" archive plugin is to include
* findbugs.xml, messages.xml and MANIFEST.MF files. The rest of the
* resources are load using the parent ClassLoader (Not requires to be in
* the jar).
* <p>
* Instead of building a file on disk, the result of the stream is kept in
* memory and return as a byte array.
*
* @return
* @throws IOException
* @throws URISyntaxException
*/
private byte[] buildFakePluginJar() throws IOException, URISyntaxException {
ClassLoader cl = getClass().getClassLoader();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
JarOutputStream jar = new JarOutputStream(buffer);
final URL metadata = cl.getResource("metadata");
if (metadata != null) {
final File dir = new File(metadata.toURI());
//Add files to the jar stream
addFilesToStream(cl, jar, dir, "");
}
jar.finish();
jar.close();
return buffer.toByteArray();
}
@Test
public void testManifestClassPathResolver() throws Exception
{
String artifactId = "hadoop-common";
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/META-INF/maven/org.apache.hadoop/" + artifactId + "/pom.properties"));
String version = properties.getProperty("version");
String jarPath = "org/apache/hadoop/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar";
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarPath);
File testFile = new File("target/resolverTest.jar");
FileOutputStream stream = new FileOutputStream(testFile);
JarOutputStream out = new JarOutputStream(stream, manifest);
out.putNextEntry(new JarEntry("foo/bar"));
out.write("Hello World".getBytes());
out.closeEntry();
out.finish();
out.close();
JarFile jarFile = new JarFile(testFile);
ClassPathResolvers.JarFileContext jfc = new ClassPathResolvers.JarFileContext(jarFile, new StringWriter());
ClassPathResolvers cpr = new ClassPathResolvers();
String baseDir = System.getProperty("localRepository", System.getProperty("user.home") + "/.m2/repository");
List<ClassPathResolvers.Resolver> resolvers = cpr.createResolvers(ClassPathResolvers.SCHEME_MANIFEST + ":" + baseDir);
Assert.assertEquals(1, resolvers.size());
resolvers.get(0).resolve(jfc);
jarFile.close();
Assert.assertEquals("number path components " + jfc.urls, 1, jfc.urls.size());
URL first = jfc.urls.iterator().next();
Assert.assertEquals("first url", baseDir + "/" + jarPath, first.getPath());
}
public void createJar(OutputStream outputStream) {
try {
JarOutputStream jarOutputStream = new JarOutputStream(outputStream);
createJar(jarOutputStream);
jarOutputStream.finish();
} catch (IOException e) {
LOGGER.error("", e);
}
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
context.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
context.err.println("\ntrouble writing output:");
ex.printStackTrace(context.err);
} else {
context.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
context.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
context.err.println("\ntrouble writing output:");
ex.printStackTrace(context.err);
} else {
context.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private static boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
DxConsole.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
DxConsole.err.println("\ntrouble writing output:");
ex.printStackTrace(DxConsole.err);
} else {
DxConsole.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
DxConsole.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
DxConsole.err.println("\ntrouble writing output:");
ex.printStackTrace(DxConsole.err);
} else {
DxConsole.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private static boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
DxConsole.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
DxConsole.err.println("\ntrouble writing output:");
ex.printStackTrace(DxConsole.err);
} else {
DxConsole.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
context.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
context.err.println("\ntrouble writing output:");
ex.printStackTrace(context.err);
} else {
context.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
context.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
context.err.println("\ntrouble writing output:");
ex.printStackTrace(context.err);
} else {
context.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}