You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2009/04/14 17:17:44 UTC

svn commit: r764825 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/utils/ArchiveUtils.java test/java/org/apache/commons/compress/ArchiveUtilsTest.java

Author: sebb
Date: Tue Apr 14 15:17:42 2009
New Revision: 764825

URL: http://svn.apache.org/viewvc?rev=764825&view=rev
Log:
Additional String <=> Byte array Ascii conversion methods + basic tests

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.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=764825&r1=764824&r2=764825&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 Tue Apr 14 15:17:42 2009
@@ -78,15 +78,38 @@
      * @return <code>true</code> if buffer is the same as the expected string
      */
     public static boolean matchAsciiBuffer(String expected, byte[] buffer){
-        byte[] buffer1;
+        return matchAsciiBuffer(expected, buffer, 0, buffer.length);
+    }
+    
+    /**
+     * Convert a string to Ascii bytes.
+     * Used for comparing "magic" strings which need to be independent of the default Locale.
+     * 
+     * @param inputString
+     * @return the bytes
+     */
+    public static byte[] toAsciiBytes(String inputString){
         try {
-            buffer1 = expected.getBytes("ASCII");
+            return inputString.getBytes("ASCII");
         } catch (UnsupportedEncodingException e) {
-            throw new RuntimeException(e); // Should not happen
+           throw new RuntimeException(e); // Should never happen
         }
-        return isEqual(buffer1, 0, buffer1.length, buffer, 0, buffer.length, false);
     }
-    
+
+    /**
+     * Convert an input byte array to a String using the ASCII character set.
+     * 
+     * @param inputBytes
+     * @return the bytes, interpreted as an Ascii string
+     */
+    public static String toAsciiString(final byte[] inputBytes){
+        try {
+            return new String(inputBytes, "ASCII");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e); // Should never happen
+        }
+    }
+
     /**
      * Compare byte buffers, optionally ignoring trailing nulls
      * 

Modified: 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=764825&r1=764824&r2=764825&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java Tue Apr 14 15:17:42 2009
@@ -20,10 +20,6 @@
 
 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;
@@ -57,4 +53,18 @@
         assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2));        
         assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2));
     }
+    
+    public void testAsciiConversions() {
+        asciiToByteAndBackOK("");
+        asciiToByteAndBackOK("abcd");
+        asciiToByteAndBackFail("\u8025");
+    }
+
+    private void asciiToByteAndBackOK(String inputString) {
+        assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
+    }
+
+    private void asciiToByteAndBackFail(String inputString) {
+        assertFalse(inputString.equals(ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString))));
+    }
 }