read-only support for snappy compression

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/compress/trunk@1553458 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/changes.xml b/changes.xml
index b0c6130..b49d450 100644
--- a/changes.xml
+++ b/changes.xml
@@ -39,9 +39,12 @@
 
     <release version="1.4" date="unreleased">
       <action type="add">
-        Read-only support for tradional Unix Compress (.Z) has been
+        Read-only support for traditional Unix Compress (.Z) has been
         added.
       </action>
+      <action type="add">
+        Read-only support for Snappy compression has been added.
+      </action>
     </release>
 
     <release version="1.3" date="2013-11-07">
diff --git a/docs/compresource.html b/docs/compresource.html
index 5e56869..0c2ff9c 100644
--- a/docs/compresource.html
+++ b/docs/compresource.html
@@ -106,7 +106,7 @@
 </pre>
 
 <p>Represents the entry named some-file.txt in archive
-  some-archive.tar.gz where the <code>lzmaresource</code> provides
+  some-archive.tar.lzma where the <code>lzmaresource</code> provides
   the decompression of the archive.</p>
 
 <h2><a name="pack200resource">pack200resource</a></h2>
@@ -130,6 +130,29 @@
   JAR some-archive.pack where the <code>pack200resource</code>
   provides the decompression of the archive.</p>
 
+<h2><a name="snappyresource">snappyresource</a></h2>
+
+<p><em>Since Apache Compress Antlib 1.4</em>.</p>
+
+<p>This is a <a href="#compresource">compressed resource</a> using the
+  <a href="http://code.google.com/p/snappy/">Snappy</a>
+  compression.</p>
+
+<h4>Examples</h4>
+
+<pre>
+&lt;cmp:tarentry xmlns:cmp="antlib:org.apache.ant.compress"
+    name="some-file.txt"&gt;
+  &lt;cmp:snappyresource&gt;
+    &lt;file file="some-archive.tar.sz"/&gt;
+  &lt;/cmp:snappyresource&gt;
+&lt;/cmp:tarentry&gt;
+</pre>
+
+<p>Represents the entry named some-file.txt in archive
+  some-archive.tar.sz where the <code>snappyresource</code> provides
+  the decompression of the archive.</p>
+
 <h2><a name="xzresource">xzresource</a></h2>
 
 <p>This is a <a href="#compresource">compressed resource</a> using the
@@ -173,5 +196,5 @@
 </pre>
 
 <p>Represents the entry named some-file.txt in archive
-  some-archive.tar.gz where the <code>zresource</code> provides
+  some-archive.tar.Z where the <code>zresource</code> provides
   the decompression of the archive.</p>
diff --git a/docs/index.html b/docs/index.html
index f1fbf26..f32107f 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -29,10 +29,10 @@
       compression/uncompression and archival/unarchival capabilities
       based on <a href="http://commons.apache.org/compress/">Apache
       Commons Compress</a>.  Using Apache Commons Compress 1.6 this
-      Antlib supports gzip, bzip2, lzma standalone, xz, .Z and pack200
-      compression and ar, arj, cpio, 7z, Unix dump, tar and zip
-      archives.  Support for lzma, arj, .Z and dump is read-only.  7z can
-      only be used on filesystem resources.</p>
+      Antlib supports gzip, bzip2, lzma standalone, xz, .Z, Snappy and
+      pack200 compression and ar, arj, cpio, 7z, Unix dump, tar and
+      zip archives.  Support for lzma, arj, .Z, Snappy and dump is
+      read-only.  7z can only be used on filesystem resources.</p>
 
     <h3>Known Limitations</h3>
 
@@ -62,6 +62,7 @@
       <li><a href="expand.html#undump">undump</a></li>
       <li><a href="unpack.html#unlzma">unlzma</a></li>
       <li><a href="unpack.html#unpack200">unpack200</a></li>
+      <li><a href="unpack.html#unsnappy">unsnappy</a></li>
       <li><a href="expand.html#untar">untar</a></li>
       <li><a href="unpack.html#unxz">unxz</a></li>
       <li><a href="unpack.html#unz">unz</a></li>
@@ -86,6 +87,7 @@
       <li><a href="compresource.html#gzipresource">gzipresource</a></li>
       <li><a href="compresource.html#lzmaresource">lzmaresource</a></li>
       <li><a href="compresource.html#pack200resource">pack200resource</a></li>
+      <li><a href="compresource.html#snappyresource">snappyresource</a></li>
       <li><a href="entry.html#tarentry">tarentry</a></li>
       <li><a href="fileset.html#sevenzfileset">sevenzfileset</a></li>
       <li><a href="entry.html#sevenzentry">sevenzentry</a></li>
diff --git a/docs/unpack.html b/docs/unpack.html
index 63397bc..c03f1fb 100644
--- a/docs/unpack.html
+++ b/docs/unpack.html
@@ -154,6 +154,12 @@
     </tr>
   </table>
 
+<h3><a name="unsnappy">UnSnappy</a></h3>
+
+<p>Is an <a href="#unpack">uncompressing task</a> that uses
+  the <a href="http://code.google.com/p/snappy/">Snappy</a>
+  compression algorithm.</p>
+
 <h3><a name="unxz">UnXZ</a></h3>
 
 <p>Is an <a href="#unpack">uncompressing task</a> that uses the XZ
diff --git a/src/main/org/apache/ant/compress/antlib.xml b/src/main/org/apache/ant/compress/antlib.xml
index 1075e23..b2e03d4 100644
--- a/src/main/org/apache/ant/compress/antlib.xml
+++ b/src/main/org/apache/ant/compress/antlib.xml
@@ -77,6 +77,10 @@
      classname="org.apache.ant.compress.taskdefs.UnPack200"
      />
   <taskdef
+     name="unsnappy"
+     classname="org.apache.ant.compress.taskdefs.UnSnappy"
+     />
+  <taskdef
      name="unxz"
      classname="org.apache.ant.compress.taskdefs.UnXZ"
      />
@@ -187,6 +191,10 @@
      classname="org.apache.ant.compress.resources.Pack200Resource"
      />
   <typedef
+     name="snappyresource"
+     classname="org.apache.ant.compress.resources.SnappyResource"
+     />
+  <typedef
      name="xzresource"
      classname="org.apache.ant.compress.resources.XZResource"
      />
diff --git a/src/main/org/apache/ant/compress/resources/SnappyResource.java b/src/main/org/apache/ant/compress/resources/SnappyResource.java
new file mode 100644
index 0000000..4b51d9f
--- /dev/null
+++ b/src/main/org/apache/ant/compress/resources/SnappyResource.java
@@ -0,0 +1,37 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.ant.compress.resources;
+
+import org.apache.ant.compress.util.SnappyStreamFactory;
+import org.apache.tools.ant.types.ResourceCollection;
+
+/**
+ * An Snappy compressed resource.
+ * @since Apache Compress Antlib 1.4
+ */
+public final class SnappyResource extends CommonsCompressCompressorResource {
+    private static final String NAME = "Snappy";
+
+    public SnappyResource() {
+        super(NAME, new SnappyStreamFactory());
+    }
+
+    public SnappyResource(ResourceCollection other) {
+        super(NAME, new SnappyStreamFactory(), other);
+    }
+}
diff --git a/src/main/org/apache/ant/compress/taskdefs/UnSnappy.java b/src/main/org/apache/ant/compress/taskdefs/UnSnappy.java
new file mode 100644
index 0000000..792d41a
--- /dev/null
+++ b/src/main/org/apache/ant/compress/taskdefs/UnSnappy.java
@@ -0,0 +1,33 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.ant.compress.taskdefs;
+
+import org.apache.ant.compress.util.SnappyStreamFactory;
+
+/**
+ * Expands an snappy archive.
+ * @since Apache Compress Antlib 1.4
+ */
+public final class UnSnappy extends UnpackBase {
+
+    public UnSnappy() {
+        super(".sz", new SnappyStreamFactory());
+    }
+
+}
diff --git a/src/main/org/apache/ant/compress/util/SnappyStreamFactory.java b/src/main/org/apache/ant/compress/util/SnappyStreamFactory.java
new file mode 100644
index 0000000..1ee5636
--- /dev/null
+++ b/src/main/org/apache/ant/compress/util/SnappyStreamFactory.java
@@ -0,0 +1,51 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.ant.compress.util;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.compress.compressors.CompressorInputStream;
+import org.apache.commons.compress.compressors.CompressorOutputStream;
+import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream;
+
+/**
+ * Creates streams for the standalone Snappy format.
+ * @since Apache Compress Antlib 1.4
+ * @see <a href="http://code.google.com/p/snappy/">Snappy Project</a>
+ */
+public class SnappyStreamFactory implements CompressorStreamFactory {
+
+    /**
+     * @param stream the stream to read from, should be buffered
+     */
+    public CompressorInputStream getCompressorStream(InputStream stream)
+        throws IOException {
+        return new FramedSnappyCompressorInputStream(stream);
+    }
+
+    /**
+     * Not implemented.
+     */
+    public CompressorOutputStream getCompressorStream(OutputStream stream)
+        throws IOException {
+        throw new UnsupportedOperationException();
+    }
+}
diff --git a/src/tests/antunit/snappyresource-test.xml b/src/tests/antunit/snappyresource-test.xml
new file mode 100644
index 0000000..7818428
--- /dev/null
+++ b/src/tests/antunit/snappyresource-test.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+
+<project default="antunit"
+         xmlns:au="antlib:org.apache.ant.antunit"
+         xmlns:cond="antlib:org.apache.tools.ant.types.conditions"
+         xmlns:cmp="antlib:org.apache.ant.compress">
+
+  <import file="antunit-base.xml" />
+
+  <target name="setUp">
+    <mkdir dir="${output}"/>
+  </target>    
+
+  <target name="testNativeSnappy" depends="setUp">
+    <copy todir="${output}">
+      <cmp:snappyresource>
+        <file file="../resources/asf-logo.gif.sz"/>
+      </cmp:snappyresource>
+      <globmapper from="*.sz" to="*"/>
+    </copy>
+    <au:assertFilesMatch expected="../resources/asf-logo.gif"
+                         actual="${output}/asf-logo.gif"/>
+  </target>
+
+</project>
diff --git a/src/tests/antunit/unsnappy-test.xml b/src/tests/antunit/unsnappy-test.xml
new file mode 100644
index 0000000..252b787
--- /dev/null
+++ b/src/tests/antunit/unsnappy-test.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+
+<project default="antunit"
+         xmlns:au="antlib:org.apache.ant.antunit"
+         xmlns:cond="antlib:org.apache.tools.ant.types.conditions"
+         xmlns:cmp="antlib:org.apache.ant.compress">
+
+  <import file="antunit-base.xml" />
+
+  <target name="setUp">
+    <mkdir dir="${output}"/>
+  </target>    
+
+  <target name="test1">
+    <au:expectfailure>
+      <cmp:unsnappy/>
+    </au:expectfailure>
+  </target>
+
+  <target name="test2">
+    <au:expectfailure>
+      <cmp:unsnappy src=""/>
+    </au:expectfailure>
+  </target>
+
+  <target name="testNativeSnappy" depends="setUp">
+    <cmp:unsnappy src="../resources/asf-logo.gif.sz"
+                dest="${output}/asf-logo.gif" />
+    <au:assertFilesMatch expected="../resources/asf-logo.gif"
+                         actual="${output}/asf-logo.gif"/>
+  </target>
+
+  <target name="testWithResource" depends="setUp">
+    <cmp:unsnappy dest="${output}/asf-logo.gif">
+      <file file="../resources/asf-logo.gif.sz"/>
+    </cmp:unsnappy>
+    <au:assertFilesMatch expected="../resources/asf-logo.gif"
+                         actual="${output}/asf-logo.gif"/>
+  </target>
+
+
+</project>
diff --git a/src/tests/resources/asf-logo.gif.sz b/src/tests/resources/asf-logo.gif.sz
new file mode 100644
index 0000000..341d458
--- /dev/null
+++ b/src/tests/resources/asf-logo.gif.sz
Binary files differ