zoukankan      html  css  js  c++  java
  • CharsetUtils.java

    /*
     * Copyright (c) 2013. 
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.example.kjdemo.utils;
    
    import org.apache.http.protocol.HTTP;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by wyouflf on 13-8-30.
     */
    public class CharsetUtils {
    
        private CharsetUtils() {
        }
    
        public static String toCharset(final String str, final String charset, int judgeCharsetLength) {
            try {
                String oldCharset = getEncoding(str, judgeCharsetLength);
                return new String(str.getBytes(oldCharset), charset);
            } catch (Throwable ex) {
                LogUtils.w(ex);
                return str;
            }
        }
    
        public static String getEncoding(final String str, int judgeCharsetLength) {
            String encode = CharsetUtils.DEFAULT_ENCODING_CHARSET;
            for (String charset : SUPPORT_CHARSET) {
                if (isCharset(str, charset, judgeCharsetLength)) {
                    encode = charset;
                    break;
                }
            }
            return encode;
        }
    
        public static boolean isCharset(final String str, final String charset, int judgeCharsetLength) {
            try {
                String temp = str.length() > judgeCharsetLength ? str.substring(0, judgeCharsetLength) : str;
                return temp.equals(new String(temp.getBytes(charset), charset));
            } catch (Throwable e) {
                return false;
            }
        }
    
        public static final String DEFAULT_ENCODING_CHARSET = HTTP.DEFAULT_CONTENT_CHARSET;
    
        public static final List<String> SUPPORT_CHARSET = new ArrayList<String>();
    
        static {
            SUPPORT_CHARSET.add("ISO-8859-1");
    
            SUPPORT_CHARSET.add("GB2312");
            SUPPORT_CHARSET.add("GBK");
            SUPPORT_CHARSET.add("GB18030");
    
            SUPPORT_CHARSET.add("US-ASCII");
            SUPPORT_CHARSET.add("ASCII");
    
            SUPPORT_CHARSET.add("ISO-2022-KR");
    
            SUPPORT_CHARSET.add("ISO-8859-2");
    
            SUPPORT_CHARSET.add("ISO-2022-JP");
            SUPPORT_CHARSET.add("ISO-2022-JP-2");
    
            SUPPORT_CHARSET.add("UTF-8");
        }
    }
  • 相关阅读:
    5个JavaScript的字符串处理库
    一款在线还款记账产品诞生实战
    企业级B端产品:如何建立一致性设计规范和度量标准?
    15个产品与交互必懂的技术用语!
    利润的来源:会员权益的前置和后置
    为什么设计工作总是感觉很被动?
    中国服装业首次负增长背后,是设计垄断权引领需求的逻辑失效
    怎么样写出卖货文案?试试加一点“想象力”
    SublimeText 3200 package Control 安装
    FlappyBirdWeb素材资源
  • 原文地址:https://www.cnblogs.com/royi123/p/5653843.html
Copyright © 2011-2022 走看看