com.faunos.util.io.file
Class ExtensionMimeMap

java.lang.Object
  extended by com.faunos.util.io.file.ExtensionMimeMap
All Implemented Interfaces:
MimeMap

public class ExtensionMimeMap
extends Object
implements MimeMap

A MimeMap implementation based solely on a file's extension. The actual contents of the file is not examined by this class.

Author:
Babak Farhang

Field Summary
static String DEFAULT_PROP_FILENAME
           
 
Constructor Summary
ExtensionMimeMap()
           
 
Method Summary
 String getMimeType(File file)
          Returns the MIME type for the specified file, or null, if one cannot be determined.
 ExtensionMimeMap load(InputStream input)
          Leniently loads the extension mappings from the specified property stream.
 ExtensionMimeMap load(InputStream input, boolean strict)
          Loads the extension mappings from the specified property stream.
 String mapExtension(String ext, String mimetype)
          Maps the specified extension to the given MIME type and returns the previous mapped MIME type, if any.
static ExtensionMimeMap newDefaultInstance()
          Returns the a new default instance.
 String toString()
          Returns a string representation of this instance for debugging purposes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_PROP_FILENAME

public static final String DEFAULT_PROP_FILENAME
See Also:
Constant Field Values
Constructor Detail

ExtensionMimeMap

public ExtensionMimeMap()
Method Detail

newDefaultInstance

public static ExtensionMimeMap newDefaultInstance()
Returns the a new default instance. The default instance is loaded from a class resource named mime-file-ext.properties.


getMimeType

public String getMimeType(File file)
Description copied from interface: MimeMap
Returns the MIME type for the specified file, or null, if one cannot be determined.

Specified by:
getMimeType in interface MimeMap

mapExtension

public String mapExtension(String ext,
                           String mimetype)
Maps the specified extension to the given MIME type and returns the previous mapped MIME type, if any. The input parameters are trimmed and some validation is performed.

Parameters:
ext - the filename extension. Must be 2 characters or more, and must start with '.'
mimetype - the MIME type to be associated with the given extension. Must not be empty.

load

public ExtensionMimeMap load(InputStream input)
                      throws IOException
Leniently loads the extension mappings from the specified property stream. This behaves exactly as if invoking load(input, false).

Throws:
IOException

load

public ExtensionMimeMap load(InputStream input,
                             boolean strict)
                      throws IOException
Loads the extension mappings from the specified property stream. The format is a simplified version of that found in $JAVA_HOME/jre/lib/content-types.properties:


      myMimeType : .ext1,.ext2,.anotherExt
 
This is an inverted format. The load method inverts the mapping and generates many-to-one mappings from file extension to MIME type.

The actual map is updated in one step after all I/O is completed.

Parameters:
strict - if true, the properties file is checked for internal consistency (no extension defined in more than one place) and form (see mapExtension(ext, mimetype) for specifics). Additionally, existing mappings in this instance may not be overwritten; o.w. (if false), existing mapping may be overwritten, and malformed settings are skipped.
Returns:
this instance (for invocation chaining)
Throws:
IOException

toString

public String toString()
Returns a string representation of this instance for debugging purposes.

Overrides:
toString in class Object


SourceForge.net Logo