title: Validating JPEG from byte array - specifically the APP segment
body: <p>I am reviewing legacy code that handles different tpyes of images, including JPEG.  The legacy code uses a 4 byte sequence to determine if a byte[] is a valid JPEG.  Specifically:</p>
  
  <p>0xFF 0xD8 0xFF and either 0xE0 or 0xE1.</p>
  
  <p>When I did my research I found that all you need is 0xFF 0xD8 0xFF, the 0xE0 or 0xE1 are part of what is known as the APP segment (<a href="http://en.wikipedia.org/wiki/JPEG#Syntax_and_structure" rel="nofollow">http://en.wikipedia.org/wiki/JPEG#Syntax_and_structure</a>).  This segment is application specific and other research I have done indicates there are at least 4 other values possible in this segment (0xE2, 0xE3, 0xE8 and 0xED).</p>
  
  <p>I thought a JPEG is a JPEG is a JPEG.  That given a file with any one of the 6 known/allowed APP segments that any device that can display a JPEG would be able to display the given file.  Is this correct?</p>
  
  <p>Is there a good reason for filtering based on particular values of the APP segment?</p>
  
  <p>How exactly does the APP segment effect the JPEG image?  Is it used at all or is it application specific data that is only used when the displaying application recognizes their APP value?  For instance I read that 0xED is used by Photoshop.  So if the image was being displayed by Photoshop then the data in the APP segment is meaningful - but to any other app that is not Photoshop the APP segment is ignored and the image is displayed just fine.</p>
  
  <p>NOTE: in the end a Java applet will be displaying the JPEG.</p>