Friday, February 06, 2004

J2SE1.5: javax.pack is awesome but ..

javax.pack.Pack200 which transforms JAR file to or from a packed stream in Pack200 format is really awsome. When Pack200 is used over rt.jar with the default example code below, its size is reduced from 35,804,926 bytes to 10,114,786 bytes. There is one gotcha though. P200 in J2SE1.5beta writes to System.out. Here are the messages written to System.out when rt.jar is compressed:


*** Error at input offset 343 of java/lang/Deprecated.class{*}
Warning: Passing class file uncompressed due to unrecognized attribute: java/lang/Deprecated.class
javax.pack.Attribute$FormatException: class.RuntimeVisibleAnnotations: passing attribute bitwise in java/lang/Deprecated
*** Error at input offset 267 of java/lang/Overrides.class{*}
Warning: Passing class file uncompressed due to unrecognized attribute: java/lang/Overrides.class
javax.pack.Attribute$FormatException: class.RuntimeVisibleAnnotations: passing attribute bitwise in java/lang/Overrides
*** Error at input offset 322 of java/lang/annotation/Documented.class{*}
Warning: Passing class file uncompressed due to unrecognized attribute: java/lang/annotation/Documented.class
javax.pack.Attribute$FormatException: class.RuntimeVisibleAnnotations: passing attribute bitwise in java/lang/annotation/Documented
*** Error at input offset 439 of java/lang/annotation/Inherited.class{*}
Warning: Passing class file uncompressed due to unrecognized attribute: java/lang/annotation/Inherited.class
javax.pack.Attribute$FormatException: class.RuntimeVisibleAnnotations: passing attribute bitwise in java/lang/annotation/Inherited
*** Error at input offset 457 of java/lang/annotation/Retention.class{*}
Warning: Passing class file uncompressed due to unrecognized attribute: java/lang/annotation/Retention.class
javax.pack.Attribute$FormatException: class.RuntimeVisibleAnnotations: passing attribute bitwise in java/lang/annotation/Retention
*** Error at input offset 451 of java/lang/annotation/Target.class{*}
Warning: Passing class file uncompressed due to unrecognized attribute: java/lang/annotation/Target.class
javax.pack.Attribute$FormatException: class.RuntimeVisibleAnnotations: passing attribute bitwise in java/lang/annotation/Target


But these doesn't really seem to be the errors though when P200.unpack is used on the packed rt.jar, the file generated is identical to original rt.jar. Hopefully this spitting to System.out will go away in final realease.

Code to Pack a Jar file:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.jar.JarFile;
import javax.pack.Pack200;

public class Pack200Test {

public static void main( String args[]) throws IOException {

Pack200 p200 = new Pack200();

// Initialize the state by setting the desired properties
Map p = p200.getProperties();
p.put(Pack200.PACK_EFFORT, "7");
p.put(Pack200.PACK_SEGMENT_LIMIT, "-1");
p.put(Pack200.PACK_KEEP_FILE_ORDER, Pack200.FALSE);
p.put(Pack200.PACK_MODIFICATION_TIME, Pack200.LATEST);
p.put(Pack200.PACK_DEFLATE_HINT, Pack200.FALSE);
p.put(Pack200.PACK_STRIP_DEBUG, Pack200.FALSE);
p.put(Pack200.PACK_UNKNOWN_ATTRIBUTE, Pack200.ERROR);

//Pack the jar file
JarFile jar = new JarFile(args[0]);
FileOutputStream out = new FileOutputStream(args[1]);
p200.pack(jar, out);

}

}


Code for Unpacking a jar file:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.jar.JarOutputStream;
import javax.pack.Pack200;

public class UnPack200Test {

public static void main( String args[]) throws IOException {

Pack200 p200 = new Pack200();

// Initialize the state by setting the desired properties
Map p = p200.getProperties();
p.put(Pack200.PACK_EFFORT, "7");
p.put(Pack200.PACK_SEGMENT_LIMIT, "-1");
p.put(Pack200.PACK_KEEP_FILE_ORDER, Pack200.FALSE);
p.put(Pack200.PACK_MODIFICATION_TIME, Pack200.LATEST);
p.put(Pack200.PACK_DEFLATE_HINT, Pack200.FALSE);
p.put(Pack200.PACK_STRIP_DEBUG, Pack200.FALSE);
p.put(Pack200.PACK_UNKNOWN_ATTRIBUTE, Pack200.ERROR);

//Unpack the jar file
File file = new File(args[0]);
JarOutputStream out = new JarOutputStream(new FileOutputStream(args[1]));
p200.unpack(file, out);

}

}

No comments: