commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r732325 - in /commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress: archivers/ArchiveStreamFactory.java archivers/jar/JarArchiveInputStream.java compressors/CompressorStreamFactory.java
Date Wed, 07 Jan 2009 13:16:03 GMT
Author: tcurdt
Date: Wed Jan  7 05:16:01 2009
New Revision: 732325

URL: http://svn.apache.org/viewvc?rev=732325&view=rev
Log:
applied patch from Christian Grobmeier

https://issues.apache.org/jira/browse/SANDBOX-262


Modified:
    commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
    commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
    commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java

Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java?rev=732325&r1=732324&r2=732325&view=diff
==============================================================================
--- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
(original)
+++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
Wed Jan  7 05:16:01 2009
@@ -21,12 +21,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
 
 import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
 import org.apache.commons.compress.archivers.ar.ArArchiveOutputStream;
@@ -36,97 +30,44 @@
 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
-import org.apache.commons.compress.utils.ReflectionUtils;
 
-public class ArchiveStreamFactory {
-
-	final Map inputStreamClasses = new HashMap();
-	final Map outputStreamClasses = new HashMap();
-	
-	public ArchiveStreamFactory() throws ArchiveException {
-		registerArchiveInputStream("zip", ZipArchiveInputStream.class);
-		registerArchiveOutputStream("zip", ZipArchiveOutputStream.class);
-
-        registerArchiveInputStream("tar", TarArchiveInputStream.class);
-        registerArchiveOutputStream("tar", TarArchiveOutputStream.class);
+/**
+ * Factory to create Archive[In|Out]putStreams from names
+ * or the first bytes of the InputStream. In order add other
+ * implementations you should extend ArchiveStreamFactory
+ * and override the appropriate methods (and call their implementation
+ * from super of course)
+ * 
+ * TODO add example here 
+ * 
+ */
 
-        registerArchiveInputStream("ar", ArArchiveInputStream.class);
-        registerArchiveOutputStream("ar", ArArchiveOutputStream.class);
-
-        registerArchiveInputStream("jar", JarArchiveInputStream.class);		
-        registerArchiveOutputStream("jar", JarArchiveOutputStream.class);
-	}
-	
-	
-	public void registerArchiveInputStream( final String name, final Class stream ) throws ArchiveException
{
-		if (ArchiveInputStream.class.isAssignableFrom(stream) && !(stream.isInterface()))
{
-			inputStreamClasses.put(name, stream);
-        } else {
-            throw new ArchiveException("Archive does not implement the ArchiveInputStream
interface.");
-        }	
-	}
+public class ArchiveStreamFactory {
 
-	public void registerArchiveOutputStream( final String name, final Class stream ) throws
ArchiveException {
-		ReflectionUtils.registerClazz(outputStreamClasses, name, ArchiveOutputStream.class, stream);
	
-		if (ArchiveOutputStream.class.isAssignableFrom(stream) && !(stream.isInterface()))
{
-			outputStreamClasses.put(name, stream);
-        } else {
-            throw new ArchiveException("Archive does not implement the ArchiveOutputStream
interface.");
-        }
-	}
-	
-    public ArchiveInputStream createArchiveInputStream( final String archiverName, final
InputStream out ) throws ArchiveException {
-        try {
-            final Class clazz = (Class) inputStreamClasses.get(archiverName);
-
-            if (clazz == null) {
-            	throw new ArchiveException("ArchiverFactory could not create instance");
-            }
-
-            final Class[] params = { InputStream.class };
-            final Constructor constructor = clazz.getConstructor(params);
-            final Object[] initargs = { out };
-            return (ArchiveInputStream) constructor.newInstance(initargs);
-        } catch (InstantiationException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (IllegalAccessException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (SecurityException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (NoSuchMethodException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (IllegalArgumentException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (InvocationTargetException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
+    public ArchiveInputStream createArchiveInputStream( final String archiverName, final
InputStream in ) throws ArchiveException {
+    	if ("ar".equalsIgnoreCase(archiverName)) {
+            return new ArArchiveInputStream(in);
+        } else if("zip".equalsIgnoreCase(archiverName)) {
+        	return new ZipArchiveInputStream(in);
+        } else if("tar".equalsIgnoreCase(archiverName)) {
+        	return new TarArchiveInputStream(in);
+        } else if("jar".equalsIgnoreCase(archiverName)) {
+        	return new JarArchiveInputStream(in);
         }
+    	return null;
     }
 
     public ArchiveOutputStream createArchiveOutputStream( final String archiverName, final
OutputStream out ) throws ArchiveException {
-        try {
-            final Class clazz = (Class) outputStreamClasses.get(archiverName);
-            
-            if (clazz == null) {
-            	throw new ArchiveException("ArchiverFactory could not create instance");
-            }
-            
-            final Class[] params = { OutputStream.class };
-            final Constructor constructor = clazz.getConstructor(params);
-            final Object[] initargs = { out };
-            return (ArchiveOutputStream) constructor.newInstance(initargs);
-        } catch (InstantiationException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (IllegalAccessException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (SecurityException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (NoSuchMethodException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (IllegalArgumentException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
-        } catch (InvocationTargetException e) {
-            throw new ArchiveException("ArchiverFactory could not create instance", e);
+    	if ("ar".equalsIgnoreCase(archiverName)) {
+            return new ArArchiveOutputStream(out);
+        } else if("zip".equalsIgnoreCase(archiverName)) {
+        	return new ZipArchiveOutputStream(out);
+        } else if("tar".equalsIgnoreCase(archiverName)) {
+        	return new TarArchiveOutputStream(out);
+        } else if("jar".equalsIgnoreCase(archiverName)) {
+        	return new JarArchiveOutputStream(out);
         }
+    	return null;
     }
 
     public ArchiveInputStream createArchiveInputStream( final InputStream input ) throws
IOException {
@@ -134,36 +75,18 @@
 		final byte[] signature = new byte[12];
 		input.mark(signature.length);
 		input.read(signature);
-		// reset not supported exception?
+		// TODO if reset is not supported pass on the IOException or return null?
 		input.reset();
 
-//		for (int i = 0; i < signature.length; i++) {
-//			System.out.print(Integer.toHexString(signature[i]));
-//			System.out.print(",");
-//		}
-//		System.out.println("");
-		
-		for (Iterator it = inputStreamClasses.values().iterator(); it.hasNext();) {
-			final Class clazz = (Class) it.next();
-			try {
-				final Method method = clazz.getMethod("matches", new Class[] { byte[].class });
-				
-				final Object result = method.invoke(null, new Object[] { signature } );
-				
-				if (result.equals(Boolean.TRUE)) {
-		            final Class[] params = { InputStream.class };
-		            final Constructor constructor = clazz.getConstructor(params);
-		            final Object[] initargs = { input };
-		            return (ArchiveInputStream) constructor.newInstance(initargs);					
-				}
-			} catch (SecurityException e) {
-			} catch (NoSuchMethodException e) {
-			} catch (IllegalArgumentException e) {
-			} catch (IllegalAccessException e) {
-			} catch (InvocationTargetException e) {
-			} catch (InstantiationException e) {
-			}
-		}
+		if(ZipArchiveInputStream.matches(signature)) {
+			return new ZipArchiveInputStream(input);
+		} else if(JarArchiveInputStream.matches(signature)) {
+			return new JarArchiveInputStream(input);
+		} else if(TarArchiveInputStream.matches(signature)) {
+			return new TarArchiveInputStream(input);
+		} else if(ArArchiveInputStream.matches(signature)) {
+			return new ArArchiveInputStream(input);
+		} 
 		return null;
 	}
 }

Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java?rev=732325&r1=732324&r2=732325&view=diff
==============================================================================
--- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
(original)
+++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
Wed Jan  7 05:16:01 2009
@@ -27,7 +27,7 @@
 
 public class JarArchiveInputStream extends ZipArchiveInputStream {
 
-	public JarArchiveInputStream( final InputStream inputStream ) throws IOException {
+	public JarArchiveInputStream( final InputStream inputStream ) {
 		super(inputStream);
 	}
 	

Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java?rev=732325&r1=732324&r2=732325&view=diff
==============================================================================
--- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
(original)
+++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
Wed Jan  7 05:16:01 2009
@@ -18,98 +18,54 @@
  */
 package org.apache.commons.compress.compressors;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
-import java.util.Map;
 
-import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
 
+/**
+ * Factory to create Compressor[In|Out]putStreams from names
+ * In order add other implementations you should extend CompressorStreamFactory
+ * and override the appropriate methods (and call their implementation
+ * from super of course)
+ * 
+ * TODO add example here 
+ * 
+ */
 public class CompressorStreamFactory {
-	final Map inputStreamClasses = new HashMap();
-	final Map outputStreamClasses = new HashMap();
-	
-	public CompressorStreamFactory() throws CompressorException {
-		registerInputStream("gz", GzipCompressorInputStream.class);
-		registerOutputStream("gz", GzipCompressorOutputStream.class);
-		registerInputStream("bzip2", BZip2CompressorInputStream.class);
-		registerOutputStream("bzip2", BZip2CompressorOutputStream.class);
+
+	public CompressorInputStream createCompressorInputStream( final String name, final InputStream
in ) throws CompressorException {
+		try {
+
+			if("gz".equalsIgnoreCase(name)) {
+				return new GzipCompressorInputStream(in);
+			} else if("bzip2".equalsIgnoreCase(name)) {
+				return new BZip2CompressorInputStream(in);
+			} 
+
+			return null;
 		
-	}
-	
-	public void registerInputStream( final String name, final Class stream ) throws CompressorException
{
-		if (CompressorInputStream.class.isAssignableFrom(stream) && !(stream.isInterface()))
{
-			inputStreamClasses.put(name, stream);
-        } else {
-            throw new CompressorException("Compressor does not implement the CompressorInputStream
interface.");
-        }	
-	}
-
-	public void registerOutputStream( final String name, final Class stream ) throws CompressorException
{
-		if (CompressorOutputStream.class.isAssignableFrom(stream) && !(stream.isInterface()))
{
-			outputStreamClasses.put(name, stream);
-        } else {
-            throw new CompressorException("Compressor does not implement the CompressorOutputStream
interface.");
-        }
-	}
-	
-	public CompressorInputStream createCompressorInputStream( final String name, final InputStream
out ) throws CompressorException {
-        try {
-            final Class clazz = (Class) inputStreamClasses.get(name);
-
-            if (clazz == null) {
-            	throw new CompressorException("CompressorFactory could not create instance");
-            }
-
-            final Class[] params = { InputStream.class };
-            final Constructor constructor = clazz.getConstructor(params);
-            final Object[] initargs = { out };
-            return (CompressorInputStream) constructor.newInstance(initargs);
-        } catch (InstantiationException e) {
-            throw new CompressorException("CompressorFactory could not create instance",
e);
-        } catch (IllegalAccessException e) {
-            throw new CompressorException("CompressorFactory could not create instance",
e);
-        } catch (SecurityException e) {
-            throw new CompressorException("CompressorFactory could not create instance",
e);
-        } catch (NoSuchMethodException e) {
-            throw new CompressorException("CompressorFactory could not create instance",
e);
-        } catch (IllegalArgumentException e) {
-            throw new CompressorException("CompressorFactory could not create instance",
e);
-        } catch (InvocationTargetException e) {
-            throw new CompressorException("CompressorFactory could not create instance",
e);
-        }
+		} catch (IOException e) {
+			throw new CompressorException("Could not create CompressorInputStream", e);
+		}
     }
 
-    public CompressorOutputStream createCompressorOutputStream( final String name, final
OutputStream out ) throws ArchiveException {
-        try {
-            final Class clazz = (Class) outputStreamClasses.get(name);
-            
-            if (clazz == null) {
-            	throw new ArchiveException("CompressorFactory could not create instance");
-            }
-            
-            final Class[] params = { OutputStream.class };
-            final Constructor constructor = clazz.getConstructor(params);
-            final Object[] initargs = { out };
-            return (CompressorOutputStream) constructor.newInstance(initargs);
-        } catch (InstantiationException e) {
-            throw new ArchiveException("CompressorFactory could not create instance", e);
-        } catch (IllegalAccessException e) {
-            throw new ArchiveException("CompressorFactory could not create instance", e);
-        } catch (SecurityException e) {
-            throw new ArchiveException("CompressorFactory could not create instance", e);
-        } catch (NoSuchMethodException e) {
-            throw new ArchiveException("CompressorFactory could not create instance", e);
-        } catch (IllegalArgumentException e) {
-            throw new ArchiveException("CompressorFactory could not create instance", e);
-        } catch (InvocationTargetException e) {
-            throw new ArchiveException("CompressorFactory could not create instance", e);
-        }
+    public CompressorOutputStream createCompressorOutputStream( final String name, final
OutputStream out ) throws CompressorException {
+		try {
+
+			if("gz".equalsIgnoreCase(name)) {
+					return new GzipCompressorOutputStream(out);
+			} else if("bzip2".equalsIgnoreCase(name)) {
+					return new BZip2CompressorOutputStream(out);
+			} 
+			return null;
+
+		} catch (IOException e) {
+			throw new CompressorException("Could not create CompressorOutputStream", e);
+		}
     }
 }



Mime
View raw message