[jboss-cvs] JBossAS SVN: r76592 - in projects/vfs/trunk/src/main/java/org/jboss/virtual: plugins/context/zip and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Aug 2 15:45:02 EDT 2008


Author: mstruk
Date: 2008-08-02 15:45:02 -0400 (Sat, 02 Aug 2008)
New Revision: 76592

Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java
Log:
Refactoring - moved stream copying methods to VFSUtils

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java	2008-08-02 12:40:09 UTC (rev 76591)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java	2008-08-02 19:45:02 UTC (rev 76592)
@@ -23,6 +23,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -765,4 +766,64 @@
       }
       return uri;
    }
+
+   /**
+    * Copy input stream to output stream and close them both
+    *
+    * @param is input stream
+    * @param os output stream
+    * @throws IOException for any error
+    */
+   public static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException
+   {
+      try
+      {
+         copyStream(is, os);
+      }
+      finally
+      {
+         if (is != null)
+         {
+            try
+            {
+               is.close();
+            }
+            catch(Exception ignored)
+            {
+            }
+         }
+         if (os != null)
+            os.close();
+      }
+   }
+
+   /**
+    * Copy input stream to output stream without closing streams.
+    * Flushes output stream when done.
+    *
+    * @param is input stream
+    * @param os output stream
+    * @throws IOException for any error
+    */
+   public static void copyStream(InputStream is, OutputStream os) throws IOException
+   {
+      if (is == null)
+         throw new IllegalArgumentException("input stream is null");
+      if (os == null)
+         throw new IllegalArgumentException("output stream is null");
+      try
+      {
+         byte [] buff = new byte[65536];
+         int rc = is.read(buff);
+         while (rc != -1)
+         {
+            os.write(buff, 0, rc);
+            rc = is.read(buff);
+         }
+      }
+      finally
+      {
+         os.flush();
+      }
+   }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java	2008-08-02 12:40:09 UTC (rev 76591)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java	2008-08-02 19:45:02 UTC (rev 76592)
@@ -21,6 +21,8 @@
 */
 package org.jboss.virtual.plugins.context.zip;
 
+import org.jboss.virtual.VFSUtils;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
@@ -53,7 +55,7 @@
    {
       // read the contents into memory buffer
       ByteArrayOutputStream bout = new ByteArrayOutputStream();
-      ZipEntryContext.copyStreamAndClose(zipStream, bout);
+      VFSUtils.copyStreamAndClose(zipStream, bout);
       zipBytes = bout.toByteArray();
 
       // TODO - delegate file meta info operations to parent?

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-08-02 12:40:09 UTC (rev 76591)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-08-02 19:45:02 UTC (rev 76592)
@@ -308,7 +308,7 @@
    protected ZipWrapper findEntry(InputStream is, String relative) throws IOException
    {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
-      ZipEntryContext.copyStreamAndClose(is, baos);
+      VFSUtils.copyStreamAndClose(is, baos);
       ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 
       // first we need to find best/longest name
@@ -448,7 +448,7 @@
 
                   InputStream is = zipSource.openStream(ent);
                   OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
-                  copyStreamAndClose(is, os);
+                  VFSUtils.copyStreamAndClose(is, os);
 
                   // mount another instance of ZipEntryContext
                   delegator = mountZipFile(parent, name, dest);
@@ -1062,43 +1062,12 @@
       }
    }
 
+
+
    //
    //   Helper methods
    //
 
-   /**
-    * Copy input stream to output stream and close them both
-    *
-    * @param is input stream
-    * @param os output stream
-    * @throws IOException for any error
-    */
-   static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException
-   {
-      try
-      {
-         byte [] buff = new byte[65536];
-         int count = is.read(buff);
-         while(count != -1)
-         {
-            os.write(buff, 0, count);
-            count = is.read(buff);
-         }
-      }
-      finally
-      {
-         if(is != null)
-         {
-            try {
-               is.close();
-            }
-            catch(Exception ignored)
-            {
-            }
-         }
-         os.close();
-      }
-   }
 
    /**
     * Make sure url protocol is <em>vfszip</em>

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java	2008-08-02 12:40:09 UTC (rev 76591)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java	2008-08-02 19:45:02 UTC (rev 76592)
@@ -89,7 +89,7 @@
          if (ent.isDirectory() == false)
          {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            copyStream(zis, baos);
+            VFSUtils.copyStream(zis, baos);
             fileBytes = baos.toByteArray();
             ent.setSize(fileBytes.length);
          }
@@ -182,17 +182,6 @@
       zout.close();
    }
 
-   private static void copyStream(InputStream is, OutputStream os) throws IOException
-   {
-      byte [] buff = new byte[4096];
-      int rc = is.read(buff);
-      while (rc != -1)
-      {
-         os.write(buff, 0, rc);
-         rc = is.read(buff);
-      }
-   }
-
    static class InMemoryFile
    {
       ZipEntry entry;




More information about the jboss-cvs-commits mailing list