11package com .pubnub .api ;
22
3- import java .io .ByteArrayOutputStream ;
4- import java .io .IOException ;
5- import java .io .OutputStreamWriter ;
6- import java .io .UnsupportedEncodingException ;
7- import java .net .URLEncoder ;
8-
93import org .json .JSONArray ;
104import org .json .JSONException ;
115import org .json .JSONObject ;
126
13- import org .apache .commons .lang3 .StringEscapeUtils ;
7+ import java .io .UnsupportedEncodingException ;
8+ import java .net .URLEncoder ;
9+ import java .util .Locale ;
1410
1511
1612/**
1713 * PubnubUtil class provides utility methods like urlEncode etc
18- *
14+ *
1915 * @author Pubnub
20- *
16+ *
2117 */
2218public class PubnubUtil extends PubnubUtilCore {
19+ static final char [] HEX_DIGITS = new char []{'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
2320
2421 public static String escapeJava (String a ) {
25- return StringEscapeUtils .escapeJava (a );
22+ // return StringEscapeUtils.escapeJava(a);
23+ StringBuilder result = new StringBuilder (a );
24+ int len = a .length ();
25+ for (int i =0 , j =0 ; i < len ; i ++, j ++) {
26+ final char in = a .charAt (i );
27+ final char escaped = escapeJavaChar (in );
28+ if (escaped != 0 ) {
29+ result .insert (j ++, '\\' );
30+ result .setCharAt (j , escaped );
31+ } else {
32+ int point = a .codePointAt (i );
33+ j = unicodeEscape (result , point , j );
34+ if (point > '\uffff' ) {
35+ // multi-char
36+ i ++;
37+ }
38+ }
39+ }
40+ return result .toString ();
41+ }
42+
43+ /**
44+ * @return 0 if the char need not be escaped,
45+ * else the character that should replace `in`
46+ * after being escaped with a backslash.
47+ */
48+ private static char escapeJavaChar (char in ) {
49+ if (in == '"' || in == '\\' ) {
50+ return in ;
51+ }
52+
53+ // JAVA_CTRL_CHARS_ESCAPE
54+ if (in == '\b' ) {
55+ return 'b' ;
56+ } else if (in == '\n' ) {
57+ return 'n' ;
58+ } else if (in == '\t' ) {
59+ return 't' ;
60+ } else if (in == '\f' ) {
61+ return 'f' ;
62+ } else if (in == '\r' ) {
63+ return 'r' ;
64+ }
65+
66+ return 0 ;
2667 }
2768
69+ /**
70+ * Given a char, and its location in a StringBuilder,
71+ * replace it with an escaped unicode if necessary
72+ * @return The new index into StringBuilder
73+ */
74+ private static int unicodeEscape (StringBuilder result , int in , int index ) {
75+ // utf8 within [32, 127] are fine
76+ if (in >= 32 && in <= 127 ) {
77+ return index ;
78+ }
79+
80+ if (in > '\uffff' ) {
81+ final char [] surrogatePair = Character .toChars (in );
82+ final String first = hex (surrogatePair [0 ]);
83+ final String second = hex (surrogatePair [1 ]);
84+
85+ // ensure we have capacity to replace 2 chars
86+ // (IE a surrogate pair) with \\u + first + \\u + second
87+ result .ensureCapacity (result .capacity () + 2
88+ + first .length () + second .length ());
89+ result .delete (index , index + 2 );
90+ result .insert (index ++, '\\' );
91+ result .insert (index ++, 'u' );
92+ result .insert (index , first );
93+ index += first .length ();
94+
95+ result .insert (index ++, '\\' );
96+ result .insert (index ++, 'u' );
97+ result .insert (index , second );
98+ index += second .length () - 1 ;
99+ } else {
100+ // ensure we have capacity to replace 1 char with 6
101+ result .ensureCapacity (result .capacity () + 5 );
102+ result .deleteCharAt (index );
103+ result .insert (index ++, '\\' );
104+ result .insert (index ++, 'u' );
105+ result .insert (index ++, HEX_DIGITS [in >> 12 & 15 ]);
106+ result .insert (index ++, HEX_DIGITS [in >> 8 & 15 ]);
107+ result .insert (index ++, HEX_DIGITS [in >> 4 & 15 ]);
108+ result .insert (index , HEX_DIGITS [in & 15 ]);
109+ }
110+
111+ return index ;
112+ }
113+
114+ private static String hex (int codepoint ) {
115+ return Integer .toHexString (codepoint ).toUpperCase (Locale .ENGLISH );
116+ }
117+
118+
28119 public static String stringEscapeSlashes (String s , String a , String b ) {
29- return s .replace (a , b );
120+ return s .replace (a , b );
30121 }
31122 public static String stringReplaceAll (String s , String a , String b ) {
32- return s .replaceAll (a , b );
123+ return s .replaceAll (a , b );
33124 }
34125 /**
35126 * Returns encoded String
36- *
127+ *
37128 * @param sUrl
38129 * , input string
39130 * @return , encoded string
@@ -52,7 +143,7 @@ public static String pamEncode(String sUrl) {
52143
53144 /**
54145 * Returns encoded String
55- *
146+ *
56147 * @param sUrl
57148 * , input string
58149 * @return , encoded string
@@ -67,10 +158,10 @@ public static String urlEncode(String sUrl) {
67158
68159 /**
69160 * Convert input String to JSONObject, JSONArray, or String
70- *
161+ *
71162 * @param str
72163 * JSON data in string format
73- *
164+ *
74165 * @return JSONArray or JSONObject or String
75166 */
76167 static Object stringToJSON (String str ) {
@@ -93,3 +184,4 @@ static Object stringToJSON(String str) {
93184 return str ;
94185 }
95186}
187+
0 commit comments