/** Make a string from UTF-8 bytes in a ByteBuffer */ public static String toString(ByteBuffer bb) { // I think that the copy from some mutable collector to immutable string is inevitable in java. int len = bb.remaining() ; CharBuffer cb = CharBuffer.allocate(len) ; toChars(bb, cb) ; return new String(cb.array(), 0, cb.position()) ; }
/** Make a string from UTF-8 bytes in a ByteBuffer */ public static String toString(ByteBuffer bb) { // I think that the copy from some mutable collector to immutable string is inevitable in java. int len = bb.remaining() ; CharBuffer cb = CharBuffer.allocate(len) ; toChars(bb, cb) ; return new String(cb.array(), 0, cb.position()) ; }
/** Make a string from UTF-8 bytes in a ByteBuffer */ public static String toString(ByteBuffer bb) { // I think that the copy from some mutable collector to immutable string is inevitable in java. int len = bb.remaining() ; CharBuffer cb = CharBuffer.allocate(len) ; toChars(bb, cb) ; return new String(cb.array(), 0, cb.position()) ; }
static void testBinary(byte[] binary, CharBuffer chars) { int N = binary.length ; ByteBuffer bytes = ByteBuffer.wrap(binary) ; CharBuffer cb = CharBuffer.allocate(N) ; BlockUTF8.toChars(bytes, cb) ; cb.flip() ; assertTrue("Binary", sameChars(chars, cb)); }
static void testInOutBinary(String x) { int N = x.length() ; CharBuffer cb = CharBuffer.wrap(x.toCharArray()) ; ByteBuffer bb = ByteBuffer.allocate(4*N) ; BlockUTF8.fromChars(cb, bb) ; bb.flip() ; CharBuffer cb2 = CharBuffer.allocate(N) ; BlockUTF8.toChars(bb, cb2) ; // compare cb and cb2. String str = new String(cb2.array(), 0, cb2.position()) ; assertEquals(x, str) ; // And re-code as bytes. CharBuffer cb3 = CharBuffer.wrap(x.toCharArray()) ; ByteBuffer bb3 = ByteBuffer.allocate(4*N) ; BlockUTF8.fromChars(cb3, bb3) ; bb3.flip() ; assertArrayEquals(bb.array(), bb3.array()) ; }
static void testBinary(byte[] binary, byte[] expected) { int N = binary.length ; ByteBuffer bytes = ByteBuffer.wrap(binary) ; CharBuffer cb = CharBuffer.allocate(N) ; BlockUTF8.toChars(bytes, cb) ; cb.flip() ; bytes.position(0) ; ByteBuffer bytes2 = ByteBuffer.allocate(2*N) ; // Null bytes get expanded. BlockUTF8.fromChars(cb, bytes2) ; bytes2.flip() ; sameBytes(bytes, bytes2) ; assertTrue("Binary", sameBytes(ByteBuffer.wrap(expected), bytes2)) ; }
static void testOut(String x, Alloc<ByteBuffer> allocBB, Alloc<CharBuffer> allocCB) { testBinary(stringAsBytes(x)) ; int N = x.length() ; // First - get bytes the Java way. ByteBuffer bytes = ByteBuffer.wrap(stringAsBytes(x)) ; CharBuffer cb = allocCB.allocate(N) ; BlockUTF8.toChars(bytes, cb) ; cb.flip() ; bytes.flip() ; ByteBuffer bytes2 = allocBB.allocate(bytes.capacity()) ; BlockUTF8.fromChars(cb, bytes2) ; bytes2.flip() ; assertTrue("Chars", sameBytes(bytes, bytes2)) ; }
static void testIn(String x, Alloc<ByteBuffer> allocBB, Alloc<CharBuffer> allocCB) { // Test as binary. testInOutBinary(x) ; // Now test, comparing to std Java. // Correct answer, in bytes ByteBuffer bytes = ByteBuffer.wrap(stringAsBytes(x)) ; // To bytes.stringAsBytes int N = x.length() ; CharBuffer cb = CharBuffer.wrap(x.toCharArray()) ; ByteBuffer bb = allocBB.allocate(4*N) ; BlockUTF8.fromChars(cb, bb) ; bb.flip() ; assertTrue("Bytes", sameBytes(bytes, bb)) ; // From bytes. CharBuffer cb2 = allocCB.allocate(N) ; BlockUTF8.toChars(bb, cb2) ; cb2.flip() ; String str = cb2.toString() ; assertEquals(x, str) ; }