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 java.util.Locale; 20 import libcore.net.MimeUtils; 21 22 /** 23 * Implements {@link FileNameMap} in terms of {@link libcore.net.MimeUtils}. 24 */ 25 class DefaultFileNameMap implements FileNameMap { getContentTypeFor(String filename)26 public String getContentTypeFor(String filename) { 27 if (filename.endsWith("/")) { 28 // a directory, return html 29 return MimeUtils.guessMimeTypeFromExtension("html"); 30 } 31 int lastCharInExtension = filename.lastIndexOf('#'); 32 if (lastCharInExtension < 0) { 33 lastCharInExtension = filename.length(); 34 } 35 int firstCharInExtension = filename.lastIndexOf('.') + 1; 36 String ext = ""; 37 if (firstCharInExtension > filename.lastIndexOf('/')) { 38 ext = filename.substring(firstCharInExtension, lastCharInExtension); 39 } 40 return MimeUtils.guessMimeTypeFromExtension(ext.toLowerCase(Locale.US)); 41 } 42 } 43