org.clapper.util.misc
Class MIMETypeUtil

java.lang.Object
  extended by org.clapper.util.misc.MIMETypeUtil

public class MIMETypeUtil
extends java.lang.Object

The MIMETypeUtil class provides some general purpose MIME type utilities not found in the JDK. Among other methods, this class provides the fileExtensionForMIMEType() method, which converts a MIME type to a file extension. That method uses a traditional mime.types files, similar to the file shipped with with web servers such as Apache. It looks for a suitable file in the following locations:

  1. First, it looks for the file .mime.types in the user's home directory.
  2. Next, it looks for mime.types (no leading ".") in all the directories in the CLASSPATH
  3. Last, it loads a default set of mappings shipped with this library

It loads all the matching files it finds; the first mapping found for a given MIME type is the one that is used. The files are only loaded once within a given running Java VM.

The syntax of the file follows the classic mime.types syntax:

 # The format is  
 # Comments begin with a '#'

 text/plain             txt text TXT
 text/html              html htm HTML HTM
 ...
 

When mapping a MIME type to an extension, fileExtensionForMIMEType() uses the first extension it finds in the mime.types file. MIME types that cannot be found in the file are mapped to extension ".dat".

Version:
$Revision: 6735 $
Author:
Copyright © 2004-2007 Brian M. Clapper

Field Summary
static java.lang.String DEFAULT_MIME_TYPE
          Default MIME type, when a MIME type cannot be determined from a file's extension.
 
Method Summary
static java.lang.String fileExtensionForMIMEType(java.lang.String mimeType)
          Get an appropriate extension for a MIME type.
static java.lang.String MIMETypeForFile(java.io.File file)
          Get the MIME type for a file.
static java.lang.String MIMETypeForFile(java.io.File file, java.lang.String defaultMIMEType)
          Get the MIME type for a file.
static java.lang.String MIMETypeForFileExtension(java.lang.String extension)
          Get the MIME type for a filename extension.
static java.lang.String MIMETypeForFileExtension(java.lang.String extension, java.lang.String defaultMIMEType)
          Get the MIME type for a filename extension.
static java.lang.String MIMETypeForFileName(java.lang.String fileName)
          Get the MIME type for a name file.
static java.lang.String MIMETypeForFileName(java.lang.String fileName, java.lang.String defaultMIMEType)
          Get the MIME type for a file name.
static void parseContentTypeHeader(java.lang.String contentTypeHeader, java.lang.StringBuffer mimeType, java.util.Map<java.lang.String,java.lang.String> parameters)
          This method parses an HTTP-style "Content-type" header into its constituent pieces.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MIME_TYPE

public static final java.lang.String DEFAULT_MIME_TYPE
Default MIME type, when a MIME type cannot be determined from a file's extension.

See Also:
MIMETypeForFile(java.io.File), MIMETypeForFileName(java.lang.String), Constant Field Values
Method Detail

fileExtensionForMIMEType

public static java.lang.String fileExtensionForMIMEType(java.lang.String mimeType)
Get an appropriate extension for a MIME type.

Parameters:
mimeType - the String MIME type
Returns:
the appropriate file name extension, or a default extension if not found. The extension will not have the leading "." character.

MIMETypeForFileExtension

public static java.lang.String MIMETypeForFileExtension(java.lang.String extension)
Get the MIME type for a filename extension.

Parameters:
extension - the extension, without the "."
Returns:
the MIME type, or a default MIME type if there's no mapping for the extension
See Also:
MIMETypeForFileExtension(String,String), MIMETypeForFile(File), MIMETypeForFile(File,String), MIMETypeForFileName(String), MIMETypeForFileName(String,String)

MIMETypeForFileExtension

public static java.lang.String MIMETypeForFileExtension(java.lang.String extension,
                                                        java.lang.String defaultMIMEType)
Get the MIME type for a filename extension.

Parameters:
extension - the extension, without the "."
defaultMIMEType - the default MIME type to use if one cannot be determined from the extension, or null to use DEFAULT_MIME_TYPE
Returns:
the MIME type, or the default MIME type
See Also:
MIMETypeForFileExtension(String), MIMETypeForFile(File), MIMETypeForFile(File,String), MIMETypeForFileName(String), MIMETypeForFileName(String,String)

MIMETypeForFile

public static java.lang.String MIMETypeForFile(java.io.File file)
Get the MIME type for a file. This method is simply a convenient front-end for java.net.FileNameMap.getContentTypeFor(), but it applies a consistent default when getContentTypeFor() returns null (which can happen).

Parameters:
file - the file
Returns:
the MIME type to use
See Also:
MIMETypeForFile(File,String), MIMETypeForFileName(String), MIMETypeForFileExtension(String), MIMETypeForFileExtension(String,String), DEFAULT_MIME_TYPE

MIMETypeForFile

public static java.lang.String MIMETypeForFile(java.io.File file,
                                               java.lang.String defaultMIMEType)
Get the MIME type for a file. This method is simply a convenient front-end for java.net.FileNameMap.getContentTypeFor(), but it applies the supplied default when getContentTypeFor() returns null (which can happen).

Parameters:
file - the file
defaultMIMEType - the default MIME type to use if one cannot be determined from the file's name, or null to use DEFAULT_MIME_TYPE
Returns:
the MIME type to use
See Also:
MIMETypeForFile(File), MIMETypeForFileName(String,String), MIMETypeForFileExtension(String), MIMETypeForFileExtension(String,String), DEFAULT_MIME_TYPE

MIMETypeForFileName

public static java.lang.String MIMETypeForFileName(java.lang.String fileName)
Get the MIME type for a name file. This method is simply a convenient front-end for java.net.FileNameMap.getContentTypeFor(), but it applies a consistent default when getContentTypeFor() returns null (which can happen).

Parameters:
fileName - the file name
Returns:
the MIME type to use
See Also:
MIMETypeForFile(File), MIMETypeForFileName(String,String), DEFAULT_MIME_TYPE

MIMETypeForFileName

public static java.lang.String MIMETypeForFileName(java.lang.String fileName,
                                                   java.lang.String defaultMIMEType)
Get the MIME type for a file name. This method is simply a convenient front-end for java.net.FileNameMap.getContentTypeFor(), but it applies the supplied default when getContentTypeFor() returns null (which can happen).

Parameters:
fileName - the file name
defaultMIMEType - the default MIME type to use if one cannot be determined from the file name, or null to use DEFAULT_MIME_TYPE
Returns:
the MIME type to use
See Also:
MIMETypeForFile(File,String), MIMETypeForFileName(String), MIMETypeForFileExtension(String), MIMETypeForFileExtension(String,String), DEFAULT_MIME_TYPE

parseContentTypeHeader

public static void parseContentTypeHeader(java.lang.String contentTypeHeader,
                                          java.lang.StringBuffer mimeType,
                                          java.util.Map<java.lang.String,java.lang.String> parameters)

This method parses an HTTP-style "Content-type" header into its constituent pieces. The HTTP specification (RFC 2616) defines the Content-type header as:

"Content-type" ":" 

A media-type is a MIME type ("type/subtype"), with optional name=value pair parameters. The parameters are separated from the media type, and from each other, by ";" characters.

A common example of a Content-type header is:

Content-type: text/html; charset=ISO-8859-1

This method parses apart the MIME type and the parameters, saving each one separately in caller-supplied objects.

Parameters:
contentTypeHeader - the header value (without the Content-type: prefix). This value is what's typically returned by URLConnection.getContentType().
mimeType - a StringBuffer to receive the MIME type, or null if you don't care about the MIME type. This method clears the string buffer before storing anything in it.
parameters - a Map to receive the parameters, or null if you don't care about the parameters. The parameter names are used as the map's keys. This method clears the map before storing anything in it.


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.