title: Mapping file suffix to content type with ImageIO
body: <p>I'm tidying up some code that uses <a href="http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html" rel="nofollow">javax.imageio.ImageIO</a>. I have the file suffix the user is requesting, e.g. jpg, and we transform a saved image then render it back to the user in the correct format.</p>
  
  <p>Now I want to map the suffix to the MIME type. This is relatively easy to do with a quick switch, but given that ImageIO has both</p>
  
  <ul>
  <li><code>static Iterator&lt;ImageWriter&gt;</code> <a href="http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#getImageWritersBySuffix%28java.lang.String%29" rel="nofollow"><code>getImageWritersBySuffix(String fileSuffix)</code></a></li>
  <li><code>static Iterator&lt;ImageWriter&gt;</code> <a href="http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#getImageWritersByMIMEType%28java.lang.String%29" rel="nofollow"><code>getImageWritersByMIMEType(String MIMEType)</code></a></li>
  </ul>
  
  <p>it must know the mapping between suffix and MIME type, or the MIME type generated by a specific <a href="http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageWriter.html" rel="nofollow"><code>ImageWriter</code></a>. Therefore there must be a way to get the content type from the writer that I'm about to use. But I don't see any methods or properties on ImageWriter that return the content type, or even the suffices.</p>
  
  <p>Essentially I'm trying to complete the following block of code:</p>
  
  <pre><code>Iterator&lt;ImageWriter&gt; imageWriters = ImageIO.getImageWritersBySuffix(type);
  ImageWriter imageWriter = imageWriters.hasNext() ? imageWriters.next() : null;
  if (imageWriter != null) {
      contentType = imageWriter. ???
  }
  </code></pre>
  
  <p>Thanks! The brute force approach I guess is to get all of the MIME types supported then iterate through those until I find one that returns the same ImageWriter as the get-by-suffix call (if indeed it's easy to compare the results) but I'm hoping for something a little more elegant than that - I'd prefer a switch to that.</p>