Here you can find the source of writeWithLength(ByteBuffer buffer, byte[] src)
public static final void writeWithLength(ByteBuffer buffer, byte[] src)
//package com.java2s; import java.nio.ByteBuffer; public class Main { public static final void writeWithLength(ByteBuffer buffer, byte[] src) { int length = src.length; if (length < 251) { buffer.put((byte) length); } else if (length < 0x10000L) { buffer.put((byte) 252); writeUB2(buffer, length);/* ww w. j a v a2s . co m*/ } else if (length < 0x1000000L) { buffer.put((byte) 253); writeUB3(buffer, length); } else { buffer.put((byte) 254); writeLong(buffer, length); } buffer.put(src); } public static final void writeWithLength(ByteBuffer buffer, byte[] src, byte nullValue) { if (src == null) { buffer.put(nullValue); } else { writeWithLength(buffer, src); } } public static final void writeUB2(ByteBuffer buffer, int i) { buffer.put((byte) (i & 0xff)); buffer.put((byte) (i >>> 8)); } public static final void writeUB3(ByteBuffer buffer, int i) { buffer.put((byte) (i & 0xff)); buffer.put((byte) (i >>> 8)); buffer.put((byte) (i >>> 16)); } public static final void writeLong(ByteBuffer buffer, long l) { buffer.put((byte) (l & 0xff)); buffer.put((byte) (l >>> 8)); buffer.put((byte) (l >>> 16)); buffer.put((byte) (l >>> 24)); buffer.put((byte) (l >>> 32)); buffer.put((byte) (l >>> 40)); buffer.put((byte) (l >>> 48)); buffer.put((byte) (l >>> 56)); } }