Repository navigation
Expand file tree
/
Copy pathPubnubUtilCore.java
More file actions
231 lines (192 loc) · 6.16 KB
/
Copy pathPubnubUtilCore.java
File metadata and controls
231 lines (192 loc) · 6.16 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.pubnub.api;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
/**
*
* @author PubnubCore
*/
class PubnubUtilCore {
/**
* Takes source and delimiter string as inputs and returns splitted string
* in form of tokens in String array
*
* @param source
* , input String
* @param delimiter
* , delimiter to split on
* @return String[] , tokens in and array
*/
public static String[] splitString(String source, String delimiter) {
int delimiterCount = 0;
int index = 0;
String tmpStr = source;
String[] splittedList;
while ((index = tmpStr.indexOf(delimiter)) != -1) {
tmpStr = tmpStr.substring(index + delimiter.length());
delimiterCount++;
}
splittedList = new String[delimiterCount + 1];
int counter = 0;
tmpStr = source;
do {
int nextIndex = tmpStr.indexOf(delimiter, index + 1);
if (nextIndex != -1) {
splittedList[counter++] = tmpStr.substring(
index + delimiter.length(), nextIndex);
tmpStr = tmpStr.substring(nextIndex);
} else {
splittedList[counter++] = tmpStr.substring(index
+ delimiter.length());
tmpStr = tmpStr.substring(index + 1);
}
} while ((index = tmpStr.indexOf(delimiter)) != -1);
return splittedList;
}
/**
* Takes String[] of tokens, and String delimiter as input and returns
* joined String
*
* @param sourceArray
* , input tokens in String array
* @param delimiter
* , delimiter to join on
* @return String , string of tokens joined by delimiter
*/
public static String joinString(String[] sourceArray, String delimiter) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < sourceArray.length - 1; i++) {
sb.append(sourceArray[i]).append(delimiter);
}
sb.append(sourceArray[sourceArray.length - 1]);
return sb.toString();
}
/**
* Returns string keys in a hashtable as array of string
*
* @param ht
* , Hashtable
* @return , string array with hash keys string
*/
public static synchronized String[] hashtableKeysToArray(Hashtable ht) {
Vector v = new Vector();
String[] sa = null;
int count = 0;
Enumeration e = ht.keys();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
v.addElement(s);
count++;
}
sa = new String[count];
v.copyInto(sa);
return sa;
}
/**
* Returns string keys in a hashtable as delimited string
*
* @param ht
* , Hashtable
* @param delimiter
* , String
* @param exclude
* , exclude channel if present as substring
* @return , string array with hash keys string
*/
public static synchronized String hashTableKeysToDelimitedString(
Hashtable ht, String delimiter, String exclude) {
StringBuffer sb = new StringBuffer();
boolean first = true;
Enumeration e = ht.keys();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
if (exclude != null) {
if (s.indexOf(exclude) != -1) {
continue;
}
}
if (first) {
sb.append(s);
first = false;
} else {
sb.append(delimiter).append(s);
}
}
return sb.toString();
}
/**
* Returns string keys in a hashtable as delimited string
*
* @param ht
* , Hashtable
* @param delimiter
* , String
* @return , string array with hash keys string
*/
public static String hashTableKeysToDelimitedString(
Hashtable ht, String delimiter) {
return hashTableKeysToDelimitedString(ht, delimiter, null);
}
static Hashtable hashtableClone(Hashtable ht) {
if (ht == null)
return null;
Hashtable htresp = new Hashtable();
Enumeration e = ht.keys();
while (e.hasMoreElements()) {
Object element = e.nextElement();
htresp.put(element, ht.get(element));
}
return htresp;
}
static Hashtable hashtableClone(Hashtable ht1, Hashtable ht2) {
if (ht1 == null && ht2 == null)
return null;
Hashtable htresp = new Hashtable();
if (ht1 != null) {
Enumeration e = ht1.keys();
while (e.hasMoreElements()) {
Object element = e.nextElement();
htresp.put(element, ht1.get(element));
}
}
if (ht2 != null) {
Enumeration e = ht2.keys();
while (e.hasMoreElements()) {
Object element = e.nextElement();
htresp.put(element, ht2.get(element));
}
}
return htresp;
}
static Hashtable hashtableMerge(Hashtable dst, Hashtable src) {
if (dst == null)
return src;
if (src == null)
return dst;
Enumeration e = src.keys();
while (e.hasMoreElements()) {
Object element = e.nextElement();
dst.put(element, src.get(element));
}
return dst;
}
/**
* Parse Json, change json string to string
*
* @param obj
* JSON data in string format
*
* @return JSONArray or JSONObject or String
*/
static Object parseJSON(Object obj) {
if (obj instanceof String) {
if (((String) obj).endsWith("\"") && ((String) obj).startsWith("\""))
obj = ((String) obj).substring(1, ((String) obj).length() -1);
}
return obj;
}
}