22
33package 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 ;
513import java .nio .ByteBuffer ;
614import java .text .ParseException ;
715import java .text .SimpleDateFormat ;
1018
1119// Generic Utils
1220public 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