You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2014/01/19 08:03:40 UTC

svn commit: r1559479 - in /commons/proper/compress/branches/compress-2.0/src: main/java/org/apache/commons/compress2/formats/ar/ test/java/org/apache/commons/compress2/formats/ar/ test/resources/test-archives/

Author: bodewig
Date: Sun Jan 19 07:03:40 2014
New Revision: 1559479

URL: http://svn.apache.org/r1559479
Log:
implement ArchiveFormat for AR

Added:
    commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveFormat.java   (with props)
    commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java   (with props)
    commons/proper/compress/branches/compress-2.0/src/test/resources/test-archives/
    commons/proper/compress/branches/compress-2.0/src/test/resources/test-archives/default.ar
      - copied unchanged from r1559078, commons/proper/compress/trunk/src/test/resources/bla.ar
Modified:
    commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveInput.java

Added: commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveFormat.java?rev=1559479&view=auto
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveFormat.java (added)
+++ commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveFormat.java Sun Jan 19 07:03:40 2014
@@ -0,0 +1,106 @@
+/*
+ * 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.commons.compress2.formats.ar;
+
+import java.nio.ByteBuffer;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import org.apache.commons.compress2.archivers.spi.AbstractArchiveFormat;
+
+/**
+ * Format descriptor for the AR format.
+ */
+public class ArArchiveFormat extends AbstractArchiveFormat<ArArchiveEntry> {
+
+    private static final byte[] SIG = StandardCharsets.US_ASCII.encode(ArArchiveEntry.HEADER).array();
+
+    /**
+     * "AR"
+     */
+    public static final String AR_FORMAT_NAME = "AR";
+
+    /**
+     * @return {@link #AR_FORMAT_NAME}
+     */
+    @Override
+    public String getName() {
+        return AR_FORMAT_NAME;
+    }
+
+    /**
+     * Yes.
+     */
+    @Override
+    public boolean supportsWriting() { return true; }
+    /**
+     * Yes.
+     */
+    @Override
+    public boolean supportsWritingToChannels() { return true; }
+    /**
+     * Yes.
+     */
+    @Override
+    public boolean supportsReadingFromChannels() { return true; }
+
+    /**
+     * Yes.
+     */
+    @Override
+    public boolean supportsAutoDetection() { return true; }
+
+    /**
+     * Each AR archive starts with "!&lt;arch&gt;" followed by a LF.
+     * @return 8
+     */
+    @Override
+    public int getNumberOfBytesRequiredForAutodetection() throws UnsupportedOperationException {
+        return SIG.length;
+    }
+
+    /**
+     * Each AR archive starts with "!&lt;arch&gt;" followed by a LF.
+     */
+    @Override
+    public boolean matches(ByteBuffer probe) throws UnsupportedOperationException {
+        byte[] sig = new byte[SIG.length];
+        probe.get(sig);
+        return Arrays.equals(SIG, sig);
+    }
+
+    /**
+     * This implementation ignores the charset as AR archives only support US-ASCII file names.
+     */
+    @Override
+    public ArArchiveInput readFrom(ReadableByteChannel channel, Charset charset) {
+        return new ArArchiveInput(channel);
+    }
+
+    /**
+     * This implementation ignores the charset as AR archives only support US-ASCII file names.
+     */
+    @Override
+    public ArArchiveOutput writeTo(WritableByteChannel channel, Charset charset) {
+        return new ArArchiveOutput(channel);
+    }
+
+}

Propchange: commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveInput.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveInput.java?rev=1559479&r1=1559478&r2=1559479&view=diff
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveInput.java (original)
+++ commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveInput.java Sun Jan 19 07:03:40 2014
@@ -271,50 +271,6 @@ public class ArArchiveInput extends Abst
         }
     }
 
-    /**
-     * Checks if the signature matches ASCII "!&lt;arch&gt;" followed by a single LF
-     * control character
-     * 
-     * @param signature
-     *            the bytes to check
-     * @param length
-     *            the number of bytes to check
-     * @return true, if this stream is an Ar archive stream, false otherwise
-     */
-    public static boolean matches(byte[] signature, int length) {
-        // 3c21 7261 6863 0a3e
-
-        if (length < 8) {
-            return false;
-        }
-        if (signature[0] != 0x21) {
-            return false;
-        }
-        if (signature[1] != 0x3c) {
-            return false;
-        }
-        if (signature[2] != 0x61) {
-            return false;
-        }
-        if (signature[3] != 0x72) {
-            return false;
-        }
-        if (signature[4] != 0x63) {
-            return false;
-        }
-        if (signature[5] != 0x68) {
-            return false;
-        }
-        if (signature[6] != 0x3e) {
-            return false;
-        }
-        if (signature[7] != 0x0a) {
-            return false;
-        }
-
-        return true;
-    }
-
     static final String BSD_LONGNAME_PREFIX = "#1/";
     private static final int BSD_LONGNAME_PREFIX_LEN =
         BSD_LONGNAME_PREFIX.length();

Added: commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java?rev=1559479&view=auto
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java (added)
+++ commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java Sun Jan 19 07:03:40 2014
@@ -0,0 +1,53 @@
+/*
+ * 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.commons.compress2.formats.ar;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ArArchiveFormatTest {
+
+    @Test
+    public void shouldDetectFormat() throws IOException {
+        Assert.assertTrue(isAr("test-archives/default.ar"));
+    }
+
+    @Test
+    public void shouldRejectXMLFile() throws IOException {
+        Assert.assertFalse(isAr("test1.xml"));
+    }
+
+
+    private boolean isAr(String file) throws IOException {
+        File f = RoundTripTest.getFile(file);
+        FileInputStream c = new FileInputStream(f);
+        try {
+            byte[] b = new byte[10];
+            IOUtils.readFully(c, b);
+            return new ArArchiveFormat().matches(ByteBuffer.wrap(b));
+        } finally {
+            c.close();
+        }
+    }
+}

Propchange: commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
------------------------------------------------------------------------------
    svn:eol-style = native