01: /*
02: * @(#)ZipWriterHandler.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.compiler;
10:
11: import java.io.ByteArrayOutputStream;
12: import java.io.DataOutputStream;
13: import java.io.IOException;
14: import java.util.zip.ZipEntry;
15: import java.util.zip.ZipOutputStream;
16:
17: /**
18: * This class is a concrete class of ClassFileHandler.
19: * When this is passed to Compiler.compile(..., ClassFileHandler),
20: * compiled class files are added to the ZipOutputStream specified with the constructor.
21: */
22: public class ZipWriterHandler implements ClassFileHandler {
23: private ZipOutputStream zout;
24: private ByteArrayOutputStream bout;
25: private DataOutputStream dout;
26: private boolean verbose;
27:
28: public ZipWriterHandler(ZipOutputStream zout) {
29: this .zout = zout;
30: bout = new ByteArrayOutputStream();
31: dout = new DataOutputStream(bout);
32: }
33:
34: public void setVerbose(boolean flag) {
35: this .verbose = flag;
36: }
37:
38: protected void handleException(Exception e) {
39: e.printStackTrace();
40: }
41:
42: public Object handle(ClassFile cf) {
43: try {
44: bout.reset();
45: cf.write(dout);
46: ZipEntry entry = new ZipEntry(cf.getClassName().replace(
47: '.', '/')
48: + ".class");
49: zout.putNextEntry(entry);
50: bout.writeTo(zout);
51: if (verbose) {
52: System.out.println(entry);
53: }
54: } catch (IOException e) {
55: handleException(e);
56: }
57: return null;
58: }
59: }
|