publicstatic String getCharsetFromContentType(Buffer value) { if (value instanceof CachedBuffer) { switch(((CachedBuffer)value).getOrdinal()) { case TEXT_HTML_8859_1_ORDINAL: case TEXT_PLAIN_8859_1_ORDINAL: case TEXT_XML_8859_1_ORDINAL: return StringUtil.__ISO_8859_1;
case TEXT_HTML_UTF_8_ORDINAL: case TEXT_PLAIN_UTF_8_ORDINAL: case TEXT_XML_UTF_8_ORDINAL: case TEXT_JSON_UTF_8_ORDINAL: return StringUtil.__UTF8; } } //下面是用来解析charset的 int i=value.getIndex(); finalint end=value.putIndex(); int state=0; int start=0; boolean quote=false; for (;i<end;i++) { finalbyteb= value.peek(i);
if (quote && state!=10) { if ('"'==b) quote=false; continue; }
switch(state) { case0: if ('"'==b) { quote=true; break; } if (';'==b) state=1; break;
case1: if ('c'==b) state=2; elseif (' '!=b) state=0; break; case2: if ('h'==b) state=3; else state=0;break; case3: if ('a'==b) state=4; else state=0;break; case4: if ('r'==b) state=5; else state=0;break; case5: if ('s'==b) state=6; else state=0;break; case6: if ('e'==b) state=7; else state=0;break; case7: if ('t'==b) state=8; else state=0;break;
case8: if ('='==b) state=9; elseif (' '!=b) state=0; break;
case9: if (' '==b) break; if ('"'==b) { quote=true; start=i+1; state=10; break; } start=i; state=10; break;