Skip to content

Commit a6873ce

Browse files
author
devendram
committed
Merge pull request #53 from bytehq/dhleong/RemoveCommonsLangDependency-develop
Replace commons lang method with own implementation
2 parents 49d3452 + 888d0a6 commit a6873ce

4 files changed

Lines changed: 170 additions & 28 deletions

File tree

‎android/pom.xml‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
<packaging>jar</packaging>
1010
<name>pubnub-android</name>
11-
<dependencies>
12-
<dependency>
13-
<groupId>org.apache.commons</groupId>
14-
<artifactId>commons-lang3</artifactId>
15-
<version>3.4</version>
16-
</dependency>
17-
</dependencies>
1811
<description>PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!</description>
1912
<url>https://github.com/pubnub/java</url>
2013
<licenses>

‎java/pom.xml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@
8383
</plugins>
8484
</build>
8585
<dependencies>
86-
<dependency>
87-
<groupId>org.apache.commons</groupId>
88-
<artifactId>commons-lang3</artifactId>
89-
<version>3.4</version>
90-
</dependency>
9186
<dependency>
9287
<groupId>org.json</groupId>
9388
<artifactId>json</artifactId>

‎java/src1/com/pubnub/api/PubnubUtil.java‎

Lines changed: 108 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,130 @@
11
package com.pubnub.api;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.IOException;
5-
import java.io.OutputStreamWriter;
6-
import java.io.UnsupportedEncodingException;
7-
import java.net.URLEncoder;
8-
93
import org.json.JSONArray;
104
import org.json.JSONException;
115
import org.json.JSONObject;
126

13-
import org.apache.commons.lang3.StringEscapeUtils;
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URLEncoder;
9+
import java.util.Locale;
1410

1511

1612
/**
1713
* PubnubUtil class provides utility methods like urlEncode etc
18-
*
14+
*
1915
* @author Pubnub
20-
*
16+
*
2117
*/
2218
public class PubnubUtil extends PubnubUtilCore {
19+
static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
2320

2421
public static String escapeJava(String a) {
25-
return StringEscapeUtils.escapeJava(a);
22+
// return StringEscapeUtils.escapeJava(a);
23+
StringBuilder result = new StringBuilder(a);
24+
int len = a.length();
25+
for (int i=0, j=0; i < len; i++, j++) {
26+
final char in = a.charAt(i);
27+
final char escaped = escapeJavaChar(in);
28+
if (escaped != 0) {
29+
result.insert(j++, '\\');
30+
result.setCharAt(j, escaped);
31+
} else {
32+
int point = a.codePointAt(i);
33+
j = unicodeEscape(result, point, j);
34+
if (point > '\uffff') {
35+
// multi-char
36+
i++;
37+
}
38+
}
39+
}
40+
return result.toString();
41+
}
42+
43+
/**
44+
* @return 0 if the char need not be escaped,
45+
* else the character that should replace `in`
46+
* after being escaped with a backslash.
47+
*/
48+
private static char escapeJavaChar(char in) {
49+
if (in == '"' || in == '\\') {
50+
return in;
51+
}
52+
53+
// JAVA_CTRL_CHARS_ESCAPE
54+
if (in == '\b') {
55+
return 'b';
56+
} else if (in == '\n') {
57+
return 'n';
58+
} else if (in == '\t') {
59+
return 't';
60+
} else if (in == '\f') {
61+
return 'f';
62+
} else if (in == '\r') {
63+
return 'r';
64+
}
65+
66+
return 0;
2667
}
2768

69+
/**
70+
* Given a char, and its location in a StringBuilder,
71+
* replace it with an escaped unicode if necessary
72+
* @return The new index into StringBuilder
73+
*/
74+
private static int unicodeEscape(StringBuilder result, int in, int index) {
75+
// utf8 within [32, 127] are fine
76+
if (in >= 32 && in <= 127) {
77+
return index;
78+
}
79+
80+
if (in > '\uffff') {
81+
final char[] surrogatePair = Character.toChars(in);
82+
final String first = hex(surrogatePair[0]);
83+
final String second = hex(surrogatePair[1]);
84+
85+
// ensure we have capacity to replace 2 chars
86+
// (IE a surrogate pair) with \\u + first + \\u + second
87+
result.ensureCapacity(result.capacity() + 2
88+
+ first.length() + second.length());
89+
result.delete(index, index + 2);
90+
result.insert(index++, '\\');
91+
result.insert(index++, 'u');
92+
result.insert(index, first);
93+
index += first.length();
94+
95+
result.insert(index++, '\\');
96+
result.insert(index++, 'u');
97+
result.insert(index, second);
98+
index += second.length() - 1;
99+
} else {
100+
// ensure we have capacity to replace 1 char with 6
101+
result.ensureCapacity(result.capacity() + 5);
102+
result.deleteCharAt(index);
103+
result.insert(index++, '\\');
104+
result.insert(index++, 'u');
105+
result.insert(index++, HEX_DIGITS[in >> 12 & 15]);
106+
result.insert(index++, HEX_DIGITS[in >> 8 & 15]);
107+
result.insert(index++, HEX_DIGITS[in >> 4 & 15]);
108+
result.insert(index, HEX_DIGITS[in & 15]);
109+
}
110+
111+
return index;
112+
}
113+
114+
private static String hex(int codepoint) {
115+
return Integer.toHexString(codepoint).toUpperCase(Locale.ENGLISH);
116+
}
117+
118+
28119
public static String stringEscapeSlashes(String s, String a, String b) {
29-
return s.replace(a, b);
120+
return s.replace(a, b);
30121
}
31122
public static String stringReplaceAll(String s, String a, String b) {
32-
return s.replaceAll(a, b);
123+
return s.replaceAll(a, b);
33124
}
34125
/**
35126
* Returns encoded String
36-
*
127+
*
37128
* @param sUrl
38129
* , input string
39130
* @return , encoded string
@@ -52,7 +143,7 @@ public static String pamEncode(String sUrl) {
52143

53144
/**
54145
* Returns encoded String
55-
*
146+
*
56147
* @param sUrl
57148
* , input string
58149
* @return , encoded string
@@ -67,10 +158,10 @@ public static String urlEncode(String sUrl) {
67158

68159
/**
69160
* Convert input String to JSONObject, JSONArray, or String
70-
*
161+
*
71162
* @param str
72163
* JSON data in string format
73-
*
164+
*
74165
* @return JSONArray or JSONObject or String
75166
*/
76167
static Object stringToJSON(String str) {
@@ -93,3 +184,4 @@ static Object stringToJSON(String str) {
93184
return str;
94185
}
95186
}
187+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.pubnub.api;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* @author dhleong
9+
*/
10+
public class PubnubUtilTest {
11+
@Test
12+
public void escapeSimple() {
13+
assertEquals("well \\\"hello\\\" there",
14+
PubnubUtil.escapeJava("well \"hello\" there"));
15+
16+
assertEquals("this\\\\that",
17+
PubnubUtil.escapeJava("this\\that"));
18+
}
19+
20+
@Test
21+
public void escapeSequences() {
22+
assertEquals("\\t",
23+
PubnubUtil.escapeJava("\t"));
24+
25+
assertEquals("\\t\\b\\f\\n\\r",
26+
PubnubUtil.escapeJava("\t\b\f\n\r"));
27+
28+
assertEquals("\\tt\\bb\\ff\\nn\\rr",
29+
PubnubUtil.escapeJava("\tt\bb\ff\nn\rr"));
30+
}
31+
32+
@Test
33+
public void escapeUnicode() {
34+
assertEquals("\\u001F", // 31, < 32
35+
PubnubUtil.escapeJava("\u001f"));
36+
assertEquals("\\u0080", // 128, > 128
37+
PubnubUtil.escapeJava("\u0080"));
38+
39+
// but these are fine:
40+
assertEquals("\u0020", // 32
41+
PubnubUtil.escapeJava("\u0020"));
42+
assertEquals("\u007f", // 127
43+
PubnubUtil.escapeJava("\u007f"));
44+
}
45+
46+
@Test
47+
public void escapeSurrogatePairs() {
48+
assertEquals("\\uD83D\\uDE00",
49+
PubnubUtil.escapeJava("\uD83D\uDE00"));
50+
}
51+
52+
@Test
53+
public void escapeUnicodeMulti() {
54+
assertEquals("\\u001F\\u0080\\u0081a\\u0082hi\\u0083",
55+
PubnubUtil.escapeJava("\u001F\u0080\u0081a\u0082hi\u0083"));
56+
57+
assertEquals("\\uD83D\\uDE00a\\u0082hi\\uD83D\\uDE00bye\\u0083",
58+
PubnubUtil.escapeJava("\uD83D\uDE00a\u0082hi\uD83D\uDE00bye\u0083"));
59+
}
60+
}
61+
62+

0 commit comments

Comments
 (0)