public ByteVector putUTF8 ( final String s ) { int charLength = s . length ( ) ; if ( charLength > 65535 ) { throw new IllegalArgumentException ( ) ; } int len = length ; if ( len + 2 + charLength > data . length ) { enlarge ( 2 + charLength ) ; } byte [ ] data = this . data ; // optimistic algorithm: instead of computing the byte length and then // serializing the string (which requires two loops), we assume the byte // length is equal to char length (which is the most frequent case), and // we start serializing the string right away. During the serialization, // if we find that this assumption is wrong, we continue with the // general method. data [ len ++ ] = ( byte ) ( charLength >>> 8 ) ; data [ len ++ ] = ( byte ) charLength ; for ( int i = 0 ; i < charLength ; ++ i ) { char c = s . charAt ( i ) ; if ( c >= ' ' && c <= ' ' ) { data [ len ++ ] = ( byte ) c ; } else { length = len ; return encodeUTF8 ( s , i , 65535 ) ; } } length = len ; return this ; }
Puts an UTF8 string into this byte vector . The byte vector is automatically enlarged if necessary .