whitespace fixups
[jSite2.git] / src / net / pterodactylus / util / io / MimeTypes.java
index 387372a..b7f9618 100644 (file)
@@ -27,12 +27,14 @@ import java.util.Map;
 
 /**
  * Handles MIME types and maps them to file extensions.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
 public class MimeTypes {
 
+       /** The default MIME type for unknown extensions. */
+       public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
+
        /** List of all MIME types. */
        private static final List<String> mimeTypes = new ArrayList<String>();
 
@@ -766,35 +768,52 @@ public class MimeTypes {
 
        /**
         * Returns a list of all known MIME types.
-        * 
+        *
         * @return All known MIME types
         */
-       public List<String> getAllMimeTypes() {
-               return mimeTypes;
+       public static List<String> getAllMimeTypes() {
+               return new ArrayList<String>(mimeTypes);
        }
 
        /**
         * Returns a list of MIME types that are registered for the given extension.
-        * 
+        *
         * @param extension
         *            The extension to get the MIME types for
         * @return A list of MIME types, or an empty list if there are no registered
         *         MIME types for the extension
         */
-       public List<String> getMimeTypes(String extension) {
+       public static List<String> getMimeTypes(String extension) {
                if (extensionMimeTypes.containsKey(extension)) {
                        return extensionMimeTypes.get(extension);
                }
                return Collections.emptyList();
        }
 
+       /**
+        * Returns a default MIME type for the given extension. If the extension
+        * does not match a MIME type the default MIME typ
+        * “application/octet-stream” is returned.
+        *
+        * @param extension
+        *            The extension to get the MIME type for
+        * @return The MIME type for the extension, or the default MIME type
+        *         “application/octet-stream”
+        */
+       public static String getMimeType(String extension) {
+               if (extensionMimeTypes.containsKey(extension)) {
+                       return extensionMimeTypes.get(extension).get(0);
+               }
+               return DEFAULT_CONTENT_TYPE;
+       }
+
        //
        // PRIVATE METHODS
        //
 
        /**
         * Adds a MIME type and optional extensions.
-        * 
+        *
         * @param mimeType
         *            The MIME type to add
         * @param extensions
@@ -802,7 +821,7 @@ public class MimeTypes {
         */
        private static void addMimeType(String mimeType, String... extensions) {
                mimeTypes.add(mimeType);
-               for (String extension: extensions) {
+               for (String extension : extensions) {
                        if (!mimeTypeExtensions.containsKey(mimeType)) {
                                mimeTypeExtensions.put(mimeType, new ArrayList<String>());
                        }