KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.Stack JavaDoc;
13 import java.util.zip.ZipEntry JavaDoc;
14 import java.util.zip.ZipInputStream JavaDoc;
15
16 import org.ejtools.archive.Archive;
17 import org.ejtools.archive.Entry;
18 import org.ejtools.archive.JarArchive;
19 import org.ejtools.archive.JarEntry;
20
21
22 /**
23  * Reader implementation based on stream.
24  * <p>Each Archive and each Entry will be created through a ZipInputStream. This classe serves as parent for derived ones.</p>
25  * <p>As the structure is dynamically created, the Visitor Pattern cannot be strictly followed.</p>
26  *
27  * @author Laurent Etiemble
28  * @version $Revision: 1.2 $
29  */

30 public abstract class StreamReader implements Reader
31 {
32    /** Stacks of the current input streams */
33    private Stack JavaDoc inStreams = new Stack JavaDoc();
34
35
36    /**
37     * Visit an entry. Do nothing.
38     *
39     * @param entry The entry to visit
40     */

41    public void visit(Entry JavaDoc entry) { }
42
43
44    /**
45     * Visit an archive. Each Archive will be build from a ZipInputStream and populated with entries.
46     *
47     * @param archive The archive to visit
48     */

49    public void visit(Archive archive)
50    {
51       try
52       {
53          ZipInputStream JavaDoc stream = this.getZipInputStream();
54          if (stream == null)
55          {
56             throw new IllegalStateException JavaDoc("No initial input stream");
57          }
58
59          ZipEntry JavaDoc ze = null;
60          while ((ze = stream.getNextEntry()) != null)
61          {
62             if ((ze != null) && (!ze.isDirectory()))
63             {
64                Entry JavaDoc entry = this.createEntry(ze.getName());
65                if (entry != null)
66                {
67                   entry.addTo(archive);
68                }
69             }
70             stream.closeEntry();
71          }
72          stream.close();
73       }
74       catch (IOException JavaDoc ioe)
75       {
76          ioe.printStackTrace();
77       }
78    }
79
80
81    /**
82     * Helper method to create an entry.
83     *
84     * @param uri The URI of the entry
85     * @return The new Entry
86     */

87    protected Entry JavaDoc createEntry(String JavaDoc uri)
88    {
89       try
90       {
91          ZipInputStream JavaDoc stream = this.getZipInputStream();
92          if (stream == null)
93          {
94             throw new IllegalStateException JavaDoc("No initial input stream");
95          }
96
97          byte[] buffer = new byte[1024];
98          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
99          int read = 0;
100          while ((read = stream.read(buffer)) > 0)
101          {
102             baos.write(buffer, 0, read);
103          }
104          byte[] content = baos.toByteArray();
105
106          try
107          {
108             ZipInputStream JavaDoc jis = new ZipInputStream JavaDoc(new ByteArrayInputStream JavaDoc(content));
109             ZipEntry JavaDoc ze = jis.getNextEntry();
110             jis.close();
111             if (ze != null)
112             {
113                Archive archive = new JarArchive(uri);
114                //
115
// Archive should not have its content !!!
116
//
117
// archive.setContent(content);
118
this.pushZipInputStream(new ZipInputStream JavaDoc(new ByteArrayInputStream JavaDoc(content)));
119                archive.accept(this);
120                this.popZipInputStream();
121
122                return archive;
123             }
124             else
125             {
126                Entry JavaDoc entry = new JarEntry(uri);
127                entry.setContent(content);
128                return entry;
129             }
130          }
131          catch (Exception JavaDoc ioe)
132          {
133             Entry JavaDoc entry = new JarEntry(uri);
134             entry.setContent(content);
135             return entry;
136          }
137       }
138       catch (IOException JavaDoc ioe)
139       {
140          ioe.printStackTrace();
141       }
142       return null;
143    }
144
145
146    /**
147     * Returns the current stream
148     *
149     * @return The current stream
150     */

151    protected ZipInputStream JavaDoc getZipInputStream()
152    {
153       return (ZipInputStream JavaDoc) this.inStreams.lastElement();
154    }
155
156
157    /**
158     * Pops the last entered stream
159     *
160     * @return The stream
161     * @exception IOException If no stream is available
162     */

163    protected ZipInputStream JavaDoc popZipInputStream()
164       throws IOException JavaDoc
165    {
166       if (this.inStreams.isEmpty())
167       {
168          return null;
169       }
170       else
171       {
172          return (ZipInputStream JavaDoc) this.inStreams.pop();
173       }
174    }
175
176
177    /**
178     * Pushes a stream onto the stack
179     *
180     * @param stream The stream
181     * @exception IOException Shoud never occured
182     */

183    protected void pushZipInputStream(ZipInputStream JavaDoc stream)
184       throws IOException JavaDoc
185    {
186       this.inStreams.push(stream);
187    }
188 }
189
Popular Tags