Example #1
0
        public static StringValue decodeEncode(Env env,
                                               StringValue str,
                                               string inCharset,
                                               string outCharset,
                                               string replacement,
                                               bool isIgnoreErrors)
        {
            bool isStartUtf8 = false;
            bool isEndUtf8   = false;

            if (inCharset.equalsIgnoreCase("utf8") ||
                inCharset.equalsIgnoreCase("utf-8"))
            {
                isStartUtf8 = true;
            }

            if (outCharset.equalsIgnoreCase("utf8") ||
                outCharset.equalsIgnoreCase("utf-8"))
            {
                isEndUtf8 = true;
            }

            if (isStartUtf8 && isEndUtf8)
            {
                return(UnicodeUtility.utf8Clean(env, str, null, isIgnoreErrors));
            }

            // decode phase

            CharSequence unicodeStr;

            Decoder decoder;

            if (isStartUtf8)
            {
                decoder = new Utf8Decoder(inCharset);
            }
            else
            {
                decoder = new GenericDecoder(inCharset);
            }

            decoder.setIgnoreErrors(isIgnoreErrors);

            unicodeStr = decoder.decode(env, str);

            // encode phase

            Encoder encoder;

            if (isEndUtf8)
            {
                encoder = new Utf8Encoder();
            }
            else
            {
                encoder = Encoder.create(outCharset);
            }

            encoder.setIgnoreErrors(isIgnoreErrors);

            StringValue sb = env.createBinaryBuilder();

            return(encoder.encode(sb, unicodeStr));
        }