public WrappedByteBuffer putPrefixedString ( int fieldSize , String v , Charset cs ) { if ( fieldSize == 0 ) { return this ; } boolean utf16 = cs . name ( ) . startsWith ( "UTF-16" ) ; if ( utf16 && ( fieldSize == 1 ) ) { throw new IllegalArgumentException ( "fieldSize is not even for UTF-16 character set" ) ; } java . nio . ByteBuffer strBuf = cs . encode ( v ) ; _autoExpand ( fieldSize + strBuf . limit ( ) ) ; int len = strBuf . remaining ( ) ; switch ( fieldSize ) { case 1 : put ( ( byte ) len ) ; break ; case 2 : putShort ( ( short ) len ) ; break ; case 4 : putInt ( len ) ; break ; default : throw new IllegalArgumentException ( "Illegal argument, field size should be 1,2 or 4 and fieldSize is: " + fieldSize ) ; } _buf . put ( strBuf ) ; return this ; }
Puts a string into the buffer at the specified index using the character set to encode the string as bytes .