1 17 18 package org.apache.tomcat.util.http; 19 20 21 30 public class ContentType { 31 32 public static String getCharsetFromContentType(String type) { 35 if (type == null) { 36 return null; 37 } 38 int semi = type.indexOf(";"); 39 if (semi == -1) { 40 return null; 41 } 42 int charsetLocation = type.indexOf("charset=", semi); 43 if (charsetLocation == -1) { 44 return null; 45 } 46 String afterCharset = type.substring(charsetLocation + 8); 47 afterCharset = afterCharset.replace('"', ' '); 51 String encoding = afterCharset.trim(); 52 return encoding; 53 } 54 55 56 64 public static boolean hasCharset(String type) { 65 66 boolean hasCharset = false; 67 68 int len = type.length(); 69 int index = type.indexOf(';'); 70 while (index != -1) { 71 index++; 72 while (index < len && Character.isSpace(type.charAt(index))) { 73 index++; 74 } 75 if (index+8 < len 76 && type.charAt(index) == 'c' 77 && type.charAt(index+1) == 'h' 78 && type.charAt(index+2) == 'a' 79 && type.charAt(index+3) == 'r' 80 && type.charAt(index+4) == 's' 81 && type.charAt(index+5) == 'e' 82 && type.charAt(index+6) == 't' 83 && type.charAt(index+7) == '=') { 84 hasCharset = true; 85 break; 86 } 87 index = type.indexOf(';', index); 88 } 89 90 return hasCharset; 91 } 92 93 } 94 | Popular Tags |