• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package java.net;
18 
19 import libcore.content.type.MimeMap;
20 
21 /**
22  * Implements {@link FileNameMap} in terms of {@link MimeMap}.
23  */
24 class DefaultFileNameMap implements FileNameMap {
25 
getContentTypeFor(String filename)26     public String getContentTypeFor(String filename) {
27         int fragmentIndex = filename.indexOf('#');
28         if (fragmentIndex >= 0) {
29             filename = filename.substring(0, fragmentIndex);
30         }
31         if (filename.endsWith("/")) { // a directory
32             return "text/html";
33         }
34 
35         int slashIndex = filename.lastIndexOf('/');
36         if (slashIndex >= 0) {
37             filename = filename.substring(slashIndex);
38         }
39 
40         MimeMap mimeMap = MimeMap.getDefault();
41         int dotIndex = -1;
42         do {
43             String ext = filename.substring(dotIndex + 1);
44             String result = mimeMap.guessMimeTypeFromExtension(ext);
45             if ((result != null) &&
46                     // Compat behavior: If there's a '/', then extension must not be the
47                     // whole string. http://b/144977800
48                     (slashIndex < 0 || dotIndex >= 0)) {
49                 return result;
50             }
51             dotIndex = filename.indexOf('.', dotIndex + 1);
52         } while (dotIndex >= 0);
53         return null;
54     }
55 }
56