public static ChannelBuffer writeString ( String msg , Charset charset ) { ChannelBuffer buffer = null ; try { ChannelBuffer stringBuffer = null ; if ( null == charset ) { charset = CharsetUtil . UTF_8 ; } stringBuffer = copiedBuffer ( ByteOrder . BIG_ENDIAN , msg , charset ) ; int length = stringBuffer . readableBytes ( ) ; ChannelBuffer lengthBuffer = ChannelBuffers . buffer ( 2 ) ; lengthBuffer . writeShort ( length ) ; buffer = ChannelBuffers . wrappedBuffer ( lengthBuffer , stringBuffer ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } return buffer ; }
Creates a channel buffer of which the first 2 bytes contain the length of the string in bytes and the remaining is the actual string in binary with specified format . Defaults to UTF - 8 encoding in case charset passed in is null