99public class StringUtils {
1010 public static final String EMPTY = "" ;
1111
12+ /**
13+ * Join a list of Strings
14+ * @param list strings to join
15+ * @param separator the separator to insert between the strings
16+ * @return a string made of the strings in list separated by separator
17+ */
1218 public static String join (List <String > list , String separator ) {
1319 if (list == null ) {
1420 return null ;
@@ -17,21 +23,40 @@ public static String join(List<String> list, String separator) {
1723 return join (list .toArray (), separator , 0 , list .size ());
1824 }
1925
26+ /**
27+ * Join a array of Strings
28+ * @param array strings to join
29+ * @param separator the separator to insert between the strings
30+ * @return a string made of the strings in array separated by separator
31+ */
2032 public static String join (Object [] array , String separator ) {
2133 if (array == null ) {
2234 return null ;
2335 }
2436 return join (array , separator , 0 , array .length );
2537 }
2638
39+ /**
40+ * Join a collection of Strings
41+ * @param collection strings to join
42+ * @param separator the separator to insert between the strings
43+ * @return a string made of the strings in collection separated by separator
44+ */
2745 public static String join (Collection <String > collection , String separator ) {
2846 if (collection == null ) {
2947 return null ;
3048 }
31-
3249 return join (collection .toArray (new String [collection .size ()]), separator , 0 , collection .size ());
3350 }
3451
52+ /**
53+ * Join a array of Strings from startIndex to endIndex
54+ * @param array strings to join
55+ * @param separator the separator to insert between the strings
56+ * @param startIndex the string to start from
57+ * @param endIndex the last string to join
58+ * @return a string made of the strings in array separated by separator
59+ */
3560 public static String join (final Object [] array , String separator , final int startIndex , final int endIndex ) {
3661 if (array == null ) {
3762 return null ;
@@ -60,6 +85,11 @@ public static String join(final Object[] array, String separator, final int star
6085
6186 final protected static char [] hexArray = "0123456789abcdef" .toCharArray ();
6287
88+ /**
89+ * Convert an array of bytes to a string of hex values
90+ * @param bytes bytes to convert
91+ * @return a string of hex values.
92+ */
6393 public static String encodeHexString (byte [] bytes ) {
6494 char [] hexChars = new char [bytes .length * 2 ];
6595 for (int j = 0 ; j < bytes .length ; j ++) {
@@ -70,39 +100,86 @@ public static String encodeHexString(byte[] bytes) {
70100 return new String (hexChars );
71101 }
72102
103+ /**
104+ * Convert a string of hex values to an array of bytes
105+ * @param s a string of two digit Hex numbers. The length of string to parse must be even.
106+ * @return bytes representation of the string
107+ */
108+ public static byte [] hexStringToByteArray (String s ) {
109+ int len = s .length ();
110+ byte [] data = new byte [len / 2 ];
111+
112+ if (len % 2 != 0 ) {
113+ throw new IllegalArgumentException ("Length of string to parse must be even." );
114+ }
115+
116+ for (int i = 0 ; i < len ; i += 2 ) {
117+ data [i / 2 ] = (byte ) ((Character .digit (s .charAt (i ), 16 ) << 4 ) + Character .digit (s .charAt (i + 1 ), 16 ));
118+ }
119+
120+ return data ;
121+ }
122+
123+ /**
124+ * {@see HtmlEscape.escapeHtml}
125+ */
73126 public static String escapeHtml (String input ) {
74127 return HtmlEscape .escapeTextArea (input );
75128 }
76129
130+ /**
131+ * Verify that the input has non whitespace characters in it
132+ * @param input a String-like object
133+ * @return true if input has non whitespace characters in it
134+ */
77135 public static boolean isNotBlank (Object input ) {
78136 if (input == null ) return false ;
79137 return !isBlank (input .toString ());
80138 }
81139
140+ /**
141+ * Verify that the input has non whitespace characters in it
142+ * @param input a String
143+ * @return true if input has non whitespace characters in it
144+ */
82145 public static boolean isNotBlank (String input ) {
83146 return !isBlank (input );
84147 }
85148
149+ /**
150+ * Verify that the input has no characters
151+ * @param input a string
152+ * @return true if input is null or has no characters
153+ */
86154 public static boolean isEmpty (String input ) {
87- if (input == null || input .length () == 0 ) {
88- return true ;
89- }
90- return false ;
155+ return input == null || input .length () == 0 ;
91156 }
92157
158+ /**
159+ * Verify that the input is an empty string or contains only whitespace characters.<br>
160+ * {@see Character.isWhitespace}
161+ * @param input a string
162+ * @return true if input is an empty string or contains only whitespace characters
163+ */
93164 public static boolean isBlank (String input ) {
94165 int strLen ;
95166 if (input == null || (strLen = input .length ()) == 0 ) {
96167 return true ;
97168 }
98169 for (int i = 0 ; i < strLen ; i ++) {
99- if (Character .isWhitespace (input .charAt (i )) == false ) {
170+ if (! Character .isWhitespace (input .charAt (i ))) {
100171 return false ;
101172 }
102173 }
103174 return true ;
104175 }
105176
177+ /**
178+ * Read the entire input stream in 1KB chunks
179+ * @param in input stream to read from
180+ * @return a String generated from the input stream
181+ * @throws IOException thrown by the input stream
182+ */
106183 public static String read (InputStream in ) throws IOException {
107184 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
108185 byte [] buffer = new byte [1024 ];
0 commit comments