55import java .nio .charset .StandardCharsets ;
66import java .security .MessageDigest ;
77import java .security .NoSuchAlgorithmException ;
8- import java .util .Iterator ;
9- import java .util .Locale ;
10- import java .util .Map ;
11- import java .util .Map .Entry ;
128import java .util .concurrent .ThreadLocalRandom ;
139import javax .annotation .Nonnull ;
1410
@@ -18,70 +14,6 @@ public final class Strings {
1814 '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f'
1915 };
2016
21- public static String escapeToJson (String string ) {
22- if (string == null || string .isEmpty ()) {
23- return "" ;
24- }
25-
26- final StringBuilder sb = new StringBuilder ();
27- int sz = string .length ();
28- for (int i = 0 ; i < sz ; ++i ) {
29- char ch = string .charAt (i );
30- if (ch > 4095 ) {
31- sb .append ("\\ u" ).append (hex (ch ));
32- } else if (ch > 255 ) {
33- sb .append ("\\ u0" ).append (hex (ch ));
34- } else if (ch > 127 ) {
35- sb .append ("\\ u00" ).append (hex (ch ));
36- } else if (ch < ' ' ) {
37- switch (ch ) {
38- case '\b' :
39- sb .append ((char ) 92 ).append ((char ) 98 );
40- break ;
41- case '\t' :
42- sb .append ((char ) 92 ).append ((char ) 116 );
43- break ;
44- case '\n' :
45- sb .append ((char ) 92 ).append ((char ) 110 );
46- break ;
47- case '\u000b' :
48- default :
49- if (ch > 15 ) {
50- sb .append ("\\ u00" ).append (hex (ch ));
51- } else {
52- sb .append ("\\ u000" ).append (hex (ch ));
53- }
54- break ;
55- case '\f' :
56- sb .append ((char ) 92 ).append ((char ) 102 );
57- break ;
58- case '\r' :
59- sb .append ((char ) 92 ).append ((char ) 114 );
60- break ;
61- }
62- } else {
63- switch (ch ) {
64- case '"' :
65- sb .append ((char ) 92 ).append ((char ) 34 );
66- break ;
67- case '\'' :
68- sb .append ((char ) 92 ).append ((char ) 39 );
69- break ;
70- case '/' :
71- sb .append ((char ) 92 ).append ((char ) 47 );
72- break ;
73- case '\\' :
74- sb .append ((char ) 92 ).append ((char ) 92 );
75- break ;
76- default :
77- sb .append (ch );
78- }
79- }
80- }
81-
82- return sb .toString ();
83- }
84-
8517 public static String toEnvVar (String string ) {
8618 return string .replace ('.' , '_' ).replace ('-' , '_' ).toUpperCase ();
8719 }
@@ -209,16 +141,12 @@ public static String trim(final String string) {
209141 return null == string ? "" : string .trim ();
210142 }
211143
212- private static String hex (char ch ) {
213- return Integer .toHexString (ch ).toUpperCase (Locale .ENGLISH );
214- }
215-
216144 public static String sha256 (String input ) throws NoSuchAlgorithmException {
217145 MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
218146 byte [] hash = digest .digest (input .getBytes (StandardCharsets .UTF_8 ));
219147 StringBuilder hexString = new StringBuilder (2 * hash .length );
220- for (int i = 0 ; i < hash . length ; i ++ ) {
221- String hex = Integer .toHexString (0xFF & hash [ i ] );
148+ for (byte b : hash ) {
149+ String hex = Integer .toHexString (0xFF & b );
222150 if (hex .length () == 1 ) {
223151 hexString .append ('0' );
224152 }
@@ -238,52 +166,6 @@ public static CharSequence truncate(CharSequence input, int limit) {
238166 return input .subSequence (0 , limit );
239167 }
240168
241- public static String toJson (final Map <String , ?> map ) {
242- return toJson (map , false );
243- }
244-
245- public static String toJson (final Map <String , ?> map , boolean valuesAreJson ) {
246- if (map == null || map .isEmpty ()) {
247- return "{}" ;
248- }
249- final StringBuilder sb = new StringBuilder ("{" );
250- final Iterator <? extends Entry <String , ?>> entriesIter = map .entrySet ().iterator ();
251- while (entriesIter .hasNext ()) {
252- final Entry <String , ?> entry = entriesIter .next ();
253-
254- sb .append ('\"' ).append (escapeToJson (entry .getKey ())).append ("\" :" );
255-
256- if (valuesAreJson ) {
257- sb .append (entry .getValue ());
258- } else {
259- sb .append ('\"' ).append (escapeToJson (String .valueOf (entry .getValue ()))).append ('\"' );
260- }
261-
262- if (entriesIter .hasNext ()) {
263- sb .append (',' );
264- }
265- }
266- sb .append ('}' );
267- return sb .toString ();
268- }
269-
270- public static String toJson (final Iterable <String > items ) {
271- if (items == null ) {
272- return "[]" ;
273- }
274- StringBuilder json = new StringBuilder ("[" );
275- Iterator <String > it = items .iterator ();
276- while (it .hasNext ()) {
277- String item = it .next ();
278- json .append ('"' ).append (escapeToJson (item )).append ('"' );
279- if (it .hasNext ()) {
280- json .append (',' );
281- }
282- }
283- json .append (']' );
284- return json .toString ();
285- }
286-
287169 /**
288170 * Checks that a string is not blank, i.e. contains at least one character that is not a
289171 * whitespace
0 commit comments