|
Java example source code file (Pack200.java)
The Pack200.java Java example source code/* * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util.jar; import java.util.SortedMap; import java.io.InputStream; import java.io.OutputStream; import java.io.File; import java.io.IOException; import java.beans.PropertyChangeListener; /** * Transforms a JAR file to or from a packed stream in Pack200 format. * Please refer to Network Transfer Format JSR 200 Specification at * <a href=http://jcp.org/aboutJava/communityprocess/review/jsr200/index.html>http://jcp.org/aboutJava/communityprocess/review/jsr200/index.html * <p> * Typically the packer engine is used by application developers * to deploy or host JAR files on a website. * The unpacker engine is used by deployment applications to * transform the byte-stream back to JAR format. * <p> * Here is an example using packer and unpacker: * <pre>{@code * import java.util.jar.Pack200; * import java.util.jar.Pack200.*; * ... * // Create the Packer object * Packer packer = Pack200.newPacker(); * * // Initialize the state by setting the desired properties * Map p = packer.properties(); * // take more time choosing codings for better compression * p.put(Packer.EFFORT, "7"); // default is "5" * // use largest-possible archive segments (>10% better compression). * p.put(Packer.SEGMENT_LIMIT, "-1"); * // reorder files for better compression. * p.put(Packer.KEEP_FILE_ORDER, Packer.FALSE); * // smear modification times to a single value. * p.put(Packer.MODIFICATION_TIME, Packer.LATEST); * // ignore all JAR deflation requests, * // transmitting a single request to use "store" mode. * p.put(Packer.DEFLATE_HINT, Packer.FALSE); * // discard debug attributes * p.put(Packer.CODE_ATTRIBUTE_PFX+"LineNumberTable", Packer.STRIP); * // throw an error if an attribute is unrecognized * p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR); * // pass one class file uncompressed: * p.put(Packer.PASS_FILE_PFX+0, "mutants/Rogue.class"); * try { * JarFile jarFile = new JarFile("/tmp/testref.jar"); * FileOutputStream fos = new FileOutputStream("/tmp/test.pack"); * // Call the packer * packer.pack(jarFile, fos); * jarFile.close(); * fos.close(); * * File f = new File("/tmp/test.pack"); * FileOutputStream fostream = new FileOutputStream("/tmp/test.jar"); * JarOutputStream jostream = new JarOutputStream(fostream); * Unpacker unpacker = Pack200.newUnpacker(); * // Call the unpacker * unpacker.unpack(f, jostream); * // Must explicitly close the output. * jostream.close(); * } catch (IOException ioe) { * ioe.printStackTrace(); * } * }</pre> * <p> * A Pack200 file compressed with gzip can be hosted on HTTP/1.1 web servers. * The deployment applications can use "Accept-Encoding=pack200-gzip". This * indicates to the server that the client application desires a version of * the file encoded with Pack200 and further compressed with gzip. Please * refer to <a href="{@docRoot}/../technotes/guides/deployment/deployment-guide/pack200.html">Java Deployment Guide for more details and * techniques. * <p> * Unless otherwise noted, passing a <tt>null argument to a constructor or * method in this class will cause a {@link NullPointerException} to be thrown. * * @author John Rose * @author Kumar Srinivasan * @since 1.5 */ public abstract class Pack200 { private Pack200() {} //prevent instantiation // Static methods of the Pack200 class. /** * Obtain new instance of a class that implements Packer. * <ul> * <li>
* <p>
* Code like this might be used to strip debugging attributes:
* <pre> .
* The special strings {@link #ERROR}, {@link #STRIP}, and {@link #PASS}
* are also allowed.
* @see #CLASS_ATTRIBUTE_PFX
*/
String METHOD_ATTRIBUTE_PFX = "pack.method.attribute.";
/**
* When concatenated with a code attribute name,
* indicates the format of that attribute.
* For example, the effect of this option is built in:
* <code>pack.code.attribute.LocalVariableTable=NH[PHOHRUHRSHH].
* The special strings {@link #ERROR}, {@link #STRIP}, and {@link #PASS}
* are also allowed.
* @see #CLASS_ATTRIBUTE_PFX
*/
String CODE_ATTRIBUTE_PFX = "pack.code.attribute.";
/**
* The unpacker's progress as a percentage, as periodically
* updated by the unpacker.
* Values of 0 - 100 are normal, and -1 indicates a stall.
* Progress can be monitored by polling the value of this
* property.
* <p>
* At a minimum, the unpacker must set progress to 0
* at the beginning of a packing operation, and to 100
* at the end.
*/
String PROGRESS = "pack.progress";
/** The string "keep", a possible value for certain properties.
* @see #DEFLATE_HINT
* @see #MODIFICATION_TIME
*/
String KEEP = "keep";
/** The string "pass", a possible value for certain properties.
* @see #UNKNOWN_ATTRIBUTE
* @see #CLASS_ATTRIBUTE_PFX
* @see #FIELD_ATTRIBUTE_PFX
* @see #METHOD_ATTRIBUTE_PFX
* @see #CODE_ATTRIBUTE_PFX
*/
String PASS = "pass";
/** The string "strip", a possible value for certain properties.
* @see #UNKNOWN_ATTRIBUTE
* @see #CLASS_ATTRIBUTE_PFX
* @see #FIELD_ATTRIBUTE_PFX
* @see #METHOD_ATTRIBUTE_PFX
* @see #CODE_ATTRIBUTE_PFX
*/
String STRIP = "strip";
/** The string "error", a possible value for certain properties.
* @see #UNKNOWN_ATTRIBUTE
* @see #CLASS_ATTRIBUTE_PFX
* @see #FIELD_ATTRIBUTE_PFX
* @see #METHOD_ATTRIBUTE_PFX
* @see #CODE_ATTRIBUTE_PFX
*/
String ERROR = "error";
/** The string "true", a possible value for certain properties.
* @see #KEEP_FILE_ORDER
* @see #DEFLATE_HINT
*/
String TRUE = "true";
/** The string "false", a possible value for certain properties.
* @see #KEEP_FILE_ORDER
* @see #DEFLATE_HINT
*/
String FALSE = "false";
/** The string "latest", a possible value for certain properties.
* @see #MODIFICATION_TIME
*/
String LATEST = "latest";
/**
* Get the set of this engine's properties.
* This set is a "live view", so that changing its
* contents immediately affects the Packer engine, and
* changes from the engine (such as progress indications)
* are immediately visible in the map.
*
* <p>The property map may contain pre-defined implementation
* specific and default properties. Users are encouraged to
* read the information and fully understand the implications,
* before modifying pre-existing properties.
* <p>
* Implementation specific properties are prefixed with a
* package name associated with the implementor, beginning
* with <tt>com. or a similar prefix.
* All property names beginning with <tt>pack. and
* <tt>unpack. are reserved for use by this API.
* <p>
* Unknown properties may be ignored or rejected with an
* unspecified error, and invalid entries may cause an
* unspecified error to be thrown.
*
* <p>
* The returned map implements all optional {@link SortedMap} operations
* @return A sorted association of property key strings to property
* values.
*/
SortedMap<String,String> properties();
/**
* Takes a JarFile and converts it into a Pack200 archive.
* <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.
*/
void pack(JarFile in, OutputStream out) throws IOException ;
/**
* Takes a JarInputStream and converts it into a Pack200 archive.
* <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 its containing directory.
*
* @see #MODIFICATION_TIME
* @see #DEFLATE_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
void pack(JarInputStream in, OutputStream out) throws IOException ;
/**
* Registers a listener for PropertyChange events on the properties map.
* This is typically used by applications to update a progress bar.
*
* <p> The default implementation of this method does nothing and has
* no side-effects.</p>
*
* <p>WARNING: This method is omitted from the interface
* declaration in all subset Profiles of Java SE that do not include
* the {@code java.beans} package. </p>
* @see #properties
* @see #PROGRESS
* @param listener An object to be invoked when a property is changed.
* @deprecated The dependency on {@code PropertyChangeListener} creates
* a significant impediment to future modularization of the
* Java platform. This method will be removed in a future
* release.
* Applications that need to monitor progress of the packer
* can poll the value of the {@link #PROGRESS PROGRESS}
* property instead.
*/
@Deprecated
default void addPropertyChangeListener(PropertyChangeListener listener) {
}
/**
* Remove a listener for PropertyChange events, added by
* the {@link #addPropertyChangeListener}.
*
* <p> The default implementation of this method does nothing and has
* no side-effects.</p>
*
* <p>WARNING: This method is omitted from the interface
* declaration in all subset Profiles of Java SE that do not include
* the {@code java.beans} package. </p>
*
* @see #addPropertyChangeListener
* @param listener The PropertyChange listener to be removed.
* @deprecated The dependency on {@code PropertyChangeListener} creates
* a significant impediment to future modularization of the
* Java platform. This method will be removed in a future
* release.
*/
@Deprecated
default void removePropertyChangeListener(PropertyChangeListener listener) {
}
}
/**
* The unpacker engine converts the packed stream to a JAR file.
* An instance of the engine can be obtained
* using {@link #newUnpacker}.
* <p>
* Every JAR file produced by this engine will include the string
* "<tt>PACK200" as a zip file comment.
* This allows a deployer to detect if a JAR archive was packed and unpacked.
* <p>
* Note: Unless otherwise noted, passing a <tt>null argument to a
* constructor or method in this class will cause a {@link NullPointerException}
* to be thrown.
* <p>
* This version of the unpacker is compatible with all previous versions.
* @since 1.5
*/
public interface Unpacker {
/** The string "keep", a possible value for certain properties.
* @see #DEFLATE_HINT
*/
String KEEP = "keep";
/** The string "true", a possible value for certain properties.
* @see #DEFLATE_HINT
*/
String TRUE = "true";
/** The string "false", a possible value for certain properties.
* @see #DEFLATE_HINT
*/
String FALSE = "false";
/**
* Property indicating that the unpacker should
* ignore all transmitted values for DEFLATE_HINT,
* replacing them by the given value, {@link #TRUE} or {@link #FALSE}.
* The default value is the special string {@link #KEEP},
* which asks the unpacker to preserve all transmitted
* deflation hints.
*/
String DEFLATE_HINT = "unpack.deflate.hint";
/**
* The unpacker's progress as a percentage, as periodically
* updated by the unpacker.
* Values of 0 - 100 are normal, and -1 indicates a stall.
* Progress can be monitored by polling the value of this
* property.
* <p>
* At a minimum, the unpacker must set progress to 0
* at the beginning of a packing operation, and to 100
* at the end.
*/
String PROGRESS = "unpack.progress";
/**
* Get the set of this engine's properties. This set is
* a "live view", so that changing its
* contents immediately affects the Packer engine, and
* changes from the engine (such as progress indications)
* are immediately visible in the map.
*
* <p>The property map may contain pre-defined implementation
* specific and default properties. Users are encouraged to
* read the information and fully understand the implications,
* before modifying pre-existing properties.
* <p>
* Implementation specific properties are prefixed with a
* package name associated with the implementor, beginning
* with <tt>com. or a similar prefix.
* All property names beginning with <tt>pack. and
* <tt>unpack. are reserved for use by this API.
* <p>
* Unknown properties may be ignored or rejected with an
* unspecified error, and invalid entries may cause an
* unspecified error to be thrown.
*
* @return A sorted association of option key strings to option values.
*/
SortedMap<String,String> properties();
/**
* Read a Pack200 archive, and write the encoded JAR to
* a JarOutputStream.
* The entire contents of the input stream will be read.
* It may be more efficient to read the Pack200 archive
* to a file and pass the File object, using the alternate
* method described below.
* <p>
* Closes its input but not its output. (The output can accumulate more elements.)
* @param in an InputStream.
* @param out a JarOutputStream.
* @exception IOException if an error is encountered.
*/
void unpack(InputStream in, JarOutputStream out) throws IOException;
/**
* Read a Pack200 archive, and write the encoded JAR to
* a JarOutputStream.
* <p>
* Does not close its output. (The output can accumulate more elements.)
* @param in a File.
* @param out a JarOutputStream.
* @exception IOException if an error is encountered.
*/
void unpack(File in, JarOutputStream out) throws IOException;
/**
* Registers a listener for PropertyChange events on the properties map.
* This is typically used by applications to update a progress bar.
*
* <p> The default implementation of this method does nothing and has
* no side-effects.</p>
*
* <p>WARNING: This method is omitted from the interface
* declaration in all subset Profiles of Java SE that do not include
* the {@code java.beans} package. </p>
*
* @see #properties
* @see #PROGRESS
* @param listener An object to be invoked when a property is changed.
* @deprecated The dependency on {@code PropertyChangeListener} creates
* a significant impediment to future modularization of the
* Java platform. This method will be removed in a future
* release.
* Applications that need to monitor progress of the
* unpacker can poll the value of the {@link #PROGRESS
* PROGRESS} property instead.
*/
@Deprecated
default void addPropertyChangeListener(PropertyChangeListener listener) {
}
/**
* Remove a listener for PropertyChange events, added by
* the {@link #addPropertyChangeListener}.
*
* <p> The default implementation of this method does nothing and has
* no side-effects.</p>
*
* <p>WARNING: This method is omitted from the interface
* declaration in all subset Profiles of Java SE that do not include
* the {@code java.beans} package. </p>
*
* @see #addPropertyChangeListener
* @param listener The PropertyChange listener to be removed.
* @deprecated The dependency on {@code PropertyChangeListener} creates
* a significant impediment to future modularization of the
* Java platform. This method will be removed in a future
* release.
*/
@Deprecated
default void removePropertyChangeListener(PropertyChangeListener listener) {
}
}
// Private stuff....
private static final String PACK_PROVIDER = "java.util.jar.Pack200.Packer";
private static final String UNPACK_PROVIDER = "java.util.jar.Pack200.Unpacker";
private static Class<?> packerImpl;
private static Class<?> unpackerImpl;
private synchronized static Object newInstance(String prop) {
String implName = "(unknown)";
try {
Class<?> impl = (PACK_PROVIDER.equals(prop))? packerImpl: unpackerImpl;
if (impl == null) {
// The first time, we must decide which class to use.
implName = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction(prop,""));
if (implName != null && !implName.equals(""))
impl = Class.forName(implName);
else if (PACK_PROVIDER.equals(prop))
impl = com.sun.java.util.jar.pack.PackerImpl.class;
else
impl = com.sun.java.util.jar.pack.UnpackerImpl.class;
}
// We have a class. Now instantiate it.
return impl.newInstance();
} catch (ClassNotFoundException e) {
throw new Error("Class not found: " + implName +
":\ncheck property " + prop +
" in your properties file.", e);
} catch (InstantiationException e) {
throw new Error("Could not instantiate: " + implName +
":\ncheck property " + prop +
" in your properties file.", e);
} catch (IllegalAccessException e) {
throw new Error("Cannot access class: " + implName +
":\ncheck property " + prop +
" in your properties file.", e);
}
}
}
Other Java examples (source code examples)Here is a short list of links related to this Java Pack200.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.