Here you can find the source of getBytes(final InputStream is, final int bufferSize)
public static byte[] getBytes(final InputStream is, final int bufferSize) throws IOException
//package com.java2s; //License from project: LGPL import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.lang.reflect.Array; public class Main { public static byte[] getBytes(final BufferedImage image) throws IOException { return getBytes(image, "jpg"); }/*from w w w . jav a 2 s . co m*/ public static byte[] getBytes(final BufferedImage image, final String format) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, null != format ? format : "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); return imageInByte; } public static byte[] getBytes(final InputStream is) throws IOException { return getBytes(is, 1024); } public static byte[] getBytes(final InputStream is, final int bufferSize) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize); try { byte[] buffer = new byte[bufferSize]; int len; while ((len = is.read(buffer)) >= 0) { out.write(buffer, 0, len); } } finally { out.close(); } return out.toByteArray(); } public static byte[] getBytes(final Object data) throws IOException { byte[] result = new byte[0]; if (!isByteArray(data)) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(data); result = bos.toByteArray(); } finally { if (null != out) out.close(); bos.close(); } } else { result = (byte[]) data; } return result; } public static boolean isByteArray(final Object data) { if (data.getClass().isArray()) { final Object val = Array.get(data, 0); if (val instanceof Byte) { return true; } } return false; } }