Package org.apache.sling.commons.mime

# Interface MimeTypeService

@ProviderType public interface MimeTypeService
The `MimeTypeService` defines the service applications can call to resolve file names to MIME types and derive default file name extensions from MIME types.

This interface is not intended to be implemented by bundles. It is implemented by this bundle and may be used by client bundles.

## Method Summary

Modifier and Type
Method
Description
`String`
`getExtension(String mimeType)`
Returns the primary name extension to which the given `mimeType` maps.
`Map<String,String>`
`getExtensionMap()`
`Map<String,String>`
`getMimeMap()`
`String`
`getMimeType(String name)`
Returns the MIME type of the extension of the given `name`.
`void`
`registerMimeType(InputStream mimeTabStream)`
Register MIME types stored in the given input stream formatted as a regular MIME type file format: One entry per line.
`void`
`registerMimeType(String mimeType, String... extensions)`
Dynamically register a new mime type with one or more file name extensions.

## Method Details

### getMimeType

String getMimeType(String name)
Returns the MIME type of the extension of the given `name`. The extension is the part of the name after the last dot. If the name does not contain a dot, the name as a whole is assumed to be the extension.

Parameters:
  `name` - The name for which the MIME type is to be returned.
Returns:
  The MIME type for the extension of the name. If the extension cannot be mapped to a MIME type or `name` is `null`, `null` is returned.
See Also:
  - `getExtension(String)`

### getExtension

String getExtension(String mimeType)
Returns the primary name extension to which the given `mimeType` maps. The returned extension must map to the given `mimeType` when fed to the `getMimeType(String)` method. In other words, the expression `mimeType.equals(getMimeType(getExtension(mimeType)))` must always be `true` for any non-`null` MIME type.

A MIME type may be mapped to multiple extensions (e.g. `text/plain` to `txt`, `log`, ...). This method is expected to returned one of those extensions. It is up to the implementation to select an appropriate extension if multiple mappings exist for a single MIME type.

Parameters:
  `mimeType` - The MIME type whose primary extension is requested.
Returns:
  A extension which maps to the given MIME type or `null` if no such mapping exists.
See Also:
  - `getMimeType(String)`

### registerMimeType

void registerMimeType(String mimeType, String... extensions)
Dynamically register a new mime type with one or more file name extensions. The first of those extensions is assumed to be default file name extension.

This registration is dynamic and not persisted.

Parameters:
  `mimeType` - The MIME type to register
  `extensions` - One or more file name extensions (without leading dot) to register for the MIME type.

### registerMimeType

void registerMimeType(InputStream mimeTabStream) throws IOException
Register MIME types stored in the given input stream formatted as a regular MIME type file format: One entry per line. Each entry consists of two or more whitespace delimited fields where the first field is the MIME type and the rest of the fields are the file extensions. The first of the extensions is considered the default extension. Empty lines and lines starting with a hash sign (`#`) are ignored.

The stream is assumed to contain string data encoded with "ISO-8859-1".

This method reads the stream until an `IOException` occurs or until it has been fully read. The stream is not closed, though, by this method.

Parameters:
  `mimeTabStream` - The stream to read the MIME type mappings from
Throws:
  `IOException` - If an error occurs reading from the stream

### getMimeMap

Map<String,String> getMimeMap()

### getExtensionMap

Map<String,String> getExtensionMap()