forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPubnubUtil.java
More file actions
85 lines (77 loc) · 2.08 KB
/
Copy pathPubnubUtil.java
File metadata and controls
85 lines (77 loc) · 2.08 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
package com.pubnub.api;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* PubnubUtil class provides utility methods like urlEncode etc
*
* @author Pubnub
*
*/
public class PubnubUtil extends PubnubUtilCore {
public static String stringReplaceAll(String s, String a, String b) {
return s.replaceAll(a, b);
}
/**
* Returns encoded String
*
* @param sUrl
* , input string
* @return , encoded string
*/
public static String pamEncode(String sUrl) {
/* !'()*~ */
String encoded = urlEncode(sUrl);
if (encoded != null) {
encoded = encoded.replace("*", "%2A").replace("!", "%21").replace("'", "%27").replace("(", "%28")
.replace(")", "%29").replace("[", "%5B").replace("]", "%5D").replace("~", "%7E");
}
return encoded;
}
/**
* Returns encoded String
*
* @param sUrl
* , input string
* @return , encoded string
*/
public static String urlEncode(String sUrl) {
try {
return URLEncoder.encode(sUrl, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
return null;
}
}
/**
* 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;
}
}