public static void fromChars(CharSequence cs, ByteBuffer bb) { fromChars(CharBuffer.wrap(cs), bb) ; }
/** 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()) ; }
private static int readMultiBytes(ByteBuffer input, int start, int len) { // We have already read one byte. if ( input.remaining() < (len-1) ) exception("Premature end to UTF-8 sequence at end of input") ; int x = start ; for ( int i = 0 ; i < len-1 ; i++ ) { int x2 = input.get() ; if ( (x2 & 0xC0) != 0x80 ) exception("Illegal UTF-8 processing character: 0x%04X",x2) ; // 6 bits of x2 x = (x << 6) | (x2 & 0x3F); } return x ; }
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)) ; }
exception("Illegal UTF-8 processing character: 0x%04X",x2) ; int ch = readMultiBytes(bb, x & 0x0F, 3) ; cb.put((char)ch) ; idx += 3 ; int ch = readMultiBytes(bb, x & 0x08, 4) ; char chars[] = Character.toChars(ch) ; cb.put(chars) ; exception("Illegal UTF-8: 0x%04X",x) ; return ;
/** Convert the bytes in the ByteBuffer to characters in the CharBuffer. * The CharBuffer must be large enough. */ public static void toChars(ByteBuffer bb, CharBuffer cb) { // if ( bb.hasArray() && cb.hasArray() ) // { // toCharsArray(bb.array(), cb.array()) ; // return ; // } toCharsBuffer(bb, cb) ; }
/** Convert characters to UTF-8 bytes in the ByteBuffer. * The ByteBuffer must be large enough. */ public static void fromChars(CharBuffer cb, ByteBuffer bb) { // if ( bb.hasArray() && cb.hasArray() ) // { // fromCharsArray(cb.array(), bb.array()) ; // return ; // } fromCharsBuffer(cb, bb) ; }
outputBytes(bb, x1, 4, ch) ; outputBytes(bb, x1, 5, ch) ; outputBytes(bb, x1, 6, ch) ;
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()) ; }
exception("Illegal UTF-8 processing character: 0x%04X",x2) ; int ch = readMultiBytes(bb, x & 0x0F, 3) ; cb.put((char)ch) ; idx += 3 ; int ch = readMultiBytes(bb, x & 0x08, 4) ; char chars[] = Character.toChars(ch) ; cb.put(chars) ; exception("Illegal UTF-8: 0x%04X",x) ; return ;
/** Convert the bytes in the ByteBuffer to characters in the CharBuffer. * The CharBuffer must be large enough. */ public static void toChars(ByteBuffer bb, CharBuffer cb) { // if ( bb.hasArray() && cb.hasArray() ) // { // toCharsArray(bb.array(), cb.array()) ; // return ; // } toCharsBuffer(bb, cb) ; }
/** Convert characters to UTF-8 bytes in the ByteBuffer. * The ByteBuffer must be large enough. */ public static void fromChars(CharBuffer cb, ByteBuffer bb) { // if ( bb.hasArray() && cb.hasArray() ) // { // fromCharsArray(cb.array(), bb.array()) ; // return ; // } fromCharsBuffer(cb, bb) ; }
outputBytes(bb, x1, 4, ch) ; outputBytes(bb, x1, 5, ch) ; outputBytes(bb, x1, 6, ch) ;
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)) ; }
public static void fromChars(CharSequence cs, ByteBuffer bb) { fromChars(CharBuffer.wrap(cs), bb) ; }
exception("Illegal UTF-8 processing character: 0x%04X",x2) ; int ch = readMultiBytes(bb, x & 0x0F, 3) ; cb.put((char)ch) ; idx += 3 ; int ch = readMultiBytes(bb, x & 0x08, 4) ; char chars[] = Character.toChars(ch) ; cb.put(chars) ; exception("Illegal UTF-8: 0x%04X",x) ; return ;
/** 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()) ; }
/** Convert the bytes in the ByteBuffer to characters in the CharBuffer. * The CharBuffer must be large enough. */ public static void toChars(ByteBuffer bb, CharBuffer cb) { // if ( bb.hasArray() && cb.hasArray() ) // { // toCharsArray(bb.array(), cb.array()) ; // return ; // } toCharsBuffer(bb, cb) ; }
/** Convert characters to UTF-8 bytes in the ByteBuffer. * The ByteBuffer must be large enough. */ public static void fromChars(CharBuffer cb, ByteBuffer bb) { // if ( bb.hasArray() && cb.hasArray() ) // { // fromCharsArray(cb.array(), bb.array()) ; // return ; // } fromCharsBuffer(cb, bb) ; }
private static int readMultiBytes(ByteBuffer input, int start, int len) { // We have already read one byte. if ( input.remaining() < (len-1) ) exception("Premature end to UTF-8 sequence at end of input") ; int x = start ; for ( int i = 0 ; i < len-1 ; i++ ) { int x2 = input.get() ; if ( (x2 & 0xC0) != 0x80 ) exception("Illegal UTF-8 processing character: 0x%04X",x2) ; // 6 bits of x2 x = (x << 6) | (x2 & 0x3F); } return x ; }