Java UTF8 codesToString(int[] utf8Codes)

Here you can find the source of codesToString(int[] utf8Codes)

Description

convert utf8 codes to string

License

Apache License

Parameter

Parameter Description
utf8Codes a parameter

Declaration

public static String codesToString(int[] utf8Codes) 

Method Source Code


//package com.java2s;
/*//from   ww  w.ja  va  2s .  c o m
 * Copyright (C) 2014-2015 Nagisa Sekiguchi
 *
 * 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.
 */

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public final static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    /**
     * convert utf8 codes to string
     *
     * @param utf8Codes
     * @return
     */
    public static String codesToString(int[] utf8Codes) {
        List<Integer> codeList = new ArrayList<>(utf8Codes.length);
        for (int code : utf8Codes) {
            codeList.add(code);
        }
        return codeListToString(codeList);
    }

    /**
     * convert utf8 code list to string
     *
     * @param utf8CodeList
     * @return
     */
    public static String codeListToString(List<Integer> utf8CodeList) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        for (int code : utf8CodeList) {
            codeToString(buffer, code);
        }
        return new String(buffer.toByteArray(), DEFAULT_CHARSET);
    }

    /**
     * convert utf8 code to string
     *
     * @param utf8Code
     * @return
     */
    public static String codeToString(int utf8Code) {
        return codesToString(new int[] { utf8Code });
    }

    /**
     * convert utf8 code to string and write buffer.
     *
     * @param buffer
     * @param utf8Code
     */
    private static void codeToString(ByteArrayOutputStream buffer, int utf8Code) {
        for (int i = 3; i > -1; i--) {
            int shift = i * 8;
            int mask = 0xff << shift;
            byte result = (byte) ((utf8Code & mask) >> shift);
            if (result != 0) {
                buffer.write(result);
            }
        }
    }
}

Related

  1. asUtf8ByteStream(final String string)
  2. asUTF8String(byte[] bytes)
  3. asUTFString(byte[] content)
  4. backup(String inputFile, String backupFile)
  5. codeListToString(List utf8CodeList)
  6. codeToString(int utf8Code)
  7. compareWithGold(String goldfile, String outputfile)
  8. convertISO8859_1_to_UTF_8(String s)
  9. ConvertUTF8(byte[] buffer)