Here you can find the source of readImage(InputStream inp)
public static ImageIcon readImage(InputStream inp)
//package com.java2s; //License from project: Apache License import javax.swing.*; import java.io.*; public class Main { public static ImageIcon readImage(InputStream inp) { byte[] bytes = readInBytes(inp); return new ImageIcon(bytes); }// w ww. j a v a2 s . c om public static byte[] readInBytes(InputStream inp, int lenthGuess) { InputStream TheStream = new BufferedInputStream(inp); return readInFile(TheStream, 4096, lenthGuess); } public static byte[] readInBytes(InputStream inp) { return readInBytes(inp, 4096); } /** * { method * * @param TheStream the file stream * @param len the file length or a good guess * @return StringBuilder holding file bytes * } * @name readInFile * @function reads all the data in a file into a StringBuilder */ public static byte[] readInFile(InputStream TheStream, int len, int chunk) { ByteArrayOutputStream os = new ByteArrayOutputStream(len); int NRead = 0; byte[] buffer = new byte[chunk]; try { NRead = TheStream.read(buffer, 0, chunk); while (NRead != -1) { os.write(buffer, 0, NRead); NRead = TheStream.read(buffer, 0, chunk); // ought to look at non-printing chars } TheStream.close(); } catch (IOException ex) { throw new RuntimeException(ex); } return (os.toByteArray()); } /** * { method * * @param TheStream the file stream * @param len the file length or a good guess * @param maxlen maxlength to read * @return file bytes * } * @name readInFile * @function reads all the data in a file into a StringBuilder */ public static byte[] readInFile(InputStream TheStream, int len, int chunk, int maxlen) { ByteArrayOutputStream os = new ByteArrayOutputStream(len); int NRead = 0; byte[] buffer = new byte[chunk]; chunk = Math.min(chunk, maxlen - os.size()); try { NRead = TheStream.read(buffer, 0, chunk); while (NRead != -1 && os.size() < maxlen) { os.write(buffer, 0, NRead); chunk = Math.min(chunk, maxlen - os.size()); NRead = TheStream.read(buffer, 0, chunk); // ought to look at non-printing chars } TheStream.close(); } catch (IOException ex) { throw new RuntimeException(ex); } return (os.toByteArray()); } }