[jboss-svn-commits] JBoss Common SVN: r2129 - common-core/trunk/src/main/java/org/jboss/util/file

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Oct 13 10:58:24 EDT 2006


Author: bill.burke at jboss.com
Date: 2006-10-13 10:58:22 -0400 (Fri, 13 Oct 2006)
New Revision: 2129

Added:
   common-core/trunk/src/main/java/org/jboss/util/file/JarStreamBrowser.java
Modified:
   common-core/trunk/src/main/java/org/jboss/util/file/ArchiveBrowser.java
   common-core/trunk/src/main/java/org/jboss/util/file/JarArchiveBrowser.java
Log:
update archive browser to support jar: urls.

Modified: common-core/trunk/src/main/java/org/jboss/util/file/ArchiveBrowser.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/file/ArchiveBrowser.java	2006-10-13 14:56:54 UTC (rev 2128)
+++ common-core/trunk/src/main/java/org/jboss/util/file/ArchiveBrowser.java	2006-10-13 14:58:22 UTC (rev 2129)
@@ -22,9 +22,11 @@
 package org.jboss.util.file;
 
 import java.io.File;
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URISyntaxException;
-import java.net.URI;
 import java.util.Iterator;
 
 /**
@@ -51,7 +53,7 @@
          }
          catch (URISyntaxException e)
          {
-            throw new RuntimeException( "Not a valid URL: " + url, e );
+            throw new RuntimeException("Not a valid URL: " + url, e);
          }
          if (f.isDirectory())
          {
@@ -62,9 +64,32 @@
             return new JarArchiveBrowser(f, filter);
          }
       }
-      else
+      else if (url.getProtocol().startsWith("jar"))
       {
-         throw new RuntimeException("NOT IMPLEMENTED");
+         if (url.toString().endsWith("!/"))
+         {
+            try
+            {
+               return new JarArchiveBrowser((JarURLConnection) url.openConnection(), filter);
+            }
+            catch (IOException e)
+            {
+               throw new RuntimeException(e);
+            }
+         }
+         else
+         {
+            try
+            {
+               return new JarStreamBrowser(url.openStream(), filter);
+            }
+            catch (IOException e)
+            {
+               throw new RuntimeException(e);
+            }
+         }
+
       }
+      else throw new RuntimeException("Archive browser cannot handle protocol: " + url);
    }
 }

Modified: common-core/trunk/src/main/java/org/jboss/util/file/JarArchiveBrowser.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/file/JarArchiveBrowser.java	2006-10-13 14:56:54 UTC (rev 2128)
+++ common-core/trunk/src/main/java/org/jboss/util/file/JarArchiveBrowser.java	2006-10-13 14:58:22 UTC (rev 2129)
@@ -23,10 +23,14 @@
 
 import java.util.Iterator;
 import java.util.Enumeration;
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipEntry;
 import java.io.File;
 import java.io.IOException;
+import java.net.URL;
+import java.net.JarURLConnection;
 
 /**
  * Comment
@@ -37,17 +41,32 @@
  **/
 public class JarArchiveBrowser implements Iterator
 {
-   ZipFile zip;
+   JarFile zip;
    Enumeration entries;
-   ZipEntry next;
+   JarEntry next;
    ArchiveBrowser.Filter filter;
 
+   public JarArchiveBrowser(JarURLConnection url,  ArchiveBrowser.Filter filter)
+   {
+      this.filter = filter;
+      try
+      {
+         zip = url.getJarFile();
+         entries = zip.entries();
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
+      }
+      setNext();
+   }
+
    public JarArchiveBrowser(File f, ArchiveBrowser.Filter filter)
    {
       this.filter = filter;
       try
       {
-         zip = new ZipFile(f);
+         zip = new JarFile(f);
          entries = zip.entries();
       }
       catch (IOException e)
@@ -69,7 +88,7 @@
       {
          do
          {
-            next = (ZipEntry) entries.nextElement();
+            next = (JarEntry)entries.nextElement();
          } while (entries.hasMoreElements() && next.isDirectory());
          if (next.isDirectory()) next = null;
 

Added: common-core/trunk/src/main/java/org/jboss/util/file/JarStreamBrowser.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/file/JarStreamBrowser.java	2006-10-13 14:56:54 UTC (rev 2128)
+++ common-core/trunk/src/main/java/org/jboss/util/file/JarStreamBrowser.java	2006-10-13 14:58:22 UTC (rev 2129)
@@ -0,0 +1,121 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.util.file;
+
+import java.util.Iterator;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarEntry;
+import java.io.File;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @version $Revision: 1958 $
+ */
+public class JarStreamBrowser implements Iterator
+{
+//   ZipFile zip;
+//   Enumeration entries;
+   JarInputStream jar;
+   JarEntry next;
+   ArchiveBrowser.Filter filter;
+
+   public JarStreamBrowser(File file, ArchiveBrowser.Filter filter) throws IOException
+   {
+      this(new FileInputStream(file), filter);
+   }
+
+   public JarStreamBrowser(InputStream is, ArchiveBrowser.Filter filter) throws IOException
+   {
+      this.filter = filter;
+      jar = new JarInputStream(is);
+      setNext();
+   }
+
+   public boolean hasNext()
+   {
+      return next != null;
+   }
+
+   private void setNext()
+   {
+      try
+      {
+         if (next != null) jar.closeEntry();
+         next = null;
+         do
+         {
+            next = jar.getNextJarEntry();
+         } while (next != null && (next.isDirectory() || !filter.accept(next.getName())));
+         if (next == null) jar.close();
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException("failed to browse jar", e);
+      }
+   }
+
+   public Object next()
+   {
+      int size = (int) next.getSize();
+      byte[] buf = new byte[size];
+      int count = 0;
+      int current = 0;
+      try
+      {
+         while ((
+                 (
+                         current = jar.read(buf, count,
+                                 size - count)
+                 ) != -1
+         ) && (count < size))
+         {
+            count += current;
+         }
+         ByteArrayInputStream bais = new ByteArrayInputStream(buf);
+         setNext();
+         return bais;
+      }
+      catch (IOException e)
+      {
+         try
+         {
+            jar.close();
+         }
+         catch (IOException ignored)
+         {
+
+         }
+         throw new RuntimeException(e);
+      }
+   }
+
+   public void remove()
+   {
+      throw new RuntimeException("Illegal operation on ArchiveBrowser");
+   }
+}




More information about the jboss-svn-commits mailing list