Java tutorial
import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; public class Main { public static void main(String[] args) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(baos); byte[] bytes = { 1, 2, 3, 4, 5 }; bos.write(bytes, 0, 5); bos.flush(); for (byte b : baos.toByteArray()) { System.out.print(b); } } }