All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.geektool.core.net.URLDecoder Maven / Gradle / Ivy

package cn.geektool.core.net;

import cn.geektool.core.util.CharUtil;
import cn.geektool.core.util.StrUtil;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;

/**
 * URL解码,数据内容的类型是 application/x-www-form-urlencoded。
 *
 * 
 * 1. 将%20转换为空格 ;
 * 2. 将"%xy"转换为文本形式,xy是两位16进制的数值;
 * 3. 跳过不符合规范的%形式,直接输出
 * 
* * @author jd */ public class URLDecoder implements Serializable { private static final long serialVersionUID = 1L; private static final byte ESCAPE_CHAR = '%'; /** * 解码 * * @param str 包含URL编码后的字符串 * @param charset 编码 * @return 解码后的字符串 */ public static String decode(String str, Charset charset) { return StrUtil.str(decode(StrUtil.bytes(str, charset)), charset); } /** * 解码 * * @param bytes url编码的bytes * @return 解码后的bytes */ public static byte[] decode(byte[] bytes) { if (bytes == null) { return null; } final ByteArrayOutputStream buffer = new ByteArrayOutputStream(bytes.length); int b; for (int i = 0; i < bytes.length; i++) { b = bytes[i]; if (b == '+') { buffer.write(CharUtil.SPACE); } else if (b == ESCAPE_CHAR) { if (i + 1 < bytes.length) { final int u = CharUtil.digit16(bytes[i + 1]); if (u >= 0 && i + 2 < bytes.length) { final int l = CharUtil.digit16(bytes[i + 2]); if (l >= 0) { buffer.write((char) ((u << 4) + l)); i += 2; continue; } } } // 跳过不符合规范的%形式 buffer.write(b); } else { buffer.write(b); } } return buffer.toByteArray(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy