Here you can find the source of writeString(ByteArrayOutputStream bout, String val)
public static void writeString(ByteArrayOutputStream bout, String val) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; public class Main { private final static String ENCODING = "UTF-8"; public static void writeString(ByteArrayOutputStream bout, String val) throws IOException { if (val == null) { writeInt(bout, -1);//w w w .j av a 2s .c o m return; } if (val.length() == 0) { writeInt(bout, 0); return; } byte[] bytes = val.getBytes(ENCODING); writeInt(bout, bytes.length); bout.write(bytes); } public static void writeInt(ByteArrayOutputStream bout, int val) throws IOException { ByteBuffer b = ByteBuffer.allocate(4); b.putInt(val); byte[] bytes = b.array(); bout.write(bytes); } }