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");
        }
    }
  • 相关阅读:
    进击的实时数仓:Flink 在 OPPO 实时计算平台的研发与应用实践
    vue中将时间戳转换为YYYY-MM-dd hh:mm格式时间的组件
    vue 将时间戳转换成日期格式 (一)
    element-ui DatePicker 日期选择器 让结束日期大于开始日期
    css 边框上如何写入文字?
    2019最新Web前端经典面试试题(含答案)
    Nginx 安装及配置
    前端基础面试题(JS部分)
    const与指针
    linux配置-------redhat虚拟机的中文输入法配置
  • 原文地址:https://www.cnblogs.com/royi123/p/5653843.html
Copyright © 2011-2022 走看看