You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by en...@apache.org on 2013/03/14 03:26:18 UTC

svn commit: r1456306 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/util/ test/java/org/apache/hadoop/hbase/io/hfile/ test/java/org/apache/hadoop/hbase/util/

Author: enis
Date: Thu Mar 14 02:26:17 2013
New Revision: 1456306

URL: http://svn.apache.org/r1456306
Log:
HBASE-8085 Backport the fix for Bytes.toStringBinary() into 94 (HBASE-6991) (Tianying Chang)

Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Bytes.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Bytes.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Bytes.java?rev=1456306&r1=1456305&r2=1456306&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Bytes.java (original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Bytes.java Thu Mar 14 02:26:17 2013
@@ -339,21 +339,16 @@ public class Bytes {
    */
   public static String toStringBinary(final byte [] b, int off, int len) {
     StringBuilder result = new StringBuilder();
-    try {
-      String first = new String(b, off, len, "ISO-8859-1");
-      for (int i = 0; i < first.length() ; ++i ) {
-        int ch = first.charAt(i) & 0xFF;
-        if ( (ch >= '0' && ch <= '9')
-            || (ch >= 'A' && ch <= 'Z')
-            || (ch >= 'a' && ch <= 'z')
-            || " `~!@#$%^&*()-_=+[]{}\\|;:'\",.<>/?".indexOf(ch) >= 0 ) {
-          result.append(first.charAt(i));
-        } else {
-          result.append(String.format("\\x%02X", ch));
-        }
+    for (int i = off; i < off + len ; ++i ) { 
+      int ch = b[i] & 0xFF;
+      if ( (ch >= '0' && ch <= '9')      
+          || (ch >= 'A' && ch <= 'Z')
+          || (ch >= 'a' && ch <= 'z')
+          || " `~!@#$%^&*()-_=+[]{}|;:'\",.<>/?".indexOf(ch) >= 0 ) {
+          result.append((char)ch);
+      } else {
+        result.append(String.format("\\x%02X", ch));
       }
-    } catch (UnsupportedEncodingException e) {
-      LOG.error("ISO-8859-1 not supported?", e);
     }
     return result.toString();
   }

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java?rev=1456306&r1=1456305&r2=1456306&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java (original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java Thu Mar 14 02:26:17 2013
@@ -247,7 +247,7 @@ public class TestHFileBlock {
             + "\\x03"
             + "\\xED\\xC3\\xC1\\x11\\x00 \\x08\\xC00DD\\xDD\\x7Fa"
             + "\\xD6\\xE8\\xA3\\xB9K\\x84`\\x96Q\\xD3\\xA8\\xDB\\xA8e\\xD4c"
-            + "\\xD46\\xEA5\\xEA3\\xEA7\\xE7\\x00LI\\s\\xA0\\x0F\\x00\\x00"
+            + "\\xD46\\xEA5\\xEA3\\xEA7\\xE7\\x00LI\\x5Cs\\xA0\\x0F\\x00\\x00"
             + "\\xAB\\x85g\\x91"; //  4 byte checksum
     final int correctGzipBlockLength = 95;
     assertEquals(correctTestBlockStr, createTestBlockStr(GZ,

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java?rev=1456306&r1=1456305&r2=1456306&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java (original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java Thu Mar 14 02:26:17 2013
@@ -157,7 +157,7 @@ public class TestHFileBlockCompatibility
             + "\\x03"
             + "\\xED\\xC3\\xC1\\x11\\x00 \\x08\\xC00DD\\xDD\\x7Fa"
             + "\\xD6\\xE8\\xA3\\xB9K\\x84`\\x96Q\\xD3\\xA8\\xDB\\xA8e\\xD4c"
-            + "\\xD46\\xEA5\\xEA3\\xEA7\\xE7\\x00LI\\s\\xA0\\x0F\\x00\\x00";
+            + "\\xD46\\xEA5\\xEA3\\xEA7\\xE7\\x00LI\\x5Cs\\xA0\\x0F\\x00\\x00";
     final int correctGzipBlockLength = 82;
     assertEquals(correctTestBlockStr, createTestBlockStr(GZ,
         correctGzipBlockLength));

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java?rev=1456306&r1=1456305&r2=1456306&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java (original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java Thu Mar 14 02:26:17 2013
@@ -27,6 +27,7 @@ import java.io.IOException;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.Arrays;
+import java.util.Random;
 
 import junit.framework.TestCase;
 import org.apache.hadoop.hbase.SmallTests;
@@ -221,6 +222,31 @@ public class TestBytes extends TestCase 
     }
   }
 
+  public void testToStringBytesBinaryReversible() {  
+    //  let's run test with 1000 randomly generated byte arrays
+    Random rand = new Random(System.currentTimeMillis());
+    byte[] randomBytes = new byte[1000];
+    for (int i = 0; i < 1000; i++) {
+      rand.nextBytes(randomBytes);
+      verifyReversibleForBytes(randomBytes); 
+    }
+    
+        
+    //  some specific cases
+    verifyReversibleForBytes(new  byte[] {});
+    verifyReversibleForBytes(new  byte[] {'\\', 'x', 'A', 'D'});
+    verifyReversibleForBytes(new  byte[] {'\\', 'x', 'A', 'D', '\\'});
+  }
+
+  private void verifyReversibleForBytes(byte[] originalBytes) {  
+    String convertedString = Bytes.toStringBinary(originalBytes);
+    byte[] convertedBytes = Bytes.toBytesBinary(convertedString);
+    if (Bytes.compareTo(originalBytes, convertedBytes) != 0) {
+      fail("Not reversible for\nbyte[]: " + Arrays.toString(originalBytes) +
+          ",\nStringBinary: " + convertedString);
+    }
+  }
+
   public void testStartsWith() {
     assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("h")));
     assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("")));