Skip to content

Commit 84d482a

Browse files
committed
Merge S String utils into single Utils class
1 parent 022daad commit 84d482a

File tree

3 files changed

+88
-29
lines changed

3 files changed

+88
-29
lines changed

src/AndroidClient/client/src/main/java/net/servicestack/client/S.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/AndroidClient/client/src/main/java/net/servicestack/client/TimeSpan.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public static TimeSpan parse(String xsdDuration){
9696
int seconds = 0;
9797
double ms = 0.0;
9898

99-
String[] t = S.splitOnFirst(xsdDuration.substring(1),'T'); //strip P
99+
String[] t = Utils.splitOnFirst(xsdDuration.substring(1),'T'); //strip P
100100

101101
boolean hasTime = t.length == 2;
102102

103-
String[] d = S.splitOnFirst(t[0],'D');
103+
String[] d = Utils.splitOnFirst(t[0],'D');
104104
if (d.length == 2) {
105105
Integer day = Utils.tryParseInt(d[0]);
106106
if (day != null) {
@@ -109,23 +109,23 @@ public static TimeSpan parse(String xsdDuration){
109109
}
110110

111111
if (hasTime) {
112-
String[] h = S.splitOnFirst(t[1], 'H');
112+
String[] h = Utils.splitOnFirst(t[1], 'H');
113113
if (h.length == 2) {
114114
Integer hour = Utils.tryParseInt(h[0]);
115115
if (hour != null) {
116116
hours = hour;
117117
}
118118
}
119119

120-
String[] m = S.splitOnFirst(h[h.length -1], 'M');
120+
String[] m = Utils.splitOnFirst(h[h.length -1], 'M');
121121
if (m.length == 2) {
122122
Integer min = Utils.tryParseInt(m[0]);
123123
if (min != null) {
124124
minutes = min;
125125
}
126126
}
127127

128-
String[] s = S.splitOnFirst(m[m.length - 1], 'S');
128+
String[] s = Utils.splitOnFirst(m[m.length - 1], 'S');
129129
if (s.length == 2) {
130130
Double millis = Utils.tryParseDouble(s[0]);
131131
if (millis != null){

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
package net.servicestack.client;
44

5+
import com.google.gson.Gson;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.io.UnsupportedEncodingException;
12+
import java.net.HttpURLConnection;
513
import java.nio.ByteBuffer;
614
import java.text.ParseException;
715
import java.text.SimpleDateFormat;
@@ -10,6 +18,7 @@
1018

1119
// Generic Utils
1220
public class Utils {
21+
1322
public static Integer tryParseInt(String str) {
1423
try {
1524
return Integer.parseInt(str);
@@ -156,8 +165,8 @@ public static Date parseDate(String string) {
156165
: string;
157166

158167
if (str.startsWith(wcfJsonPrefix)) {
159-
String body = S.splitOnLast(S.splitOnFirst(str,'(')[1],')')[0];
160-
String unixTimeStr = S.splitOnLast(body.replace('+','-'),'-')[0];
168+
String body = splitOnLast(splitOnFirst(str, '(')[1], ')')[0];
169+
String unixTimeStr = splitOnLast(body.replace('+', '-'), '-')[0];
161170
long unixTime = Long.parseLong(unixTimeStr);
162171
return new Date(unixTime);
163172
}
@@ -179,4 +188,76 @@ public static Date fromIsoDateString(String iso8601string){
179188
throw new RuntimeException(e);
180189
}
181190
}
191+
192+
/*String Utils*/
193+
public static String[] splitOnFirst(String strVal, char needle) {
194+
if (strVal == null) return new String[0];
195+
int pos = strVal.indexOf(needle);
196+
return pos == -1
197+
? new String[] { strVal }
198+
: new String[] { strVal.substring(0, pos), strVal.substring(pos + 1) };
199+
}
200+
201+
public static String[] splitOnLast(String strVal, char needle) {
202+
if (strVal == null) return new String[0];
203+
int pos = strVal.lastIndexOf(needle);
204+
return pos == -1
205+
? new String[] { strVal }
206+
: new String[] { strVal.substring(0, pos), strVal.substring(pos + 1) };
207+
}
208+
209+
public static String combinePath(String basePath, String withPath){
210+
if (basePath == null)
211+
basePath = "";
212+
if (withPath == null)
213+
withPath = "";
214+
215+
String prefix = basePath.endsWith("/")
216+
? basePath
217+
: basePath + "/";
218+
219+
String suffix = withPath.startsWith("/")
220+
? withPath.substring(1)
221+
: withPath;
222+
223+
return prefix + suffix;
224+
}
225+
226+
public static String fromUtf8Bytes(byte[] bytes) {
227+
try {
228+
return new String(bytes, "UTF-8");
229+
} catch (UnsupportedEncodingException e) {
230+
throw new RuntimeException(e);
231+
}
232+
}
233+
234+
public static byte[] toUtf8Bytes(String string) {
235+
try {
236+
return string.getBytes("UTF-8");
237+
} catch (UnsupportedEncodingException e) {
238+
throw new RuntimeException(e);
239+
}
240+
}
241+
242+
public static String readToEnd(HttpURLConnection response){
243+
try {
244+
return readToEnd(response.getInputStream(), "UTF-8");
245+
} catch (IOException e) {
246+
throw new RuntimeException(e);
247+
}
248+
}
249+
250+
public static String readToEnd(InputStream stream, final String charsetName) throws IOException {
251+
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charsetName));
252+
253+
String line;
254+
StringBuilder sb = new StringBuilder();
255+
while ((line = reader.readLine()) != null) {
256+
sb.append(line);
257+
}
258+
259+
String text = sb.toString();
260+
reader.close();
261+
return text;
262+
}
182263
}

0 commit comments

Comments
 (0)