Java UTF8 codeListToString(List utf8CodeList)

Here you can find the source of codeListToString(List utf8CodeList)

Description

convert utf8 code list to string

License

Apache License

Parameter

Parameter Description
utf8CodeList a parameter

Declaration

public static String codeListToString(List<Integer> utf8CodeList) 

Method Source Code


//package com.java2s;
/*//from   w w  w  .ja  va2s  .  c  om
 * 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 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);
            }
        }
    }

    /**
     * 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);
    }
}

Related

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