Java Code Examples for java.nio.charset.StandardCharsets.UTF_16

The following are Jave code examples for showing how to use UTF_16 of the java.nio.charset.StandardCharsets class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: MetadataEditor   File: AbstractString.java   Source Code and License Vote up 5 votes
/**
 * If they have specified UTF-16 then decoder works out by looking at BOM
 * but if missing we have to make an educated guess otherwise just use
 * specified decoder
 *
 * @param inBuffer
 * @return
 */
protected CharsetDecoder getCorrectDecoder(ByteBuffer inBuffer)
{
    CharsetDecoder decoder=null;
    if(inBuffer.remaining()<=2)
    {
        decoder = getTextEncodingCharSet().newDecoder();
        decoder.reset();
        return decoder;
    }

    if(getTextEncodingCharSet()== StandardCharsets.UTF_16)
    {
        if(inBuffer.getChar(0)==0xfffe || inBuffer.getChar(0)==0xfeff)
        {
            //Get the Specified Decoder
            decoder = getTextEncodingCharSet().newDecoder();
            decoder.reset();
        }
        else
        {
            if(inBuffer.get(0)==0)
            {
                decoder = StandardCharsets.UTF_16BE.newDecoder();
                decoder.reset();
            }
            else
            {
                decoder = StandardCharsets.UTF_16LE.newDecoder();
                decoder.reset();
            }
        }
    }
    else
    {
        decoder = getTextEncodingCharSet().newDecoder();
        decoder.reset();
    }
    return decoder;
}