Repository navigation
Expand file tree
/
Copy pathPubnubUtil.java
More file actions
147 lines (131 loc) · 4.4 KB
/
Copy pathPubnubUtil.java
File metadata and controls
147 lines (131 loc) · 4.4 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.pubnub.api;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.json.me.*;
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 urlEncode(String sUrl) {
try {
return replace(encode(sUrl, "UTF-8"),"+", "%20");
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String encode(String s, String enc)
throws UnsupportedEncodingException {
boolean needToChange = false;
boolean wroteUnencodedChar = false;
int maxBytesPerChar = 10;
StringBuffer out = new StringBuffer(s.length());
ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
OutputStreamWriter writer = new OutputStreamWriter(buf, enc);
for (int i = 0; i < s.length(); i++) {
int c = (int) s.charAt(i);
if (dontNeedEncoding(c)) {
if (c == ' ') {
c = '+';
needToChange = true;
}
out.append((char)c);
wroteUnencodedChar = true;
} else {
try {
if (wroteUnencodedChar) {
writer = new OutputStreamWriter(buf, enc);
wroteUnencodedChar = false;
}
writer.write(c);
if (c >= 0xD800 && c <= 0xDBFF) {
if ( (i+1) < s.length()) {
int d = (int) s.charAt(i+1);
if (d >= 0xDC00 && d <= 0xDFFF) {
writer.write(d);
i++;
}
}
}
writer.flush();
} catch (IOException e) {
buf.reset();
continue;
}
byte[] ba = buf.toByteArray();
for (int j = 0; j < ba.length; j++) {
out.append('%');
char ch = CCharacter.forDigit((ba[j] >> 4) & 0xF, 16);
out.append(ch);
ch = CCharacter.forDigit(ba[j] & 0xF, 16);
out.append(ch);
}
buf.reset();
needToChange = true;
}
}
return (needToChange? out.toString() : s);
}
static class CCharacter {
public static char forDigit(int digit, int radix) {
if ((digit >= radix) || (digit < 0)) {
return '\0';
}
if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX)) {
return '\0';
}
if (digit < 10) {
return (char)('0' + digit);
}
return (char)('a' - 10 + digit);
}
}
public static boolean dontNeedEncoding(int ch) {
int len = _dontNeedEncoding.length();
boolean en = false;
for (int i =0; i< len; i++) {
if (_dontNeedEncoding.charAt(i) == ch) {
en = true;
break;
}
}
return en;
}
/**
* 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) {
}
return str;
}
private static String _dontNeedEncoding = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -_.*";
}