Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java (revision 1399094) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java (working copy) @@ -343,21 +343,16 @@ // Just in case we are passed a 'len' that is > buffer length... if (off >= b.length) return result.toString(); if (off + len > b.length) len = b.length - off; - 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(); } Index: hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java =================================================================== --- hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java (revision 1399094) +++ hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java (working copy) @@ -24,6 +24,7 @@ import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; +import java.util.Random; import junit.framework.TestCase; import org.junit.experimental.categories.Category; @@ -219,6 +220,30 @@ } } + 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(""))); Index: hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java =================================================================== --- hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java (revision 1399094) +++ hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlock.java (working copy) @@ -248,7 +248,7 @@ + "\\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, Index: hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java =================================================================== --- hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java (revision 1399094) +++ hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileBlockCompatibility.java (working copy) @@ -160,7 +160,7 @@ + "\\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; String returnedStr = createTestBlockStr(GZ, correctGzipBlockLength);