KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > archive > io > StreamWriter


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.archive.io;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Stack JavaDoc;
13 import java.util.jar.JarEntry JavaDoc;
14 import java.util.zip.ZipOutputStream JavaDoc;
15
16 import org.ejtools.archive.Archive;
17 import org.ejtools.archive.Entry;
18
19 /**
20  * Writer implementation based on stream. Each Archive and each Entry will be output through a ZipOutputStream. This classe serves as parent for derived ones.
21  *
22  * @author Laurent Etiemble
23  * @version $Revision: 1.2 $
24  */

25 public abstract class StreamWriter implements Writer
26 {
27    /** Stacks of the current output streams */
28    private Stack JavaDoc outStreams = new Stack JavaDoc();
29
30
31    /**
32     * Visit an archive. A new ZipOutputStream is created for each Archive.
33     *
34     * @param archive The archive to visit
35     */

36    public void visit(Archive archive)
37    {
38       try
39       {
40          ZipOutputStream JavaDoc stream = this.getZipOutputStream();
41          if (stream == null)
42          {
43             throw new IllegalStateException JavaDoc("No initial input stream");
44          }
45
46          // For each archive, output its content
47
for (Iterator JavaDoc iterator = archive.iterator(); iterator.hasNext(); )
48          {
49             Entry JavaDoc entry = (Entry JavaDoc) iterator.next();
50
51             if (entry.isArchive())
52             {
53                ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
54                ZipOutputStream JavaDoc jos = new ZipOutputStream JavaDoc(baos);
55
56                this.pushZipOutputStream(jos);
57                entry.accept(this);
58                this.popZipOutputStream();
59
60                JarEntry JavaDoc je = new JarEntry JavaDoc(entry.getURI());
61                stream.putNextEntry(je);
62                stream.write(baos.toByteArray());
63             }
64             else
65             {
66                entry.accept(this);
67             }
68          }
69          stream.close();
70       }
71       catch (IOException JavaDoc ioe)
72       {
73          ioe.printStackTrace();
74       }
75    }
76
77
78    /**
79     * Visit an entry.
80     *
81     * @param entry The entry to visit
82     */

83    public void visit(Entry JavaDoc entry)
84    {
85       try
86       {
87          // Gets the current stream and dump the content
88
ZipOutputStream JavaDoc stream = this.getZipOutputStream();
89          if (stream == null)
90          {
91             throw new IllegalStateException JavaDoc("No initial input stream");
92          }
93          JarEntry JavaDoc je = new JarEntry JavaDoc(entry.getURI());
94          stream.putNextEntry(je);
95          stream.write(entry.getContent());
96       }
97       catch (IOException JavaDoc ioe)
98       {
99          ioe.printStackTrace();
100       }
101    }
102
103
104    /**
105     * Returns the current stream
106     *
107     * @return The current stream
108     */

109    protected ZipOutputStream JavaDoc getZipOutputStream()
110    {
111       return (ZipOutputStream JavaDoc) this.outStreams.lastElement();
112    }
113
114
115    /**
116     * Pops the last entered stream
117     *
118     * @return The stream
119     * @exception IOException If no stream is available
120     */

121    protected ZipOutputStream JavaDoc popZipOutputStream()
122       throws IOException JavaDoc
123    {
124       if (this.outStreams.isEmpty())
125       {
126          return null;
127       }
128       else
129       {
130          return (ZipOutputStream JavaDoc) this.outStreams.pop();
131       }
132    }
133
134
135    /**
136     * Pushes a stream onto the stack
137     *
138     * @param stream The stream
139     * @exception IOException Shoud never occured
140     */

141    protected void pushZipOutputStream(ZipOutputStream JavaDoc stream)
142       throws IOException JavaDoc
143    {
144       this.outStreams.push(stream);
145    }
146 }
147
Popular Tags