Repository navigation
Expand file tree
/
Copy pathPubnubUtilCore.java
More file actions
141 lines (114 loc) · 3.65 KB
/
Copy pathPubnubUtilCore.java
File metadata and controls
141 lines (114 loc) · 3.65 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
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
*/
public 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
* @return , string array with hash keys string
*/
public static synchronized String hashTableKeysToDelimitedString(
Hashtable ht, String delimiter) {
StringBuffer sb = new StringBuffer();
boolean first = true;
Enumeration e = ht.keys();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
if (first) {
sb.append(s);
first = false;
} else {
sb.append(delimiter).append(s);
}
}
return sb.toString();
}
}