title: Can I tell what the file type of a BufferedImage originally was?
body: <p>In my code, I have a <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html" rel="noreferrer">BufferedImage</a> that was loaded with the <a href="http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html" rel="noreferrer">ImageIO</a> class like so:</p>
  
  <pre><code>BufferedImage image = ImageIO.read(new File (filePath);
  </code></pre>
  
  <p>Later on, I want to save it to a byte array, but the <code>ImageIO.write</code> method requires me to pick either a GIF, PNG, or JPG format to write my image as (as described in the tutorial <a href="http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html" rel="noreferrer">here</a>).</p>
  
  <p>I want to pick the same file type as the original image. If the image was originally a GIF, I don't want the extra overhead of saving it as a PNG. But if the image was originally a PNG, I don't want to lose translucency and such by saving it as a JPG or GIF. Is there a way that I can determine from the BufferedImage what the original file format was? </p>
  
  <p>I'm aware that I could simply parse the file path when I load the image to find the extension and just save it for later, but I'd ideally like a way to do it straight from the BufferedImage.</p>