下面列出了java.util.jar.Manifest#write ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private byte[] manifestContentImpl(Manifest manifest) throws IOException {
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
Attributes.Name createdBy = new Attributes.Name("Created-By");
if (attributes.getValue(createdBy) == null) {
attributes.put(createdBy, "bazel");
}
if (mainClass != null) {
attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
}
if (targetLabel != null) {
attributes.put(JarHelper.TARGET_LABEL, targetLabel);
}
if (injectingRuleKind != null) {
attributes.put(JarHelper.INJECTING_RULE_KIND, injectingRuleKind);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return out.toByteArray();
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
if (isMultiRelease) {
addMultiRelease(m);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private boolean updateManifest(Manifest m, ZipOutputStream zos)
throws IOException
{
addVersion(m);
addCreatedBy(m);
if (ename != null) {
addMainClass(m, ename);
}
ZipEntry e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, m);
}
zos.putNextEntry(e);
m.write(zos);
if (vflag) {
output(getMsg("out.update.manifest"));
}
return true;
}
private byte[] manifestContent(@Nullable String targetLabel, @Nullable String injectingRuleKind)
throws IOException {
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
Attributes.Name createdBy = new Attributes.Name("Created-By");
if (attributes.getValue(createdBy) == null) {
attributes.put(createdBy, "bazel");
}
if (targetLabel != null) {
// Enable add_deps support. add_deps expects this attribute in the jar manifest.
attributes.putValue("Target-Label", targetLabel);
}
if (injectingRuleKind != null) {
// add_deps support for aspects. Usually null.
attributes.putValue("Injecting-Rule-Kind", injectingRuleKind);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return out.toByteArray();
}
/**
* Creates a JAR file.
*
* Equivalent to {@code jar cfm <jarfile> <manifest> -C <dir> file...}
*
* The input files are resolved against the given directory. Any input
* files that are directories are processed recursively.
*/
static void createJarFile(Path jarfile, Manifest man, Path dir, String... files)
throws IOException
{
// create the target directory
Path parent = jarfile.getParent();
if (parent != null)
Files.createDirectories(parent);
List<Path> entries = new ArrayList<>();
for (String file : files) {
Files.find(dir.resolve(file), Integer.MAX_VALUE,
(p, attrs) -> attrs.isRegularFile())
.map(e -> dir.relativize(e))
.forEach(entries::add);
}
try (OutputStream out = Files.newOutputStream(jarfile);
JarOutputStream jos = new JarOutputStream(out))
{
if (man != null) {
JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
jos.putNextEntry(je);
man.write(jos);
jos.closeEntry();
}
for (Path entry : entries) {
String name = toJarEntryName(entry);
jos.putNextEntry(new JarEntry(name));
Files.copy(dir.resolve(entry), jos);
jos.closeEntry();
}
}
}
@Test
public void testResolveLinkWithSourceAndClass() throws Exception {
Compiler<TestGenProcessor> compiler = buildCompiler(new TestGenProcessor(), "io.vertx.test.linkresolution.resolvable");
compiler.assertCompile();
File dependency = compiler.classOutput;
File metaInf = new File(dependency, "META-INF");
assertTrue(metaInf.mkdir());
File manifest = new File(metaInf, "MANIFEST.MF");
Manifest m = new Manifest();
m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
m.getMainAttributes().put(new Attributes.Name("Maven-Group-Id"), "foo");
m.getMainAttributes().put(new Attributes.Name("Maven-Artifact-Id"), "bar");
m.getMainAttributes().put(new Attributes.Name("Maven-Version"), "1.2.3");
try (OutputStream out = new FileOutputStream(manifest)) {
m.write(out);
}
LinkedList<Coordinate> resolved = new LinkedList<>();
compiler = buildCompiler(new TestGenProcessor() {
@Override
protected String resolveTypeLink(TypeElement elt, Coordinate coordinate) {
resolved.add(coordinate);
return super.resolveTypeLink(elt, coordinate);
}
}, "io.vertx.test.linkresolution");
List<File> files = new ArrayList<>();
files.add(dependency);
compiler.fileManager.getLocation(StandardLocation.CLASS_PATH).forEach(files::add);
compiler.fileManager.setLocation(StandardLocation.CLASS_PATH, files);
compiler.assertCompile();
String s = compiler.processor.getDoc("io.vertx.test.linkresolution.resolving");
assertEquals("`link:type[ResolvableType]`", s);
assertEquals(Collections.<Coordinate>singletonList(null), resolved);
}
/**
* Creates a new JAR file.
*/
void create(OutputStream out, Manifest manifest)
throws IOException
{
ZipOutputStream zos = new JarOutputStream(out);
if (flag0) {
zos.setMethod(ZipOutputStream.STORED);
}
if (manifest != null) {
if (vflag) {
output(getMsg("out.added.manifest"));
}
ZipEntry e = new ZipEntry(MANIFEST_DIR);
e.setTime(System.currentTimeMillis());
e.setSize(0);
e.setCrc(0);
zos.putNextEntry(e);
e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, manifest);
}
zos.putNextEntry(e);
manifest.write(zos);
zos.closeEntry();
}
for (File file: entries) {
addFile(zos, file);
}
zos.close();
}
protected JavaArchive createBootstrapArchive(String mainClassName, String appArtifact) throws IOException {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
archive.as(ZipImporter.class).importFrom(new JarFile(findBootstrapJar()));
Properties props = new Properties();
if (appArtifact != null) {
props.put(BootstrapProperties.APP_ARTIFACT, appArtifact);
}
ByteArrayOutputStream propsOut = new ByteArrayOutputStream();
props.store(propsOut, "");
propsOut.close();
archive.addAsManifestResource(new ByteArrayAsset(propsOut.toByteArray()), "wildfly-swarm.properties");
if (appArtifact != null) {
String conf = "path:" + appArtifact + "\n";
archive.addAsManifestResource(new ByteArrayAsset(conf.getBytes()), "wildfly-swarm-application.conf");
}
if (mainClassName != null) {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(new Attributes.Name("Wildfly-Swarm-Main-Class"), mainClassName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
out.close();
archive.addAsManifestResource(new ByteArrayAsset(out.toByteArray()), "MANIFEST.MF");
}
return archive;
}
/**
* Creates a new JAR file.
*/
void create(OutputStream out, Manifest manifest)
throws IOException
{
ZipOutputStream zos = new JarOutputStream(out);
if (flag0) {
zos.setMethod(ZipOutputStream.STORED);
}
if (manifest != null) {
if (vflag) {
output(getMsg("out.added.manifest"));
}
ZipEntry e = new ZipEntry(MANIFEST_DIR);
e.setTime(System.currentTimeMillis());
e.setSize(0);
e.setCrc(0);
zos.putNextEntry(e);
e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, manifest);
}
zos.putNextEntry(e);
manifest.write(zos);
zos.closeEntry();
}
for (File file: entries) {
addFile(zos, file);
}
zos.close();
}
/**
* Write the signature file to the given output stream.
*/
private void generateSignatureFile(Manifest manifest, OutputStream out)
throws IOException, GeneralSecurityException {
out.write(("Signature-Version: 1.0\r\n").getBytes());
out.write(("Created-By: 1.0 (Android SignApk)\r\n").getBytes());
// BASE64Encoder base64 = new BASE64Encoder();
MessageDigest md = MessageDigest.getInstance("SHA1");
PrintStream print = new PrintStream(
new DigestOutputStream(new ByteArrayOutputStream(), md),
true, "UTF-8");
// Digest of the entire manifest
manifest.write(print);
print.flush();
out.write(("SHA1-Digest-Manifest: " + Base64.encode(md.digest()) + "\r\n\r\n").getBytes());
Map<String, Attributes> entries = manifest.getEntries();
for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
if (canceled) break;
progressHelper.progress(ProgressEvent.PRORITY_NORMAL, resourceAdapter.getString(ResourceAdapter.Item.GENERATING_SIGNATURE_FILE));
// Digest of the manifest stanza for this entry.
String nameEntry = "Name: " + entry.getKey() + "\r\n";
print.print(nameEntry);
for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
print.print(att.getKey() + ": " + att.getValue() + "\r\n");
}
print.print("\r\n");
print.flush();
out.write(nameEntry.getBytes());
out.write(("SHA1-Digest: " + Base64.encode(md.digest()) + "\r\n\r\n").getBytes());
}
}
/**
* Creates a new JAR file.
*/
void create(OutputStream out, Manifest manifest)
throws IOException
{
ZipOutputStream zos = new JarOutputStream(out);
if (flag0) {
zos.setMethod(ZipOutputStream.STORED);
}
if (manifest != null) {
if (vflag) {
output(getMsg("out.added.manifest"));
}
ZipEntry e = new ZipEntry(MANIFEST_DIR);
e.setTime(System.currentTimeMillis());
e.setSize(0);
e.setCrc(0);
zos.putNextEntry(e);
e = new ZipEntry(MANIFEST_NAME);
e.setTime(System.currentTimeMillis());
if (flag0) {
crc32Manifest(e, manifest);
}
zos.putNextEntry(e);
manifest.write(zos);
zos.closeEntry();
}
for (File file: entries) {
addFile(zos, file);
}
zos.close();
}
private static void build(Path path, Attributes attributes) throws IOException {
try (OutputStream os = Files.newOutputStream(path);
JarOutputStream jos = new JarOutputStream(os)) {
JarEntry me = new JarEntry("META-INF/MANIFEST.MF");
jos.putNextEntry(me);
Manifest manifest = new Manifest();
manifest.getMainAttributes().putAll(attributes);
manifest.write(jos);
jos.closeEntry();
}
}
/**
* Computes the crc32 of a Manifest. This is necessary when the
* ZipOutputStream is in STORED mode.
*/
private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {
CRC32OutputStream os = new CRC32OutputStream();
m.write(os);
os.updateEntry(e);
}
/**
* Computes the crc32 of a Manifest. This is necessary when the
* ZipOutputStream is in STORED mode.
*/
private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {
CRC32OutputStream os = new CRC32OutputStream();
m.write(os);
os.updateEntry(e);
}
/**
* Computes the crc32 of a Manifest. This is necessary when the
* ZipOutputStream is in STORED mode.
*/
private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {
CRC32OutputStream os = new CRC32OutputStream();
m.write(os);
os.updateEntry(e);
}
/** Writes a .SF file with a digest to the manifest. */
private void writeSignatureFile(SignatureOutputStream out)
throws IOException, GeneralSecurityException {
Manifest sf = new Manifest();
Attributes main = sf.getMainAttributes();
main.putValue("Signature-Version", "1.0");
main.putValue("Created-By", "1.0 (Android)");
Base64.Encoder base64 = Base64.getMimeEncoder();
MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
PrintStream print = new PrintStream(
new DigestOutputStream(new ByteArrayOutputStream(), md),
true, SdkConstants.UTF_8);
// Digest of the entire manifest
mManifest.write(print);
print.flush();
main.putValue(DIGEST_MANIFEST_ATTR, base64.encodeToString(md.digest()));
Map<String, Attributes> entries = mManifest.getEntries();
for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
// Digest of the manifest stanza for this entry.
print.print("Name: " + entry.getKey() + "\r\n");
for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
print.print(att.getKey() + ": " + att.getValue() + "\r\n");
}
print.print("\r\n");
print.flush();
Attributes sfAttr = new Attributes();
sfAttr.putValue(DIGEST_ATTR, base64.encodeToString(md.digest()));
sf.getEntries().put(entry.getKey(), sfAttr);
}
sf.write(out);
// A bug in the java.util.jar implementation of Android platforms
// up to version 1.6 will cause a spurious IOException to be thrown
// if the length of the signature file is a multiple of 1024 bytes.
// As a workaround, add an extra CRLF in this case.
if ((out.size() % 1024) == 0) {
out.write('\r');
out.write('\n');
}
}