Java UTF8 utf8urldecode(String text)

Here you can find the source of utf8urldecode(String text)

Description

utfurldecode

License

Open Source License

Declaration

public static String utf8urldecode(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class Main {

    public static String utf8urldecode(String text) {
        StringBuilder result = new StringBuilder();
        int p;//from   w w w .j  ava2  s  .co m
        if (text != null && text.length() > 0) {
            text = text.toLowerCase();
            p = text.indexOf("%e");
            if (p == -1) {
                return text;
            }
            while (p != -1) {
                result.append(text, 0, p);
                text = text.substring(p);
                if (Objects.equals(text, "") || text.length() < 9) {
                    return result.toString();
                }
                result.append(codetoword(text.substring(0, 9)));
                text = text.substring(9);
                p = text.indexOf("%e");
            }
        }
        return result + text;
    }

    private static String codetoword(String text) {
        String result;
        if (utf8codecheck(text)) {
            byte[] code = new byte[3];
            code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
            code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
            code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
            result = new String(code, StandardCharsets.UTF_8);
        } else {
            result = text;
        }
        return result;
    }

    private static boolean utf8codecheck(String text) {
        String sign = "";
        String prefix = "%e";
        if (text.startsWith(prefix)) {
            for (int p = 0; p != -1;) {
                p = text.indexOf("%", p);
                if (p != -1) {
                    p++;
                }
                sign += p;
            }
        }
        return "147-1".equals(sign);
    }
}

Related

  1. utf8StringSizeInBytes(String s)
  2. utf8StringToByteArray(String dataString)
  3. utf8StringToBytes(String string)
  4. utf8ToString(byte[] data)
  5. utf8URLDecode(String input)
  6. Utf8URLencode(String text)