commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r427890 - /jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/
Date Wed, 02 Aug 2006 04:45:27 GMT
Author: tcurdt
Date: Tue Aug  1 21:45:26 2006
New Revision: 427890

URL: http://svn.apache.org/viewvc?rev=427890&view=rev
Log:
forgot a few files to add: draft 8 from C. Grobmeier, grant has been received, cheers

Added:
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
  (with props)
    jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
  (with props)

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+/**
+ * AbstractCompressor handles all compression/decompression 
+ * actions on an abstract basis. 
+ */
+public abstract class AbstractCompressor 
+	extends PackableObject 
+	implements Compressor {
+	/**
+	 * Constructor
+	 */
+	public AbstractCompressor() {
+		super();
+	}
+
+	/**
+	 * Returns a String with the default file extension
+	 * for this compressor. For example, a zip-files default
+	 * file extension would be "zip" (without leading dot).
+	 *  
+	 * @return the default file extension
+	 */
+	public abstract String getDefaultFileExtension();
+	
+    
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Compressor#compressStream(java.io.FileInputStream)
+	 */
+	public InputStream compress(FileInputStream input) throws CompressException {
+		FileOutputStream outputStream = null;
+		FileOutputStream tempFileOutputStream = null;
+		try {
+			File temp = File.createTempFile("commons_","jkt");;
+			tempFileOutputStream = new FileOutputStream(temp);
+			compressTo(input, tempFileOutputStream);
+			return new FileInputStream(temp);
+		} catch (IOException e) {
+			throw new CompressException("An IO Exception has occured", e);
+		} finally {
+			try {
+				tempFileOutputStream.close();
+				outputStream.close();
+			} catch (IOException e) {
+				throw new CompressException("An IO Exception occured while closing the streams", e);
+			}
+		}
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Compressor#compress(java.io.File, java.io.File)
+	 */
+	public void compressTo(File input, File output) throws CompressException {
+		FileOutputStream outputStream = null;
+		FileInputStream inputStream = null;
+		try {
+			outputStream = new FileOutputStream( output );
+			inputStream = new FileInputStream( input );
+			this.compressTo(inputStream, outputStream);
+		} catch (FileNotFoundException e) {
+			throw new CompressException("File not found" ,e);
+		} 
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Compressor#compress(java.io.File)
+	 */
+	public void compressToHere(File input) throws CompressException {
+		String pathToFile = input.getAbsolutePath().concat(".").concat(getDefaultFileExtension());
+		File output = new File(pathToFile);
+		this.compressTo(input, output);
+	}
+	
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Compressor#compressStream(java.io.File)
+	 */
+	public InputStream compress(File input) throws CompressException {
+		try {
+			return this.compress(
+				new FileInputStream(input));
+		} catch (FileNotFoundException e) {
+			throw new CompressException("File could not be found.",e);
+		}
+	}
+	
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Decompressor#decompress(java.io.File)
+	 */
+	public InputStream decompress(File input) throws CompressException {
+		File temp;
+		InputStream result;
+		try {
+			temp = File.createTempFile("compress_", "jkt");
+			this.decompressTo(input, temp);
+			result = new FileInputStream(temp);
+		} catch (IOException e) {
+			throw new CompressException("Error while creating a temporary file", e);
+		} 
+		return result; 
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Decompressor#decompress(java.io.FileInputStream)
+	 */
+	public InputStream decompress(FileInputStream input) 
+		throws CompressException {
+		File temp;
+		InputStream result;
+		try {
+			temp = File.createTempFile("compress_", "jkt");
+			this.decompressTo(input, new FileOutputStream(temp));
+			result = new FileInputStream(temp);
+		} catch (IOException e) {
+			throw new CompressException("Error while creating a temporary file", e);
+		} 
+		return result;
+	}
+	
+	/* (non-Javadoc)
+	 * @see org.apache.commons.compress.Compressor#decompress(java.io.File, java.io.File)
+	 */
+	public void decompressTo(File input, File output) 
+		throws CompressException {
+		FileInputStream inputStream = null;
+		FileOutputStream outputStream = null;
+		try {
+			outputStream = new FileOutputStream( output );
+			inputStream = new FileInputStream( input );
+			decompressTo(inputStream, outputStream);
+		} catch (FileNotFoundException e) {
+			throw new CompressException("File could not be found", e);
+		} finally {
+	      	try {
+				inputStream.close();
+				outputStream.close();
+			} catch (IOException e1) {
+				throw new CompressException("An I/O Exception has occured while closing a stream", e1);
+			}
+		}
+	}
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/AbstractCompressor.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Iterator;
+/**
+ * Archive is the interface which defines all operations 
+ * for all possible archive-operations.
+ * 
+ * TODO:
+ * - delete files from an archive
+ * - add files to an existing archive
+ * - recursivley add directories
+ */
+public interface Archive {
+	/**
+	 * Adds a file to the internal filelist 
+	 * for a possible pack-operation
+	 */
+	public void add(File file) throws FileNotFoundException;
+	
+	/**
+	 * Adds a FileInputStream to the internal filelist 
+	 * for a possible pack-operation
+	 */
+	public void add(ArchiveEntry entry);
+	
+	/**
+	 * Packs a file. 
+	 * The destination filename must be set manually with setDestinationFile(...).
+	 * There must be at least 1 file to be packed.
+	 * 
+	 * @throws ArchiveException if there is no destination file or files to be packed
+	 * @return true, if the operation has been ended without exceptions
+	 */
+	public void save(FileOutputStream output) throws ArchiveException;
+
+	/**
+	 * Packs this file. 
+	 * This methods ignores what has been set in setDestinationFile(...) and
+	 * uses the filename of the parameter. This string must not be null.
+	 * 
+	 * @throws ArchiveException if there is no destination file or files to be packed
+	 */
+	public void save(File output) throws ArchiveException;
+
+	/**
+	 * Sets an Archive for manipulating. An archive is set if someone
+	 * saves an Archive or calls getInstance(...) with an archive.
+	 * @param archive file to manipulate
+	 */
+	void setArchive(File file);
+	
+	/**
+	 * Returns the archive file and null,
+	 * if this archiver has not been saved yet or
+	 * there has not been set an archive manually.
+	 * @return the archiver, or null
+	 */
+	public File getArchive();
+
+	/**
+	 * Unpacks to the specified directory 
+	 * @param dir to unpack
+	 * @throws UnpackException if an unpack error occurs
+	 */
+	public void unpack(File destinationDir) throws UnpackException;
+	
+	/**
+	 * Get an iterator of ArchiveEntrys which shall be archived
+	 * @return the iterator
+	 */
+	public Iterator getEntryIterator();
+	
+	/**
+	 * Closes this archiver and all internal streams. 
+	 */
+	public void close() throws IOException ;
+}

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Archive.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.compress.archivers.tar.TarArchive;
+import org.apache.commons.compress.archivers.zip.ZipArchive;
+/**
+ * Provides implementations for all ArchiverFactory methods.
+ */
+public class ArchiverFactory {
+	/* internal archiver list */
+	private static List archives;
+	
+	// pre-register archives 
+	static {
+		archives = new ArrayList();
+		try {
+			registerArchiver(TarArchive.class);
+			registerArchiver(ZipArchive.class);
+		} catch (ArchiveException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Constructor.
+	 * @param name name of the implementation
+	 */
+	private ArchiverFactory() { 
+	}
+
+	/**
+	 * Registers a new archiver in the factory.
+	 * The archiver must implement the archiver interface.
+	 * 
+	 * @param className full qualified archiver implementation
+	 * @throws ClassNotFoundException if the new archiver class could not be found
+	 * @throws ArchiveException if the new archiver does not implement the archiver interface
+	 */
+	public static void registerArchiver(String className) 
+		throws ClassNotFoundException, ArchiveException {
+		Class clazz = Class.forName(className);
+		registerArchiver(clazz);
+	}
+	
+	/**
+	 * Registers a new archiver in the factory.
+	 * The archiver must implement the archiver interface and must
+	 * be an concrete implementation
+	 * 
+	 * @param className full qualified archiver implementation
+	 * @throws ArchiveException if the new archiver does not implement the archiver interface
+	 */
+	public static void registerArchiver(Class clazz) 
+		throws ArchiveException {
+		// register only, if the class is assignable and not an interface
+		if(Archive.class.isAssignableFrom(clazz) && !(clazz.isInterface())) {
+			try {
+				archives.add(clazz.newInstance());
+			} catch (InstantiationException e) {
+				throw new ArchiveException("Archive could not be instantiated", e);
+			} catch (IllegalAccessException e) {
+				throw new ArchiveException("Archive could not be instantiated", e);
+			}
+		} else {
+			throw new ArchiveException("Archive does not implement the Archive.class interface.");
+		}
+	}
+	
+	/**
+	 * Returns an empty Archive, if an archiver could be found for this factory.
+	 * If two Archive-implementations with the same name are registered,
+	 * the first matching archiver will be instanciated.
+	 * 
+	 * @return the archiver, or null, if no matching archiver could be found
+	 * @throws ArchiveException if the archiver could not be created
+	 */
+	public static Archive getInstance(String archiverName) 
+		throws ArchiveException {
+		try {
+			if(archiverName == null) {
+				throw new ArchiveException("ArchiverFactory could not create instance");
+			}
+			Iterator it = archives.iterator();
+			while(it.hasNext()) {
+				PackableObject po = (PackableObject)it.next();
+				if(po.isPackableWith(archiverName, PackableObject.CHOOSE_NAME)) {
+					return (Archive)po.getClass().newInstance();
+				}
+			}
+			return null;
+		} catch (InstantiationException e) {
+			throw new ArchiveException("ArchiverFactory could not create instance", e);
+		} catch (IllegalAccessException e) {
+			throw new ArchiveException("ArchiverFactory could not create instance", e);
+		}
+	}
+
+	/**
+	 * Returns an archiver, filled with an existing archive.
+	 * Uses the byte header to identify the archiver. If no corresponding
+	 * archiver could be found, a filename extension check will be done.
+	 * @param archivedFile an existing archive
+	 * @return an archiver, filled with the archive
+	 */
+	public static Archive getInstance(File file) 
+		throws ArchiveException {
+		if(file == null && !file.isFile()) {
+			throw new ArchiveException("ArchiverFactory could not create instance for this file");
+		}
+		
+		
+		/* Archive result */
+		PackableObject packable = null; 
+		
+		try {
+			packable = PackableObject.identifyByHeader(file, archives);
+			
+			if(packable == null) {
+				return null;
+			}
+			Archive archive = (Archive)packable.getClass().newInstance();
+			archive.setArchive(file);
+			return archive;
+		} catch (SecurityException e) {
+			throw new ArchiveException("A security violation occured while reading the field ARCHIVER_NAME",
e);
+		} catch (IllegalArgumentException e) {
+			throw new ArchiveException("Internal factory exception", e);
+		} 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 (PackableObjectException e) {
+			throw new ArchiveException("ArchiverFactory could not create instance", e);
+		} 
+	}
+}

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/ArchiverFactory.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+/**
+ * Exception occurs when a exception within 
+ * the compress actions occurs.
+ */
+public class CompressException extends PackableObjectException {
+	/**
+	 * Comment for <code>serialVersionUID</code>
+	 */
+	private static final long serialVersionUID = 3257005449554507057L;
+
+	/**
+	 * Calls the IOException constructor
+	 */
+	public CompressException() {
+		super();
+	}
+
+	/**
+	 * Calls the IOException constructor with a message
+	 * @param message the message
+	 */
+	public CompressException(String message) {
+		super(message);
+	}
+	
+	/**
+	 * Calls the IOException constructor with a message
+	 * and fills the stacktrace with the stacktrace of 
+	 * an exception
+	 * 
+	 * @param message the message
+	 * @param e the exception 
+	 */
+	public CompressException(String message, Exception e) {
+		super(message);
+		this.initCause(e);
+	}
+}

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressException.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Utils for Compress.
+ */
+public class CompressUtils {
+	/*
+	 * This class should not be constructed
+	 */
+	private CompressUtils() {
+		// unused
+	}
+	
+	/**
+     * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
+     */
+    public static void copy( final InputStream input,
+                              final OutputStream output )
+        throws IOException {
+        final byte[] buffer = new byte[ 8024 ];
+        int n = 0;
+        while( -1 != ( n = input.read( buffer ) ) ) {
+            output.write( buffer, 0, n );
+        }
+    }
+    
+    /**
+     * Compares one byte array to another
+	 * @param source- the array to compare to 
+	 * @param headerBytes - the bytearray match
+	 */
+	public static boolean compareByteArrays(byte[] source, byte[] match) {
+		int i = 0;
+		while(source.length < i || i < match.length ) {
+			if(source[i] != match[i]) {
+				return false;
+			}
+			i++;
+		}
+		return true;
+	}
+}

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressUtils.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+/**
+ * The Compressor Interface defines all operations for 
+ * the compress/decompress actions.
+ */
+public interface Compressor {
+	/**
+	 * Compresses this file and returns an 
+	 * InputStream to the compressed File
+	 * 
+	 * @param input File to compress
+	 * @return FileInputStream of the compressed file
+	 * @throws CompressException if the Compressor reports an error
+	 */
+	public InputStream compress(File input) 
+		throws CompressException;
+	
+	/**
+	 * Compresses this InputStream and returns an 
+	 * InputStream to the compressed file
+	 * 
+	 * @param input Stream to compress
+	 * @return Stream to the compressed file
+	 * @throws CompressException if the Compressor reports an error
+	 */
+	public InputStream compress(FileInputStream input) 
+		throws CompressException;
+
+	/**
+	 * Compresses the file input and creates a file in the same
+	 * directory with the default file extension in its name.
+	 * 
+	 * @param input the file to compress
+	 * @throws CompressException if the Compressor reports an error
+	 */
+	public void compressToHere(File input) 
+		throws CompressException;
+
+	/**
+	 * Creates the file "output" with the compressed
+	 * content of file "input"
+	 * 
+	 * @param input the file to compress
+	 * @param output the file to create
+	 * @throws CompressException if the Compressor reports an error
+	 */
+	public void compressTo(File input, File output) 
+		throws CompressException;
+	
+	/**
+	 * Compresses the input stream and writes the compressed
+	 * bytes to the output stream. This method must be implemented
+	 * by all new compressortypes.
+	 * 
+	 * @param input InputStream to compress to
+	 * @param output OutputStream to which the byte shall be written
+	 * @throws CompressException if the Compressor reports an error
+	 */
+	public void compressTo(FileInputStream input, FileOutputStream output) 
+		throws CompressException;
+	
+	/**
+	 * Decompresses a file and returns an InputStream
+	 * @param input file to decompress
+	 * @return the decompressed file as an inputstream
+	 */
+	public InputStream decompress(File input) 
+		throws CompressException;
+	
+	/**
+	 * Decompresses a file and returns an InputStream
+	 * @param input inputstream to decompress
+	 * @return the decompressed InputStream
+	 */
+	public InputStream decompress(FileInputStream inputStream) 
+		throws CompressException;;
+	
+	/**
+	 * Decompresses this file and writes the decompressed byte to the output file
+	 * @param input File to decompress
+	 * @param output File to write the decompressed bytes to
+	 * @throws DecompressException if the Compressor reports an error
+	 */
+	public void decompressTo(File input, File output) 
+		throws CompressException;
+	
+	/**
+	 * Decompresses this file and writes the decompressed file to the output-stream
+	 * @param input Stream to decompress
+	 * @param output Stream to write the decompressed bytes to
+	 * @throws DecompressException if the Compressor reports an error
+	 */
+	public void decompressTo(FileInputStream input, FileOutputStream output) 
+		throws CompressException;
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/Compressor.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.compress.compressors.bzip2.BZip2Compressor;
+/**
+ * Compressor-Factory. 
+ * Use CompressorFactory.TYPE.getInstance() for an new Compressor-Instance.
+ */
+public abstract class CompressorFactory {
+	/* Name of this CompressorFactory*/
+	private final String name;
+	/* internal archiver list */
+	private static List compressors;
+	
+	// register compressors 
+	static {
+		compressors = new ArrayList();
+		try {
+			registerCompressor(BZip2Compressor.class);
+		} catch (CompressException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Registers a new archiver in the factory.
+	 * The archiver must implement the archiver interface.
+	 * 
+	 * @param className full qualified archiver implementation
+	 * @throws ClassNotFoundException if the new archiver class could not be found
+	 * @throws ArchiveException if the new archiver does not implement the archiver interface
+	 */
+	public static void registerCompressor(String className) 
+		throws ClassNotFoundException, CompressException {
+		Class clazz = Class.forName(className);
+		registerCompressor(clazz);
+	}
+	
+	/**
+	 * Registers a new archiver in the factory.
+	 * The archiver must implement the archiver interface and must
+	 * be an concrete implementation
+	 * 
+	 * @param className full qualified archiver implementation
+	 * @throws ArchiveException if the new archiver does not implement the archiver interface
+	 */
+	public static void registerCompressor(Class clazz) 
+		throws CompressException {
+		// register only, if the class is assignable and not an interface
+		if(Compressor.class.isAssignableFrom(clazz) && !(clazz.isInterface())) {
+			try {
+				compressors.add(clazz.newInstance());
+			} catch (InstantiationException e) {
+				throw new CompressException("Compressor could not be instantiated", e);
+			} catch (IllegalAccessException e) {
+				throw new CompressException("Compressor could not be instantiated", e);
+			}
+		} else {
+			throw new CompressException("Compressor does not implement the Compressor.class interface.");
+		}
+	}
+
+	/**
+	 * Constructor. Takes the name of the implementation.
+	 * @param _name - name of the implementation
+	 */
+	private CompressorFactory(String name) { 
+		this.name = name; 
+	}
+
+	/**
+	 * Returns a compressor
+	 * @return the compressor
+	 */
+	public abstract Compressor getInstance();
+
+	/**
+	 * Returns an empty Compressor, if an matching compressor 
+	 * could be found within this factory.
+	 * If two implementations with the same name are registered,
+	 * the first matching implementation will be instanciated.
+	 * 
+	 * @return the compressor, or null, if no matching compressor could be found
+	 * @throws CompressorException if the compressor could not be created
+	 */
+	public static Compressor getInstance(String compressorName) 
+		throws CompressException {
+		try {
+			if(compressorName == null) {
+				throw new CompressException("CompressorFactory could not create instance");
+			}
+			Iterator it = compressors.iterator();
+			while(it.hasNext()) {
+				PackableObject po = (PackableObject)it.next();
+				if(po.isPackableWith(compressorName, PackableObject.CHOOSE_NAME)) {
+					return (Compressor)po.getClass().newInstance();
+				}
+			}
+			return null;
+		} catch (InstantiationException e) {
+			throw new CompressException("CompressorFactory could not create instance", e);
+		} catch (IllegalAccessException e) {
+			throw new CompressException("CompressorFactory could not create instance", e);
+		}
+	}
+	
+	/**
+	 * Returns an archiver, filled with an existing archive.
+	 * Uses the byte header to identify the archiver. If no corresponding
+	 * archiver could be found, a filename extension check will be done.
+	 * @param archivedFile an existing archive
+	 * @return an archiver, filled with the archive
+	 */
+	public static Compressor getInstance(File file) 
+		throws CompressException {
+		if(file == null && !file.isFile()) {
+			throw new CompressException("CompressorFactory could not create instance for this file");
+		}
+		
+		
+		/* Archive result */
+		PackableObject packable = null; 
+		
+		try {
+			packable = PackableObject.identifyByHeader(file, compressors);
+			
+			if(packable == null) {
+				return null;
+			}
+			Compressor compressor = (Compressor)packable.getClass().newInstance();
+			return compressor;
+		} catch (SecurityException e) {
+			throw new CompressException("A security violation occured while reading the field ARCHIVER_NAME",
e);
+		} catch (IllegalArgumentException e) {
+			throw new CompressException("Internal factory exception", e);
+		} catch (InstantiationException e) {
+			throw new CompressException("CompressorFactory could not create instance", e);
+		} catch (IllegalAccessException e) {
+			throw new CompressException("CompressorFactory could not create instance", e);
+		} catch (PackableObjectException e) {
+			throw new CompressException("CompressorFactory could not create instance", e);
+		} 
+	}
+	/**
+	 * <code>BZIP2</code> Compressor Factory
+	 */
+	public static CompressorFactory BZIP2 = new CompressorFactory("BZIP2") {
+		/* (non-Javadoc)
+		 * @see org.apache.commons.compress.ArchiverFactory#getInstance()
+		 */
+		public Compressor getInstance() { 
+			return new BZip2Compressor(); 
+		}
+	};
+	
+	/* (non-Javadoc)
+	 * @see java.lang.Object#toString()
+	 */
+	public String toString() { 
+		return name; 
+	}
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/CompressorFactory.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java?rev=427890&view=auto
==============================================================================
--- jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
(added)
+++ jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
Tue Aug  1 21:45:26 2006
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+/**
+ * Abstract super object for Compressor and Archiver classes.
+ */
+public abstract class PackableObject {
+	/* Type for archive choosing: String */
+	protected static final int CHOOSE_EXTENSION = 1;
+	/* Type for archive choosing: Long */
+	protected static final int CHOOSE_NAME = 2;
+
+	/** 
+	 * Header byte array for this archive. 
+	 */
+	public abstract byte[] getHeader();
+
+	/** 
+	 * Returns the default FileExtension for this archive,
+	 * for example "zip", "tar"... 
+	 */
+	public abstract String getDefaultFileExtension();
+	
+	/** 
+	 * Returns the ArchiveName for this archive.  
+	 */
+	public abstract String getName();
+
+	/**
+	 * String Chooser.
+	 * @param value
+	 * @param methodName
+	 * @return
+	 * @throws ArchiveException
+	 * @throws FileNotFoundException 
+	 */
+	protected boolean isPackableWith(Object value, int choose) {
+		if(value == null) {
+			return false;
+		}
+		if(choose == CHOOSE_EXTENSION) {
+			if(value.equals( getDefaultFileExtension())) {
+				return true;
+			}
+		} else if (choose == CHOOSE_NAME) {
+			if(value.equals( getName())) {
+				return true;
+			}
+		}
+		return false;
+	}
+	
+	/**
+	 * Compares a file to a list of packables and identifies an object by header.
+	 * If no matching header is found, it identifies the file by file extension. 
+	 * If identification was not successfull, null is returned
+	 * 
+	 * @param file the file to identify
+	 * @param packables a list of packables
+	 * @return a matching packable object, or null
+	 * @throws ArchiveException
+	 */
+	public static PackableObject identifyByHeader(File file, List packables) 
+		throws PackableObjectException {
+		/* FileInputStream for the archive */
+		FileInputStream fis = null;
+		
+		try {
+			/* Archive result */
+			PackableObject packable = null; 
+			
+	//		 identify archive by header
+			fis = new FileInputStream(file);
+			byte[] headerBytes = new byte[20];
+			fis.read(headerBytes, 0, 20);
+			
+			Iterator iter = packables.iterator();
+			while(iter.hasNext()) {
+				PackableObject p = (PackableObject)iter.next();
+				byte[] fieldHeader = p.getHeader();
+				if(fieldHeader != null) {
+					if(CompressUtils.compareByteArrays(headerBytes, fieldHeader)) {
+						return p;
+					}
+				}
+			}
+			
+			// if we couldn't find an archiver by header bytes, we'll give it a try
+			// with the default name extension. This is useful, cause some archives 
+			// like tar have no header.
+			String name = file.getName();
+			String extension = null;
+			String[] s = name.split("\\.");
+			if(s.length > 1) {
+				extension = s[ s.length - 1];
+			}
+			Iterator it = packables.iterator();
+			while(it.hasNext()) {
+				PackableObject p = (PackableObject)it.next();
+				if(p.isPackableWith(extension, PackableObject.CHOOSE_EXTENSION)) {
+					return p;
+				}
+			}
+			
+			// No implementation found
+			return null;
+		} catch (FileNotFoundException e) {
+			throw new PackableObjectException("File not found", e);
+		} catch (IOException e) {
+			throw new PackableObjectException("Internal factory exception", e);
+		} finally {
+			try {
+				fis.close();
+			} catch (IOException e1) {
+				throw new PackableObjectException("Error while closing InputStream to file", e1);
+			}
+		}
+	}
+}

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/sandbox/compress/trunk/src/java/org/apache/commons/compress/PackableObject.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message