title: How to read image bytes in java?
body: <p>I am trying to display image in jsp page for that i am using bellow code.Everything was fine but some error was displayed and i don't know how to fix this can any one help me to fix this.</p>
  
  <p><strong>code:</strong></p>
  
  <pre><code>  &lt;%@page import="java.awt.image.BufferedImage"%&gt;
  &lt;%@page import="javax.imageio.ImageIO"%&gt;
  &lt;%@page import="java.io.*"%&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;%
  InputStream inStream = null;
  BufferedInputStream bis = null;
  BufferedImage bImage=null;
  
  inStream = new FileInputStream("/home/anand/Desktop/encrypt/firstBG.jpg");
  bis = new BufferedInputStream(inStream);
  int numByte = bis.available();
  byte[] buf = new byte[numByte];
  bis.read(buf, 2, 3);
  
  
   bImage = ImageIO.read(new ByteArrayInputStream(buf));//give the path of an image
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ImageIO.write( bImage, "jpg", baos );
  baos.flush();
  byte[] imageInByteArray = baos.toByteArray();
  baos.close();
  String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
  %&gt;
  
  &lt;div&gt;
      &lt;p&gt;As of v6, Java SE provides JAXB&lt;/p&gt;
      &lt;img src="data:image/jpg;base64, &lt;%=b64%&gt;" alt="Visruth.jpg not found" /&gt;
  &lt;/div&gt;          
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>error</strong></p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [jsp] in context with path [/get] threw exception [An exception occurred processing JSP page /NewFile2.jsp at line 27
  
  24: 
  25:  bImage = ImageIO.read(new ByteArrayInputStream(buf));//give the path of an image
  26: ByteArrayOutputStream baos = new ByteArrayOutputStream();
  27: ImageIO.write( bImage, "jpg", baos );
  28: baos.flush();
  29: byte[] imageInByteArray = baos.toByteArray();
  30: baos.close();
  
  
  Stacktrace:] with root cause
  java.lang.IllegalArgumentException: image == null!
      at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
      at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
      at javax.imageio.ImageIO.write(ImageIO.java:1578)
      at org.apache.jsp.NewFile2_jsp._jspService(NewFile2_jsp.java:90)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>I know the problem is writing bytes in BufferedImage but i don't know how to fix this.</p>