public static void fromChars(CharSequence cs, ByteBuffer bb) { fromChars(CharBuffer.wrap(cs), bb) ; }
public static void fromChars(CharSequence cs, ByteBuffer bb) { fromChars(CharBuffer.wrap(cs), bb) ; }
public static void fromChars(CharSequence cs, ByteBuffer bb) { fromChars(CharBuffer.wrap(cs), bb) ; }
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) ; }
str = NodeFmtLib.str(node, (String)null, pmap0) ; BlockUTF8.fromChars(str, bb) ; bb.flip() ; return bb.limit() ;