Here you can find the source of writeString(String s, ByteBuffer buff)
public static void writeString(String s, ByteBuffer buff)
//package com.java2s; //License from project: LGPL import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; public class Main { public static void writeString(String s, ByteBuffer buff) { byte[] bytes = s.getBytes(StandardCharsets.UTF_8); writeVarInt(bytes.length, buff); buff.put(bytes);//from w ww. jav a 2 s .com } /** * Writes a signed integer with the "VarInt" format. * * @param n the int to encode * @param buff where to write the bytes */ public static void writeVarInt(int n, ByteBuffer buff) { while ((n & 0xFFFF_FF80) != 0) {// While we have more than 7 bits (0b0xxxxxxx) byte data = (byte) (n | 0x80);// Discard bit sign and set msb to 1 (VarInt byte prefix). buff.put(data); n >>>= 7; } buff.put((byte) n); } }