Java tutorial
//package com.java2s; //License from project: LGPL import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { private static byte[] transStreamToBytes(InputStream is, int buffSize) { if (is == null) { return null; } if (buffSize <= 0) { throw new IllegalArgumentException("buffSize can not less than zero....."); } byte[] data = null; byte[] buffer = new byte[buffSize]; int actualSize = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { while ((actualSize = is.read(buffer)) != -1) { baos.write(buffer, 0, actualSize); } data = baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } return data; } }