KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > file > JarStreamBrowser


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util.file;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.jar.JarInputStream JavaDoc;
26 import java.util.jar.JarEntry JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32
33 /**
34  * Comment
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 1958 $
38  */

39 public class JarStreamBrowser implements Iterator JavaDoc
40 {
41 // ZipFile zip;
42
// Enumeration entries;
43
JarInputStream JavaDoc jar;
44    JarEntry JavaDoc next;
45    ArchiveBrowser.Filter filter;
46
47    public JarStreamBrowser(File JavaDoc file, ArchiveBrowser.Filter filter) throws IOException JavaDoc
48    {
49       this(new FileInputStream JavaDoc(file), filter);
50    }
51
52    public JarStreamBrowser(InputStream JavaDoc is, ArchiveBrowser.Filter filter) throws IOException JavaDoc
53    {
54       this.filter = filter;
55       jar = new JarInputStream JavaDoc(is);
56       setNext();
57    }
58
59    public boolean hasNext()
60    {
61       return next != null;
62    }
63
64    private void setNext()
65    {
66       try
67       {
68          if (next != null) jar.closeEntry();
69          next = null;
70          do
71          {
72             next = jar.getNextJarEntry();
73          } while (next != null && (next.isDirectory() || !filter.accept(next.getName())));
74          if (next == null) jar.close();
75       }
76       catch (IOException JavaDoc e)
77       {
78          throw new RuntimeException JavaDoc("failed to browse jar", e);
79       }
80    }
81
82    public Object JavaDoc next()
83    {
84       int size = (int) next.getSize();
85       byte[] buf = new byte[size];
86       int count = 0;
87       int current = 0;
88       try
89       {
90          while ((
91                  (
92                          current = jar.read(buf, count,
93                                  size - count)
94                  ) != -1
95          ) && (count < size))
96          {
97             count += current;
98          }
99          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(buf);
100          setNext();
101          return bais;
102       }
103       catch (IOException JavaDoc e)
104       {
105          try
106          {
107             jar.close();
108          }
109          catch (IOException JavaDoc ignored)
110          {
111
112          }
113          throw new RuntimeException JavaDoc(e);
114       }
115    }
116
117    public void remove()
118    {
119       throw new RuntimeException JavaDoc("Illegal operation on ArchiveBrowser");
120    }
121 }
122
Popular Tags