最近需要将一些python代码转成java,遇到url编码
urllib.quote(str,safe='/')
但java中URLEncoder.encode(arg,Constant.UTF_8)会将'/'转成%2F
网上查了一下 java没见到类似的safe方式,只好自己实现一个类
<span >public <span >class<span > UrlSafeEncoder {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> BitSet dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">final</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> caseDiff = ('a' - 'A'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> String dfltEnc<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> The <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a> of ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters that are not encoded has been * deter<a href="https://www.jb51.cc/tag/mine/" target="_blank" >mine</a>d as f<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>lows: * * RFC 2396 states: * ----- * Data ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters that are allowed in a URI but do not have a * reserved purpose are called unreserved. These include upper * and lower case letters,decimal digits,and a <a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>mited set of * punctuation marks and symb<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>s. * * unreserved = <a href="https://m.jb51.cc/tag/Alpha/" target="_blank" >Alpha</a>num | mark * * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" * * Unreserved ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters can be escaped without changing the * semantics of the URI,but this sho<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d not be done unless the * URI is being used in a context that does not allow the * u<a href="https://www.jb51.cc/tag/nes/" target="_blank" >nes</a>caped ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter to appear. * ----- * * It appears that both <a href="https://www.jb51.cc/tag/netscape/" target="_blank" >netscape</a> and Internet Explorer escape * all special ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters from this <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a> with the exception * of "-","_",".","*". While it is not clear why t<a href="https://www.jb51.cc/tag/hey/" target="_blank" >hey</a> are * esca<a href="https://m.jb51.cc/tag/Ping/" target="_blank" >Ping</a> the other ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters,perhaps it is safest to * assume that there might be contexts in which the others * are unsafe if not escaped. Therefore,we will use the same * <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>. It is also noteworthy that this is consistent with * O'Reilly's "<a href="https://m.jb51.cc/tag/HTML/" target="_blank" >HTML</a>: The Definitive Gu<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e" (page 164). * * As a last note,Intenet Explorer does not encode the "@" * ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter which is clearly not unreserved according to the * RFC. We are being consistent with the RFC in this matter,* as is <a href="https://www.jb51.cc/tag/netscape/" target="_blank" >netscape</a>. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> BitSet(256<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> i; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (i = 'a'; i <= 'z'; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(i); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (i = 'A'; i <= 'Z'; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(i); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (i = '0'; i <= '9'; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(i); } dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>' '); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> a space to a + is done * in the encode() method </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'-'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'_'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'.'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'*'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); dfltEnc<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> AccessContr<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>ler.doPrivileged( </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> GetPropertyAction("<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>.en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) );}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/**</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> * You can't call the constructor. </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> UrlSafeEncoder() { }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/**</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> * Translates a string into {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@code</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> app<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cation/x-www-form-urlencoded} * format using a specific en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> scheme. This method uses the * suppl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>d en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> scheme to obtain the bytes for unsafe * ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters. * <p> * <em><strong>Note:</strong> The <a href= * "</span><span >http://www.w3.org/TR/html40/appendix/notes.html</span><span >#non-ascii-chars"> * World Wide Web Consortium Recommendation</a> states that * UTF-8 sho<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d be used. Not doing so may introduce * incompatibi<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>t<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>s.</em> * * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> s {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@code</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> String} to be translated. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> enc The <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> of a supported * <a <a href="https://m.jb51.cc/tag/href/" target="_blank" >href</a>="../lang/package-summary.<a href="https://m.jb51.cc/tag/HTML/" target="_blank" >HTML</a>#charenc">ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter * en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a></a>. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> the translated {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@code</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> String}. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@exception</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception * If the <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>d en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> is not supported * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@see</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> URLDecoder#decode(<a href="https://www.jb51.cc/tag/javalang/" target="_blank" >java.lang</a>.String,<a href="https://www.jb51.cc/tag/javalang/" target="_blank" >java.lang</a>.String) * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">@since</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> 1.4 </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> String encode(String s,String enc,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> safe) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception { dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(safe); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">bo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>ean</span> need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; StringBuffer out </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> StringBuffer(s.length()); Charset charset; Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (enc == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>lPointerException("charset<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { charset </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Charset.for<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>(enc); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Ille<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a>Charset<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>Exception e) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception(enc); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (UnsupportedCharsetException e) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception(enc); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = 0; i <<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> s.length();) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> c = (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) s.ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>t(i); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("Examining ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter: " + c);</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.get(c)) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (c == ' '<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { c </span>= '+'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("Storing: " + c);</span> out.append((<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)c); i</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> convert to external en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> before hex conversion</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">do</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.write(c); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> * If this ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter represents the start of a Unicode * surrogate pair,then pass in two ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters. It's not * clear what sho<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d be done if a bytes reserved in the * surrogate pa<a href="https://www.jb51.cc/tag/irs/" target="_blank" >irs</a> range occurs outs<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e of a le<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a> * surrogate pair. For <a href="https://www.jb51.cc/tag/Now/" target="_blank" >Now</a>,just treat it as if it were * any other ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter. </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (c >= 0xD800 && c <= 0xDBFF<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(Integer.toHexString(c) + " is high surrogate"); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> ( (i+1) <<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> s.length()) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> d = (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>) s.ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>t(i+1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("\tExamining " + Integer.toHexString(d)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (d >= 0xDC00 && d <= 0xDFFF<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("\t" + Integer.toHexString(d) + " is low surrogate"); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.write(d); i</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } } } i</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">while</span> (i < s.length() && !dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.get((c = (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) s.ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>t(i)))); ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.flush(); String str </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> String(ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a><a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rray()); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">byte</span>[] ba =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> str.getBytes(charset); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> j = 0; j < ba.length; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { out.append(</span>'%'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">char</span> ch = Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.forDigit((ba[j] >> 4) & 0xF,16<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> converting to use uppercase letter as part of </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> the hex value if ch is a letter.</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.isLetter(ch)) { ch </span>-=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> caseDiff; } out.append(ch); ch </span>= Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.forDigit(ba[j] & 0xF,16<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.isLetter(ch)) { ch </span>-=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> caseDiff; } out.append(ch); } ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.<a href="https://m.jb51.cc/tag/reset/" target="_blank" >reset</a>(); need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> (need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange?<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> out.toString() : s);}
}
验证下 基本ok
总结以上是内存溢出为你收集整理的java如何实现python的urllib.quote(str,safe='/')全部内容,希望文章能够帮你解决java如何实现python的urllib.quote(str,safe='/')所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)