11package ly .count .sdk .java .internal ;
22
33import java .io .BufferedReader ;
4- import java .io .ByteArrayInputStream ;
54import java .io .ByteArrayOutputStream ;
65import java .io .File ;
76import java .io .IOException ;
1110import java .math .BigDecimal ;
1211import java .net .URLDecoder ;
1312import java .net .URLEncoder ;
13+ import java .nio .charset .StandardCharsets ;
1414import java .nio .file .Files ;
1515import java .security .MessageDigest ;
1616import java .util .ArrayList ;
@@ -53,12 +53,12 @@ public static <T> String join(Collection<T> objects, String separator) {
5353 * URLDecoder wrapper to remove try-catch
5454 *
5555 * @param str string to decode
56- * @return url-decoded {@code str}
56+ * @return url-decoded {@code str}, empty string if decoding failed
5757 */
5858 public static String urldecode (String str ) {
5959 try {
6060 return URLDecoder .decode (str , UTF8 );
61- } catch (UnsupportedEncodingException e ) {
61+ } catch (UnsupportedEncodingException | IllegalArgumentException e ) {
6262 return null ;
6363 }
6464 }
@@ -72,7 +72,7 @@ public static String urldecode(String str) {
7272 * @return list of declared fields
7373 */
7474 public static List <Field > reflectiveGetDeclaredFields (Class <?> cls , boolean goUp ) {
75- return reflectiveGetDeclaredFields (new ArrayList <Field >(), cls , goUp );
75+ return reflectiveGetDeclaredFields (new ArrayList <>(), cls , goUp );
7676 }
7777
7878 public static List <Field > reflectiveGetDeclaredFields (List <Field > list , Class <?> cls , boolean goUp ) {
@@ -97,7 +97,7 @@ public static List<Field> reflectiveGetDeclaredFields(List<Field> list, Class<?>
9797 * @return true if null or empty string, false otherwise
9898 */
9999 public static boolean isEmptyOrNull (String str ) {
100- return str == null || "" . equals ( str );
100+ return str == null || str . isEmpty ( );
101101 }
102102
103103 /**
@@ -133,37 +133,49 @@ public static boolean contains(String string, String part) {
133133
134134 /**
135135 * URLEncoder wrapper to remove try-catch
136+ * this class is for the test purposes
136137 *
137138 * @param str string to encode
139+ * @param encoding encoding to use (for testing)
138140 * @return url-encoded {@code str}
139141 */
140- public static String urlencode (String str , Log L ) {
142+ protected static String urlencode (final String str , Log L , final String encoding ) {
141143 try {
142- return URLEncoder .encode (str , UTF8 );
144+ return URLEncoder .encode (str , encoding );
143145 } catch (UnsupportedEncodingException e ) {
144146 if (L != null ) {
145- L .e ("Utils No UTF-8 encoding?" + e );
147+ L .e ("[ Utils] urlencode, No " + encoding + " encoding?" + e );
146148 }
147149 return "" ;
148150 }
149151 }
150152
153+ /**
154+ * URLEncoder wrapper to remove try-catch
155+ *
156+ * @param str string to encode
157+ * @return url-encoded {@code str}
158+ */
159+ public static String urlencode (final String str , Log L ) {
160+ return urlencode (str , L , UTF8 );
161+ }
162+
151163 /**
152164 * Calculate digest (SHA-1, SHA-256, etc.) hash of the string provided
153165 *
154166 * @param digestName digest name like {@code "SHA-256"}, must be supported by Java, see {@link MessageDigest}
155167 * @param string string to hash
156168 * @return hash of the string or null in case of error
157169 */
158- public static String digestHex (String digestName , String string , Log L ) {
170+ public static String digestHex (final String digestName , final String string , Log L ) {
159171 try {
160172 MessageDigest digest = MessageDigest .getInstance (digestName );
161- byte [] bytes = string .getBytes (UTF8 );
173+ byte [] bytes = string .getBytes (StandardCharsets . UTF_8 );
162174 digest .update (bytes , 0 , bytes .length );
163175 return hex (digest .digest ());
164176 } catch (Throwable e ) {
165177 if (L != null ) {
166- L .e ("Utils Cannot calculate sha1" + " / " + e );
178+ L .e ("[ Utils] digestHex, Cannot calculate sha1" + " / " + e );
167179 }
168180 return null ;
169181 }
@@ -173,9 +185,12 @@ public static String digestHex(String digestName, String string, Log L) {
173185 * Get hexadecimal string representation of a byte array
174186 *
175187 * @param bytes array of bytes to convert
176- * @return hex string of the byte array in lower case
188+ * @return hex string of the byte array in lower case, if null or empty returns empty string
177189 */
178190 public static String hex (byte [] bytes ) {
191+ if (bytes == null || bytes .length == 0 ) {
192+ return "" ;
193+ }
179194 char [] hexChars = new char [bytes .length * 2 ];
180195 for (int j = 0 ; j < bytes .length ; j ++) {
181196 int v = bytes [j ] & 0xFF ;
@@ -199,14 +214,14 @@ public static byte[] readStream(InputStream stream, Log L) {
199214 ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
200215 try {
201216 byte [] buffer = new byte [1024 ];
202- int len = 0 ;
217+ int len ;
203218 while ((len = stream .read (buffer )) != -1 ) {
204219 bytes .write (buffer , 0 , len );
205220 }
206221 return bytes .toByteArray ();
207222 } catch (IOException e ) {
208223 if (L != null ) {
209- L .e ("Utils Couldn't read stream" + e . toString () );
224+ L .e ("[ Utils] readStream, Couldn't read stream" + e );
210225 }
211226 return null ;
212227 } finally {
@@ -258,7 +273,7 @@ public static Map<String, String> fixSegmentKeysAndValues(final int keyLength, f
258273 for (Map .Entry <String , String > entry : segments .entrySet ()) {
259274 String k = entry .getKey ();
260275 String v = entry .getValue ();
261- if (k == null || k . length () == 0 || v == null ) {
276+ if (isEmptyOrNull ( k ) || v == null ) {
262277 continue ;
263278 }
264279
@@ -333,12 +348,7 @@ public static String encode(byte[] bytes) {
333348 }
334349
335350 public static String encode (String string ) {
336- try {
337- return encode (string .getBytes (UTF8 ));
338- } catch (UnsupportedEncodingException e ) {
339- // shouldn't happen
340- return null ;
341- }
351+ return encode (string .getBytes (StandardCharsets .UTF_8 ));
342352 }
343353
344354 public static byte [] decode (String string , Log L ) {
@@ -348,27 +358,26 @@ public static byte[] decode(String string, Log L) {
348358 } catch (IllegalArgumentException e ) {
349359 //should not get here
350360 if (L != null ) {
351- L .e ("Utils Error while decoding base64 string, " + e );
361+ L .e ("[ Utils] [Base64] decode, Error while decoding base64 string, " + e );
352362 }
353363 }
354364 return res ;
355365 }
356366
357367 public static String decodeToString (String string , Log L ) {
358- try {
359- return new String (decode (string , L ), UTF8 );
360- } catch (UnsupportedEncodingException e ) {
361- // shouldn't happen
368+ byte [] result = decode (string , L );
369+ if (result == null ) {
362370 return null ;
363371 }
372+ return new String (decode (string , L ), StandardCharsets .UTF_8 );
364373 }
365374 }
366375
367376 /**
368377 * Check whether given string is a valid URL or not
369378 *
370- * @param url
371- * @return
379+ * @param url to validate
380+ * @return true if it is a valid url by RFC2396
372381 */
373382 public static boolean isValidURL (String url ) {
374383 try {
0 commit comments