public static String UTF8toUTF16()

in lightning-core/src/main/java/org/apache/directmemory/lightning/internal/util/UnicodeUtil.java [302:360]


    public static String UTF8toUTF16( Source source )
        throws IOException
    {
        int charLength = source.readInt();

        int offset = 0;
        final char[] out = new char[charLength];
        while ( offset < charLength )
        {
            int b = source.readByte() & 0xff;
            if ( b < 0xc0 )
            {
                assert b < 0x80;
                out[offset++] = (char) b;
            }
            else if ( b < 0xe0 )
            {
                out[offset++] = (char) ( ( ( b & 0x1f ) << 6 ) + ( source.readByte() & 0x3f ) );
            }
            else if ( b < 0xf0 )
            {
                out[offset++] =
                    (char) ( ( ( b & 0xf ) << 12 ) + ( ( source.readByte() & 0x3f ) << 6 ) + ( source.readByte() & 0x3f ) );
            }
            else
            {
                assert b < 0xf8 : "b = 0x" + Integer.toHexString( b );
                int ch =
                    ( ( b & 0x7 ) << 18 ) + ( ( source.readByte() & 0x3f ) << 12 )
                        + ( ( source.readByte() & 0x3f ) << 6 ) + ( source.readByte() & 0x3f );
                if ( ch < UNI_MAX_BMP )
                {
                    out[offset++] = (char) ch;
                }
                else
                {
                    int chHalf = ch - 0x0010000;
                    out[offset++] = (char) ( ( chHalf >> 10 ) + 0xD800 );
                    out[offset++] = (char) ( ( chHalf & HALF_MASK ) + 0xDC00 );
                }
            }
        }

        if ( STRING_PP_CONSTRUCTOR != null )
        {
            return new String( out );
        }
        else
        {
            try
            {
                return STRING_PP_CONSTRUCTOR.newInstance( 0, out.length, out );
            }
            catch ( Exception e )
            {
                return new String( out );
            }
        }
    }