下面列出了怎么用java.util.jar.Pack200的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateProgress() {
// Progress is a combination of segment reading and file writing.
final double READ_WT = 0.33;
final double WRITE_WT = 0.67;
double readProgress = _segCount;
if (_estByteLimit > 0 && _byteCount > 0)
readProgress += (double)_byteCount / _estByteLimit;
double writeProgress = _fileCount;
double scaledProgress
= READ_WT * readProgress / Math.max(_estSegLimit,1)
+ WRITE_WT * writeProgress / Math.max(_estFileLimit,1);
int percent = (int) Math.round(100*scaledProgress);
if (percent > 100) percent = 100;
if (percent > _prevPercent) {
_prevPercent = percent;
_props.setInteger(Pack200.Unpacker.PROGRESS, percent);
if (_verbose > 0)
Utils.log.info("progress = "+percent);
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
*
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
private void updateProgress() {
// Progress is a combination of segment reading and file writing.
final double READ_WT = 0.33;
final double WRITE_WT = 0.67;
double readProgress = _segCount;
if (_estByteLimit > 0 && _byteCount > 0)
readProgress += (double)_byteCount / _estByteLimit;
double writeProgress = _fileCount;
double scaledProgress
= READ_WT * readProgress / Math.max(_estSegLimit,1)
+ WRITE_WT * writeProgress / Math.max(_estFileLimit,1);
int percent = (int) Math.round(100*scaledProgress);
if (percent > 100) percent = 100;
if (percent > _prevPercent) {
_prevPercent = percent;
_props.setInteger(Pack200.Unpacker.PROGRESS, percent);
if (_verbose > 0)
Utils.log.info("progress = "+percent);
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
*
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert(Utils.currentInstance.get() == null);
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE))
? null
: TimeZone.getDefault();
try {
Utils.currentInstance.set(this);
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (tz != null) TimeZone.setDefault(tz);
in.close();
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
*
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
private Map<String, String> properties() {
// Create the Packer object
Pack200.Packer packer = Pack200.newPacker();
Map<String, String> p = packer.properties();
p.put(Pack200.Packer.EFFORT, "9"); // default is "5"
p.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
p.put(Pack200.Packer.KEEP_FILE_ORDER, Pack200.Packer.FALSE);
p.put(Pack200.Packer.MODIFICATION_TIME, Pack200.Packer.LATEST);
p.put(Pack200.Packer.DEFLATE_HINT, Pack200.Packer.TRUE); // compression enabled
// p.put("com.sun.java.util.jar.pack.verbose", Pack200.Packer.FALSE);
// p.put("com.sun.java.util.jar.pack.nolog", Pack200.Packer.TRUE);
String[] attributes = { UNKNOWN_ATTRIBUTE, CLASS_ATTRIBUTE_PFX, FIELD_ATTRIBUTE_PFX, METHOD_ATTRIBUTE_PFX,
CODE_ATTRIBUTE_PFX, };
String[] stripCodeAttributes = { "SourceFile", "LineNumberTable", "LocalVariableTable", "Deprecated" };
for (String attribute : attributes) {
for (String attributeName : stripCodeAttributes) {
p.put(attribute + attributeName, Pack200.Packer.STRIP);
}
}
p.put(Pack200.Packer.UNKNOWN_ATTRIBUTE, Pack200.Packer.STRIP);
return p;
}
private void updateProgress() {
// Progress is a combination of segment reading and file writing.
final double READ_WT = 0.33;
final double WRITE_WT = 0.67;
double readProgress = _segCount;
if (_estByteLimit > 0 && _byteCount > 0)
readProgress += (double)_byteCount / _estByteLimit;
double writeProgress = _fileCount;
double scaledProgress
= READ_WT * readProgress / Math.max(_estSegLimit,1)
+ WRITE_WT * writeProgress / Math.max(_estFileLimit,1);
int percent = (int) Math.round(100*scaledProgress);
if (percent > 100) percent = 100;
if (percent > _prevPercent) {
_prevPercent = percent;
_props.setInteger(Pack200.Unpacker.PROGRESS, percent);
if (_verbose > 0)
Utils.log.info("progress = "+percent);
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
*
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert(Utils.currentInstance.get() == null);
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE))
? null
: TimeZone.getDefault();
try {
Utils.currentInstance.set(this);
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (tz != null) TimeZone.setDefault(tz);
in.close();
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
*
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarFile and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* @param in a JarFile
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarFile in, OutputStream out) throws IOException {
assert(Utils.currentInstance.get() == null);
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE))
? null
: TimeZone.getDefault();
try {
Utils.currentInstance.set(this);
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (tz != null) TimeZone.setDefault(tz);
in.close();
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println("command unput-directory output-directory");
System.exit(1);
}
Packer packer = Pack200.newPacker();
Map p = packer.properties();
p.put(Packer.EFFORT, "9");
p.put(Packer.SEGMENT_LIMIT, "-1");
p.put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
p.put(Packer.MODIFICATION_TIME, Packer.LATEST);
p.put(Packer.DEFLATE_HINT, Packer.FALSE);
p.put(Packer.CODE_ATTRIBUTE_PFX + "LineNumberTable", Packer.STRIP);
p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);
File inputDirectory = new File(args[0]);
File outputDirectory = new File(args[1]);
pack(packer, inputDirectory, outputDirectory);
}
private void updateProgress() {
// Progress is a combination of segment reading and file writing.
final double READ_WT = 0.33;
final double WRITE_WT = 0.67;
double readProgress = _segCount;
if (_estByteLimit > 0 && _byteCount > 0)
readProgress += (double)_byteCount / _estByteLimit;
double writeProgress = _fileCount;
double scaledProgress
= READ_WT * readProgress / Math.max(_estSegLimit,1)
+ WRITE_WT * writeProgress / Math.max(_estFileLimit,1);
int percent = (int) Math.round(100*scaledProgress);
if (percent > 100) percent = 100;
if (percent > _prevPercent) {
_prevPercent = percent;
_props.setInteger(Pack200.Unpacker.PROGRESS, percent);
if (_verbose > 0)
Utils.log.info("progress = "+percent);
}
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
void flushAll(OutputStream out) throws IOException {
props.setInteger(Pack200.Packer.PROGRESS, 50);
flushPackage(out, 0);
out.flush();
props.setInteger(Pack200.Packer.PROGRESS, 100);
segmentCount += 1;
segmentTotalSize += segmentSize;
segmentSize = 0;
if (verbose > 0 && segmentCount > 1) {
Utils.log.info("Transmitted "
+segmentTotalSize+" input bytes in "
+segmentCount+" segments totaling "
+totalOutputSize+" bytes");
}
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
private static void unpack0(File inFile, JarOutputStream jarStream,
boolean useJavaUnpack) throws IOException {
// Unpack the files
Pack200.Unpacker unpacker = Pack200.newUnpacker();
Map<String, String> props = unpacker.properties();
if (useJavaUnpack) {
props.put("com.sun.java.util.jar.pack.disable.native", "true");
}
// Call the unpacker
unpacker.unpack(inFile, jarStream);
}
void flushPartial(OutputStream out, int nextCount) throws IOException {
if (pkg.files.isEmpty() && pkg.classes.isEmpty()) {
return; // do not flush an empty segment
}
flushPackage(out, Math.max(1, nextCount));
props.setInteger(Pack200.Packer.PROGRESS, 25);
// In case there will be another segment:
makeNextPackage();
segmentCount += 1;
segmentTotalSize += segmentSize;
segmentSize = 0;
}
void flushAll(OutputStream out) throws IOException {
props.setInteger(Pack200.Packer.PROGRESS, 50);
flushPackage(out, 0);
out.flush();
props.setInteger(Pack200.Packer.PROGRESS, 100);
segmentCount += 1;
segmentTotalSize += segmentSize;
segmentSize = 0;
if (verbose > 0 && segmentCount > 1) {
Utils.log.info("Transmitted "
+segmentTotalSize+" input bytes in "
+segmentCount+" segments totaling "
+totalOutputSize+" bytes");
}
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
private static void unpack0(File inFile, JarOutputStream jarStream,
boolean useJavaUnpack) throws IOException {
// Unpack the files
Pack200.Unpacker unpacker = Pack200.newUnpacker();
Map<String, String> props = unpacker.properties();
if (useJavaUnpack) {
props.put("com.sun.java.util.jar.pack.disable.native", "true");
}
// Call the unpacker
unpacker.unpack(inFile, jarStream);
}
static void verify6991164() {
Unpacker unpacker = Pack200.newUnpacker();
String versionStr = unpacker.toString();
String expected = "Pack200, Vendor: " +
System.getProperty("java.vendor") + ", Version: " +
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
if (!versionStr.equals(expected)) {
System.out.println("Expected: " + expected);
System.out.println("Obtained: " + versionStr);
throw new RuntimeException("did not get expected string " + expected);
}
}
private static void unpack0(File inFile, JarOutputStream jarStream,
boolean useJavaUnpack) throws IOException {
// Unpack the files
Pack200.Unpacker unpacker = Pack200.newUnpacker();
Map<String, String> props = unpacker.properties();
if (useJavaUnpack) {
props.put("com.sun.java.util.jar.pack.disable.native", "true");
}
// Call the unpacker
unpacker.unpack(inFile, jarStream);
}
void flushAll(OutputStream out) throws IOException {
props.setInteger(Pack200.Packer.PROGRESS, 50);
flushPackage(out, 0);
out.flush();
props.setInteger(Pack200.Packer.PROGRESS, 100);
segmentCount += 1;
segmentTotalSize += segmentSize;
segmentSize = 0;
if (verbose > 0 && segmentCount > 1) {
Utils.log.info("Transmitted "
+segmentTotalSize+" input bytes in "
+segmentCount+" segments totaling "
+totalOutputSize+" bytes");
}
}