Here you can find the source of writeString(ByteBuffer buffer, String string)
public static void writeString(ByteBuffer buffer, String string) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; public class Main { public static void writeString(ByteBuffer buffer, String string) throws IOException { try {// ww w .j a v a 2 s. c o m byte[] abyte = string.getBytes(StandardCharsets.UTF_8); if (abyte.length > 32767) { throw new IOException("String too big (was " + abyte.length + " bytes encoded, max " + 32767 + ")"); } else { writeVarInt(buffer, abyte.length); buffer.put(abyte, 0, abyte.length); } } catch (UnsupportedEncodingException e) { throw new IOException(e); } } public static void writeVarInt(ByteBuffer buffer, int input) { while ((input & -128) != 0) { buffer.put((byte) (input & 127 | 128)); input >>>= 7; } buffer.put((byte) input); } }