下面列出了java.util.jar.JarEntry#setCrc ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void addZipEntryStream(ZipEntry zipEntry, InputStream is, String path) throws IOException {
if (fJarPackage.areDirectoryEntriesIncluded())
addDirectories(path);
JarEntry newEntry= new JarEntry(path.replace(File.separatorChar, '/'));
if (fJarPackage.isCompressed())
newEntry.setMethod(ZipEntry.DEFLATED);
// Entry is filled automatically.
else {
newEntry.setMethod(ZipEntry.STORED);
newEntry.setSize(zipEntry.getSize());
newEntry.setCrc(zipEntry.getCrc());
}
long lastModified= System.currentTimeMillis();
// Set modification time
newEntry.setTime(lastModified);
addEntry(newEntry, is);
}
/**
* Creates the directory entries for the given path and writes it to the current archive.
*
* @param destPath the path to add
*
* @throws IOException if an I/O error has occurred
* @since 3.5
*/
protected void addDirectories(String destPath) throws IOException {
String path= destPath.replace(File.separatorChar, '/');
int lastSlash= path.lastIndexOf('/');
List<JarEntry> directories= new ArrayList<JarEntry>(2);
while (lastSlash != -1) {
path= path.substring(0, lastSlash + 1);
if (!fDirectories.add(path))
break;
JarEntry newEntry= new JarEntry(path);
newEntry.setMethod(ZipEntry.STORED);
newEntry.setSize(0);
newEntry.setCrc(0);
newEntry.setTime(System.currentTimeMillis());
directories.add(newEntry);
lastSlash= path.lastIndexOf('/', lastSlash - 1);
}
for (int i= directories.size() - 1; i >= 0; --i) {
fJarOutputStream.putNextEntry(directories.get(i));
}
}
@Override
public JarEntry transform(JarEntry entry) {
JarEntry renamedEntry = new JarEntry(this.namePrefix + entry.getName());
renamedEntry.setTime(entry.getTime());
renamedEntry.setSize(entry.getSize());
renamedEntry.setMethod(entry.getMethod());
if (entry.getComment() != null) {
renamedEntry.setComment(entry.getComment());
}
renamedEntry.setCompressedSize(entry.getCompressedSize());
renamedEntry.setCrc(entry.getCrc());
//setCreationTimeIfPossible(entry, renamedEntry);
if (entry.getExtra() != null) {
renamedEntry.setExtra(entry.getExtra());
}
return renamedEntry;
}
private static void writeJarEntry(JarOutputStream jos, String path, File f) throws IOException, FileNotFoundException {
JarEntry je = new JarEntry(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(f);
try {
copyStreams(is, baos);
} finally {
is.close();
}
byte[] data = baos.toByteArray();
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
private static void writeJarEntry(JarOutputStream jos, String path, File f) throws IOException, FileNotFoundException {
JarEntry je = new JarEntry(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(f);
try {
copyStreams(is, baos);
} finally {
is.close();
}
byte[] data = baos.toByteArray();
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
/**
* Creates a new JarEntry with the passed path and contents, and writes it
* to the current archive.
*
* @param path the path inside the archive
* @param contents the bytes to write
* @param lastModified a long which represents the last modification date
* @throws IOException if an I/O error has occurred
*/
protected void write(IPath path, byte[] contents, long lastModified) throws IOException {
JarEntry newEntry= new JarEntry(path.toString().replace(File.separatorChar, '/'));
if (fJarPackage.isCompressed())
newEntry.setMethod(ZipEntry.DEFLATED);
// Entry is filled automatically.
else {
newEntry.setMethod(ZipEntry.STORED);
newEntry.setSize(contents.length);
CRC32 checksumCalculator= new CRC32();
checksumCalculator.update(contents);
newEntry.setCrc(checksumCalculator.getValue());
}
// Set modification time
newEntry.setTime(lastModified);
try {
fJarOutputStream.putNextEntry(newEntry);
fJarOutputStream.write(contents);
} finally {
/*
* Commented out because some JREs throw an NPE if a stream
* is closed twice. This works because
* a) putNextEntry closes the previous entry
* b) closing the stream closes the last entry
*/
// fJarOutputStream.closeEntry();
}
}
/**
* Check that jar: URLs are correctly mapped back into JarFileSystem resources.
* @see "#39190"
*/
public void testJarMapping() throws Exception {
clearWorkDir();
File workdir = getWorkDir();
File jar = new File(workdir, "test.jar");
String textPath = "x.txt";
OutputStream os = new FileOutputStream(jar);
try {
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(ZipEntry.STORED);
JarEntry entry = new JarEntry(textPath);
entry.setSize(0L);
entry.setTime(System.currentTimeMillis());
entry.setCrc(new CRC32().getValue());
jos.putNextEntry(entry);
jos.flush();
jos.close();
} finally {
os.close();
}
assertTrue("JAR was created", jar.isFile());
assertTrue("JAR is not empty", jar.length() > 0L);
JarFileSystem jfs = new JarFileSystem();
jfs.setJarFile(jar);
Repository.getDefault().addFileSystem(jfs);
FileObject rootFO = jfs.getRoot();
FileObject textFO = jfs.findResource(textPath);
assertNotNull("JAR contains a/b.txt", textFO);
String rootS = "jar:" + BaseUtilities.toURI(jar) + "!/";
URL rootU = new URL(rootS);
URL textU = new URL(rootS + textPath);
assertEquals("correct FO -> URL for root", rootU, URLMapper.findURL(rootFO, URLMapper.EXTERNAL));
assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL));
assertTrue("correct URL -> FO for root", Arrays.asList(URLMapper.findFileObjects(rootU)).contains(rootFO));
assertTrue("correct URL -> FO for " + textPath, Arrays.asList(URLMapper.findFileObjects(textU)).contains(textFO));
}
/**
* Create a fresh JAR file.
* @param jar the file to create
* @param contents keys are JAR entry paths, values are text contents (will be written in UTF-8)
* @param manifest a manifest to store (key/value pairs for main section)
*/
public static void createJar(File jar, Map<String,String> contents, Map<String,String> manifest) throws IOException {
// XXX use TestFileUtils.writeZipFile
Manifest m = new Manifest();
m.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
for (Map.Entry<String,String> line : manifest.entrySet()) {
m.getMainAttributes().putValue(line.getKey(), line.getValue());
}
jar.getParentFile().mkdirs();
OutputStream os = new FileOutputStream(jar);
try {
JarOutputStream jos = new JarOutputStream(os, m);
Iterator it = contents.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String path = (String) entry.getKey();
byte[] data = ((String) entry.getValue()).getBytes("UTF-8");
JarEntry je = new JarEntry(path);
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
jos.close();
} finally {
os.close();
}
}
private static void writeJarEntries(JarFile jar, JarOutputStream jos, String signatureName) throws IOException {
for (Enumeration<?> jarEntries = jar.entries(); jarEntries.hasMoreElements();) {
JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
if (!jarEntry.isDirectory()) {
String entryName = jarEntry.getName();
// Signature files not to write across
String sigFileLocation = MessageFormat.format(METAINF_FILE_LOCATION, signatureName, SIGNATURE_EXT)
.toUpperCase();
String dsaSigBlockLocation = MessageFormat.format(METAINF_FILE_LOCATION, signatureName,
DSA_SIG_BLOCK_EXT);
String rsaSigBlockLocation = MessageFormat.format(METAINF_FILE_LOCATION, signatureName,
RSA_SIG_BLOCK_EXT);
// Do not write across existing manifest or matching signature files
if ((!entryName.equalsIgnoreCase(MANIFEST_LOCATION)) && (!entryName.equalsIgnoreCase(sigFileLocation))
&& (!entryName.equalsIgnoreCase(dsaSigBlockLocation))
&& (!entryName.equalsIgnoreCase(rsaSigBlockLocation))) {
// New JAR entry based on original
JarEntry newJarEntry = new JarEntry(jarEntry.getName());
newJarEntry.setMethod(jarEntry.getMethod());
newJarEntry.setCompressedSize(jarEntry.getCompressedSize());
newJarEntry.setCrc(jarEntry.getCrc());
jos.putNextEntry(newJarEntry);
try (InputStream jis = jar.getInputStream(jarEntry)) {
byte[] buffer = new byte[2048];
int read = -1;
while ((read = jis.read(buffer)) != -1) {
jos.write(buffer, 0, read);
}
jos.closeEntry();
}
}
}
}
}
/**
* Saves a class entry to the jar output.
*/
private void saveEntry(String entry, URL url, Set<String> entries, JarOutputStream jarOut, boolean compress) {
if (!entries.add(entry)) {
return;
}
LOG.trace("adding bundle entry " + entry);
try {
JarEntry jarEntry = new JarEntry(entry);
try (InputStream is = url.openStream()) {
if (compress) {
jarOut.putNextEntry(jarEntry);
ByteStreams.copy(is, jarOut);
} else {
crc32.reset();
TransferByteOutputStream os = new TransferByteOutputStream();
CheckedOutputStream checkedOut = new CheckedOutputStream(os, crc32);
ByteStreams.copy(is, checkedOut);
checkedOut.close();
long size = os.size();
jarEntry.setMethod(JarEntry.STORED);
jarEntry.setSize(size);
jarEntry.setCrc(checkedOut.getChecksum().getValue());
jarOut.putNextEntry(jarEntry);
os.transfer(jarOut);
}
}
jarOut.closeEntry();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
private static void addEntry(JarOutputStream jos, String name, byte[] bytes) throws IOException {
JarEntry je = new JarEntry(name);
// TODO(cushon): switch to setLocalTime after we migrate to JDK 9
je.setTime(DEFAULT_TIMESTAMP);
je.setMethod(ZipEntry.STORED);
je.setSize(bytes.length);
je.setCrc(Hashing.crc32().hashBytes(bytes).padToLong());
jos.putNextEntry(je);
jos.write(bytes);
}
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i=0; i<8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i=0; i<8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb",
jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
je.setTime(file.modtime);
// Convert back to milliseconds
je.setTime((long)file.modtime * 1000);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i=0; i<8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i=0; i<8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb",
jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
je.setTime(file.modtime);
// Convert back to milliseconds
je.setTime((long)file.modtime * 1000);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
je.setTime(file.modtime);
// Convert back to milliseconds
je.setTime((long)file.modtime * 1000);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
je.setTime(file.modtime);
// Convert back to milliseconds
je.setTime((long)file.modtime * 1000);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i=0; i<8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i=0; i<8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb",
jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i=0; i<8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i=0; i<8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb",
jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
je.setTime(file.modtime);
// Convert back to milliseconds
je.setTime((long)file.modtime * 1000);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}