get Encoded String - Java java.lang

Java examples for java.lang:String Unicode

Description

get Encoded String

Demo Code

/*//from  w w w . j  a va 2  s  . com
 * Copyright (C) 2013 Lee Hong (http://blog.csdn.net/leehong2005)
 *
 * 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.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(getEncodedString(bytes));
    }

    // CHECKSTYLE:OFF
    public static String getEncodedString(final byte[] bytes) {
        if (bytes == null) {
            return null;
        }

        /** ? */
        final String gbk = "GBK";
        final String utf8 = "UTF-8";
        String result = null;

        try {
            // NOTE: GBK is not guaranteed on all platforms
            String gbkText = new String(bytes, gbk);
            String utf8Text = new String(bytes, utf8);

            byte[] gbkBytes = gbkText.getBytes(gbk);
            /** GBK? */
            int invalidGBKcount = 0;
            for (int c, i = 0; i < gbkBytes.length; i++) {
                c = gbkBytes[i] & 0xFF;
                if (c > 0xA0) {
                    if (c > 0xD7) {
                        invalidGBKcount++;
                    }
                    i++;
                } else {
                    if (c < 0x20 || c == 0x3f) {
                        invalidGBKcount++;
                    }
                }
            }

            byte[] utf8Bytes = utf8Text.getBytes(gbk);
            /** UTF-8? */
            int invalidUTFcount = 0;
            for (int c, i = 0; i < utf8Bytes.length; i++) {
                c = utf8Bytes[i] & 0xFF;
                if (c > 0xA0) {
                    if (c > 0xD7) {
                        invalidUTFcount++;
                    }
                    i++;
                } else {
                    if (c < 0x20 || c == 0x3f) {
                        invalidUTFcount++;
                    }
                }
            }

            /**  */
            result = invalidGBKcount >= invalidUTFcount ? utf8Text
                    : gbkText;
            result = result.trim();
            result = result.replace("\r", "");
        } catch (UnsupportedEncodingException e) {
            // system does not support GBK encoding
            e.printStackTrace();
        } catch (Exception e) {
            // keep result empty
            e.printStackTrace();
        }

        /** ? */
        return result == null ? new String(bytes) : result;
    }
}

Related Tutorials