Java String Format formatNameToMimeType(final String formatName)

Here you can find the source of formatNameToMimeType(final String formatName)

Description

Returns the mime type matching the format name of an image file.

License

Open Source License

Parameter

Parameter Description
format name The format name of an image file.

Exception

Parameter Description
IIOException if no image reader are able to handle the format name given.

Return

The mime type for the format name specified.

Declaration

public static String formatNameToMimeType(final String formatName) throws IIOException 

Method Source Code

//package com.java2s;
/*/*from   w w w .  j a  v  a 2  s  .  c o m*/
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2004 - 2008, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2008 - 2010, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.util.Iterator;

import javax.imageio.IIOException;

import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;

public class Main {
    /**
     * Returns the mime type matching the format name of an image file.
     * For example, for a format name "png" it will return "image/png", in most cases.
     *
     * @param format name The format name of an image file.
     * @return The mime type for the format name specified.
     *
     * @throws IIOException if no image reader are able to handle the format name given.
     */
    public static String formatNameToMimeType(final String formatName) throws IIOException {
        final Iterator<ImageReaderSpi> readers = IIORegistry.lookupProviders(ImageReaderSpi.class);
        while (readers.hasNext()) {
            final ImageReaderSpi reader = readers.next();
            final String[] formats = reader.getFormatNames();
            for (String format : formats) {
                if (formatName.equalsIgnoreCase(format)) {
                    final String[] mimeTypes = reader.getMIMETypes();
                    if (mimeTypes != null && mimeTypes.length > 0) {
                        return mimeTypes[0];
                    }
                }
            }
        }
        throw new IIOException("No available image reader able to handle the format name specified: " + formatName);
    }
}

Related

  1. formatError(String errorMsg)
  2. formatJson(String json)
  3. formatLore(String str)
  4. formatMailtoLink(String emailAddress)
  5. formatMessage(String message)
  6. formatParam(String param)
  7. formatProtein(String seq)
  8. formatString(String stringFormat, String... args)
  9. formatStringTo80Columns(String str)