Java InputStream Read by Charset convertFromUnicode(String input, String targetCharset)

Here you can find the source of convertFromUnicode(String input, String targetCharset)

Description

convert From Unicode

License

Apache License

Declaration

public static String convertFromUnicode(String input, String targetCharset) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {
    public static String convertFromUnicode(String input, String targetCharset) {
        // Create the encoder and decoder for inCharset
        Charset charset = Charset.forName(targetCharset);
        CharsetDecoder decoder = charset.newDecoder();

        CharBuffer cbuf = null;//from w ww  .  j  av  a 2 s  .  c  o m
        try {
            // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character
            // ByteBuffer and then to a
            // string.
            // The newstate ByteBuffer is ready to be read.
            cbuf = decoder.decode(ByteBuffer.wrap(input.getBytes()));
        } catch (CharacterCodingException e) {
            // LOG.logError( e.getMessage(), e );
        }
        return cbuf.toString();
    }
}

Related

  1. asString(final InputStream is, Charset charset)
  2. collectStream(InputStream stream, Charset charset)
  3. consume(InputStream stream, Charset encoding)
  4. convertEncoding(Charset output_charset, String input_string)
  5. convertStreamToString(final InputStream is, final Charset charset)
  6. convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)
  7. copyToString(InputStream in, Charset charset)
  8. createInput(String s, String charsetName)