Java tutorial
//package com.java2s; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main { public static void putMessageLength(byte[] message) { ByteBuffer lengthBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN); lengthBuffer.putInt(message.length); byte[] lenArray = lengthBuffer.array(); for (int i = 0; i < 4; i++) message[i] = lenArray[i]; } }