commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r761474 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/utils/ArchiveUtils.java test/java/org/apache/commons/compress/ArchiveUtilsTest.java
Date Thu, 02 Apr 2009 23:33:43 GMT
Author: sebb
Date: Thu Apr  2 23:33:43 2009
New Revision: 761474

URL: http://svn.apache.org/viewvc?rev=761474&view=rev
Log:
Add some utility methods and tests

Added:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
  (with props)
Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java?rev=761474&r1=761473&r2=761474&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
Thu Apr  2 23:33:43 2009
@@ -18,6 +18,8 @@
 
 package org.apache.commons.compress.utils;
 
+import java.io.UnsupportedEncodingException;
+
 import org.apache.commons.compress.archivers.ArchiveEntry;
 
 /**
@@ -47,4 +49,143 @@
         sb.append(' ').append(entry.getName());
         return sb.toString();
     }
+
+    /**
+     * Check if buffer contents matches Ascii String.
+     * 
+     * @param expected
+     * @param buffer
+     * @param offset
+     * @param length
+     * @return <code>true</code> if buffer is the same as the expected string
+     */
+    public static boolean matchAsciiBuffer(
+            String expected, byte[] buffer, int offset, int length){
+        byte[] buffer1;
+        try {
+            buffer1 = expected.getBytes("ASCII");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e); // Should not happen
+        }
+        return isEqual(buffer1, 0, buffer1.length, buffer, offset, length, false);
+    }
+    
+    /**
+     * Check if buffer contents matches Ascii String.
+     * 
+     * @param expected
+     * @param buffer
+     * @return <code>true</code> if buffer is the same as the expected string
+     */
+    public static boolean matchAsciiBuffer(String expected, byte[] buffer){
+        byte[] buffer1;
+        try {
+            buffer1 = expected.getBytes("ASCII");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e); // Should not happen
+        }
+        return isEqual(buffer1, 0, buffer1.length, buffer, 0, buffer.length, false);
+    }
+    
+    /**
+     * Compare byte buffers, optionally ignoring trailing nulls
+     * 
+     * @param buffer1
+     * @param offset1
+     * @param length1
+     * @param buffer2
+     * @param offset2
+     * @param length2
+     * @param ignoreTrailingNulls
+     * @return <code>true</code> if buffer1 and buffer2 have same contents, having
regard to trailing nulls
+     */
+    public static boolean isEqual(
+            final byte[] buffer1, final int offset1, final int length1,
+            final byte[] buffer2, final int offset2, final int length2,
+            boolean ignoreTrailingNulls){
+        int minLen=length1 < length2 ? length1 : length2;
+        for (int i=0; i < minLen; i++){
+            if (buffer1[offset1+i] != buffer2[offset2+i]){
+                return false;
+            }
+        }
+        if (length1 == length2){
+            return true;
+        }
+        if (ignoreTrailingNulls){
+            if (length1 > length2){
+                for(int i = length2; i < length1; i++){
+                    if (buffer1[offset1+i] != 0){
+                        return false;
+                    }
+                }
+            } else {
+                for(int i = length1; i < length2; i++){
+                    if (buffer2[offset2+i] != 0){
+                        return false;
+                    }
+                }                
+            }
+            return true;
+        }
+        return false;
+    }
+    
+    /**
+     * Compare byte buffers
+     * 
+     * @param buffer1
+     * @param offset1
+     * @param length1
+     * @param buffer2
+     * @param offset2
+     * @param length2
+     * @return <code>true</code> if buffer1 and buffer2 have same contents
+     */
+    public static boolean isEqual(
+            final byte[] buffer1, final int offset1, final int length1,
+            final byte[] buffer2, final int offset2, final int length2){
+        return isEqual(buffer1, offset1, length1, buffer2, offset2, length2, false);
+    }
+    
+    /**
+     * Compare byte buffers
+     * 
+     * @param buffer1
+     * @param buffer2
+     * @return <code>true</code> if buffer1 and buffer2 have same contents
+     */
+    public static boolean isEqual(final byte[] buffer1, final byte[] buffer2 ){
+        return isEqual(buffer1, 0, buffer1.length, buffer2, 0, buffer2.length, false);
+    }
+    
+    /**
+     * Compare byte buffers, optionally ignoring trailing nulls
+     * 
+     * @param buffer1
+     * @param buffer2
+     * @param ignoreTrailingNulls
+     * @return <code>true</code> if buffer1 and buffer2 have same contents
+     */
+    public static boolean isEqual(final byte[] buffer1, final byte[] buffer2, boolean ignoreTrailingNulls){
+        return isEqual(buffer1, 0, buffer1.length, buffer2, 0, buffer2.length, ignoreTrailingNulls);
+    }
+    
+    /**
+     * Compare byte buffers, ignoring trailing nulls
+     * 
+     * @param buffer1
+     * @param offset1
+     * @param length1
+     * @param buffer2
+     * @param offset2
+     * @param length2
+     * @return <code>true</code> if buffer1 and buffer2 have same contents, having
regard to trailing nulls
+     */
+    public static boolean isEqualWithNull(
+            final byte[] buffer1, final int offset1, final int length1,
+            final byte[] buffer2, final int offset2, final int length2){
+        return isEqual(buffer1, offset1, length1, buffer2, offset2, length2, true);
+    }
+    
 }

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java?rev=761474&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
(added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
Thu Apr  2 23:33:43 2009
@@ -0,0 +1,74 @@
+/*
+ * 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.compress;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
+import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
+import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.utils.ArchiveUtils;
+
+/**
+ * Check that the different write methods create the same output.
+ * TODO perform the same checks for reads.
+ */
+public class ArchiveUtilsTest extends AbstractTestCase {
+
+    private static final int bytesToTest = 50;
+    private static final byte[] byteTest = new byte[bytesToTest];
+    static {
+        for(int i=0; i < byteTest.length ;) {
+            byteTest[i]=(byte) i;
+            byteTest[i+1]=(byte) -i;
+            i += 2;
+        }
+    }
+    public void testCompareBA(){
+        byte[] buffer1 = {1,2,3};
+        byte[] buffer2 = {1,2,3,0};
+        byte[] buffer3 = {1,2,3};
+        assertTrue(ArchiveUtils.isEqual(buffer1, buffer2, true));
+        assertFalse(ArchiveUtils.isEqual(buffer1, buffer2, false));
+        assertFalse(ArchiveUtils.isEqual(buffer1, buffer2));
+        assertTrue(ArchiveUtils.isEqual(buffer2, buffer1, true));
+        assertFalse(ArchiveUtils.isEqual(buffer2, buffer1, false));
+        assertFalse(ArchiveUtils.isEqual(buffer2, buffer1));
+        assertTrue(ArchiveUtils.isEqual(buffer1, buffer3));
+        assertTrue(ArchiveUtils.isEqual(buffer3, buffer1));
+    }
+    
+    public void testCompareAscii(){
+        byte[] buffer1 = {'a','b','c'};
+        byte[] buffer2 = {'d','e','f',0};
+        assertTrue(ArchiveUtils.matchAsciiBuffer("abc", buffer1));
+        assertFalse(ArchiveUtils.matchAsciiBuffer("abc\0", buffer1));
+        assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2));        
+        assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2));
+    }
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message