Repository navigation
Expand file tree
/
Copy pathPubnubUtil.java
More file actions
95 lines (86 loc) · 2.41 KB
/
Copy pathPubnubUtil.java
File metadata and controls
95 lines (86 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.pubnub.api;
import com.codename1.io.Util;
import com.codename1.util.StringUtil;
import org.json.*;
/**
* PubnubUtil class provides utility methods like urlEncode etc
*
* @author Pubnub
*
*/
public class PubnubUtil extends PubnubUtilCore {
private static String replace(String str, String pattern, String replace) {
int s = 0;
int e = 0;
StringBuffer result = new StringBuffer();
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(s, e));
result.append(replace);
s = e + pattern.length();
}
result.append(str.substring(s));
return result.toString();
}
/**
* Returns encoded String
*
* @param sUrl
* , input string
* @return , encoded string
*/
public static String pamEncode(String sUrl) {
/* !'()*~ */
String encoded = urlEncode(sUrl);
if (encoded != null) {
encoded = replace(encoded, "*", "%2A");
encoded = replace(encoded, "!", "%21");
encoded = replace(encoded, "'", "%27");
encoded = replace(encoded, "(", "%28");
encoded = replace(encoded, ")", "%29");
encoded = replace(encoded, "[", "%5B");
encoded = replace(encoded, "]", "%5D");
encoded = replace(encoded, "~", "%7E");
}
return encoded;
}
/**
* Returns encoded String
*
* @param sUrl
* , input string
* @return , encoded string
*/
public static String urlEncode(String sUrl) {
return Util.encodeUrl(sUrl);
}
public static String stringReplaceAll(String s, String a, String b) {
return StringUtil.replaceAll(s, a, b);
}
/**
* Convert input String to JSONObject, JSONArray, or String
*
* @param str
* JSON data in string format
*
* @return JSONArray or JSONObject or String
*/
static Object stringToJSON(String str) {
try {
return new JSONArray(str);
} catch (JSONException e) {
}
try {
return new JSONObject(str);
} catch (JSONException ex) {
}
try {
return Integer.parseInt(str);
} catch (Exception ex) {
}
try {
return Double.parseDouble(str);
} catch (Exception ex) {
}
return str;
}
}