title: Read Image File Through Java Socket
body: <p>This is what I have so far,</p>
  
  <pre><code>Socket clientSocket = new Socket(HOST, PORT);
  
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  
  InputStream is = socket.getInputStream();
  byte[] byteChunk = new byte[1024];
  
  int c = is.read(byteChunk);
  
  while (c != -1){
      buffer.write(byteChunk, 0, c);
      c = is.read(byteChunk);
  }
  
  BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(buffer.toByteArray()));
  </code></pre>
  
  <p>My problem with my code is <code>ImageIO.read()</code> returns null.</p>
  
  <p>When I print the content of <code>ByteArrayOutputStream</code> object what i get is header part</p>
  
  <pre><code>HTTP/1.1 200 OK
  Date: Fri, 30 Dec 2011 11:34:19 GMT
  Server: Apache/2.2.3 (Debian) ...........
  Last-Modified: Tue, 20 Dec 2011 19:12:23 GMT
  ETag: "502812-490e-4b48ad8d273c0"
  Accept-Ranges: bytes
  Content-Length: 18702
  Connection: close
  Content-Type: image/jpeg
  </code></pre>
  
  <p>followed with a empty line plus many lines with different characters such as <code>Àã$sU,e6‡Í~áŸP;Öã…</code>. </p>
  
  <p>Again my problem is <code>ImageIO.read()</code> function returns null.</p>
  
  <p>Thanks in advance.</p>