Java tutorial
//package com.java2s; import java.io.IOException; import java.io.InputStream; public class Main { public static byte[] readCLenData(InputStream is) throws IOException { int len = is.read(); if (len <= 0) { return null; } return readBytes(is, len); } public static byte[] readBytes(InputStream in, int len) throws IOException { if (len <= 0) { return null; } int pos = 0, recvBytes = 0; byte[] ret = null; byte[] buffer = new byte[len]; try { while (pos < len && (recvBytes = in.read(buffer, pos, len - pos)) > 0) { pos += recvBytes; } ret = buffer; } finally { buffer = null; } return ret; } }