1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.util;
21
22
23 import static org.apache.directory.api.util.Chars.isHex;
24 import static org.apache.directory.api.util.Hex.encodeHex;
25 import static org.apache.directory.api.util.Hex.getHexValue;
26
27 import java.io.UnsupportedEncodingException;
28 import java.nio.charset.Charset;
29 import java.nio.charset.StandardCharsets;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.UUID;
35
36 import org.apache.directory.api.i18n.I18n;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47
48 public final class Strings
49 {
50
51 private static final Logger LOG = LoggerFactory.getLogger( Strings.class );
52
53
54 private static final byte[] HEX_CHAR = new byte[]
55 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
56
57
58 private static final boolean[] IS_PRINTABLE_CHAR =
59 {
60
61 false, false, false, false, false, false, false, false,
62
63 false, false, false, false, false, false, false, false,
64
65 false, false, false, false, false, false, false, false,
66
67 false, false, false, false, false, false, false, false,
68
69 true, false, false, false, false, false, false, true,
70
71 true, true, false, true, true, true, true, true,
72
73 true, true, true, true, true, true, true, true,
74
75 true, true, true, false, false, true, false, true,
76
77 false, true, true, true, true, true, true, true,
78
79 true, true, true, true, true, true, true, true,
80
81 true, true, true, true, true, true, true, true,
82
83 true, true, true, false, false, false, false, false,
84
85 false, true, true, true, true, true, true, true,
86
87 true, true, true, true, true, true, true, true,
88
89 true, true, true, true, true, true, true, true,
90
91 true, true, true, false, false, false, false, false
92 };
93
94 private static final char[] TO_LOWER_CASE =
95 {
96 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
97 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
98 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
99 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
100 ' ', 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, '\'',
101 '(', ')', 0x2A, '+', ',', '-', '.', '/',
102 '0', '1', '2', '3', '4', '5', '6', '7',
103 '8', '9', ':', 0x3B, 0x3C, '=', 0x3E, '?',
104 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
105 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
106 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
107 'x', 'y', 'z', 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
108 0x60, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
109 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
110 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
111 'x', 'y', 'z', 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
112 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
113 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
114 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
115 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
116 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
117 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
118 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
119 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
120 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
121 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
122 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
123 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
124 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
125 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
126 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
127 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
128 };
129
130 private static final byte[] TO_LOWER_CASE_BYTE =
131 {
132 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
133 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
134 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
135 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
136 ' ', 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, '\'',
137 '(', ')', 0x2A, '+', ',', '-', '.', '/',
138 '0', '1', '2', '3', '4', '5', '6', '7',
139 '8', '9', ':', 0x3B, 0x3C, '=', 0x3E, '?',
140 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
141 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
142 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
143 'x', 'y', 'z', 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
144 0x60, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
145 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
146 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
147 'x', 'y', 'z', 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
148 ( byte ) 0x80, ( byte ) 0x81, ( byte ) 0x82, ( byte ) 0x83,
149 ( byte ) 0x84, ( byte ) 0x85, ( byte ) 0x86, ( byte ) 0x87,
150 ( byte ) 0x88, ( byte ) 0x89, ( byte ) 0x8A, ( byte ) 0x8B,
151 ( byte ) 0x8C, ( byte ) 0x8D, ( byte ) 0x8E, ( byte ) 0x8F,
152 ( byte ) 0x90, ( byte ) 0x91, ( byte ) 0x92, ( byte ) 0x93,
153 ( byte ) 0x94, ( byte ) 0x95, ( byte ) 0x96, ( byte ) 0x97,
154 ( byte ) 0x98, ( byte ) 0x99, ( byte ) 0x9A, ( byte ) 0x9B,
155 ( byte ) 0x9C, ( byte ) 0x9D, ( byte ) 0x9E, ( byte ) 0x9F,
156 ( byte ) 0xA0, ( byte ) 0xA1, ( byte ) 0xA2, ( byte ) 0xA3,
157 ( byte ) 0xA4, ( byte ) 0xA5, ( byte ) 0xA6, ( byte ) 0xA7,
158 ( byte ) 0xA8, ( byte ) 0xA9, ( byte ) 0xAA, ( byte ) 0xAB,
159 ( byte ) 0xAC, ( byte ) 0xAD, ( byte ) 0xAE, ( byte ) 0xAF,
160 ( byte ) 0xB0, ( byte ) 0xB1, ( byte ) 0xB2, ( byte ) 0xB3,
161 ( byte ) 0xB4, ( byte ) 0xB5, ( byte ) 0xB6, ( byte ) 0xB7,
162 ( byte ) 0xB8, ( byte ) 0xB9, ( byte ) 0xBA, ( byte ) 0xBB,
163 ( byte ) 0xBC, ( byte ) 0xBD, ( byte ) 0xBE, ( byte ) 0xBF,
164 ( byte ) 0xC0, ( byte ) 0xC1, ( byte ) 0xC2, ( byte ) 0xC3,
165 ( byte ) 0xC4, ( byte ) 0xC5, ( byte ) 0xC6, ( byte ) 0xC7,
166 ( byte ) 0xC8, ( byte ) 0xC9, ( byte ) 0xCA, ( byte ) 0xCB,
167 ( byte ) 0xCC, ( byte ) 0xCD, ( byte ) 0xCE, ( byte ) 0xCF,
168 ( byte ) 0xD0, ( byte ) 0xD1, ( byte ) 0xD2, ( byte ) 0xD3,
169 ( byte ) 0xD4, ( byte ) 0xD5, ( byte ) 0xD6, ( byte ) 0xD7,
170 ( byte ) 0xD8, ( byte ) 0xD9, ( byte ) 0xDA, ( byte ) 0xDB,
171 ( byte ) 0xDC, ( byte ) 0xDD, ( byte ) 0xDE, ( byte ) 0xDF,
172 ( byte ) 0xE0, ( byte ) 0xE1, ( byte ) 0xE2, ( byte ) 0xE3,
173 ( byte ) 0xE4, ( byte ) 0xE5, ( byte ) 0xE6, ( byte ) 0xE7,
174 ( byte ) 0xE8, ( byte ) 0xE9, ( byte ) 0xEA, ( byte ) 0xEB,
175 ( byte ) 0xEC, ( byte ) 0xED, ( byte ) 0xEE, ( byte ) 0xEF,
176 ( byte ) 0xF0, ( byte ) 0xF1, ( byte ) 0xF2, ( byte ) 0xF3,
177 ( byte ) 0xF4, ( byte ) 0xF5, ( byte ) 0xF6, ( byte ) 0xF7,
178 ( byte ) 0xF8, ( byte ) 0xF9, ( byte ) 0xFA, ( byte ) 0xFB,
179 ( byte ) 0xFC, ( byte ) 0xFD, ( byte ) 0xFE, ( byte ) 0xFF
180 };
181
182
183 private static final char[] UPPER_CASE =
184 {
185 0, 0, 0, 0, 0, 0, 0, 0,
186 0, 0, 0, 0, 0, 0, 0, 0,
187 0, 0, 0, 0, 0, 0, 0, 0,
188 0, 0, 0, 0, 0, 0, 0, 0,
189 0, 0, 0, 0, 0, 0, 0, 0,
190 0, 0, 0, 0, 0, '-', 0, 0,
191 '0', '1', '2', '3', '4', '5', '6', '7',
192 '8', '9', 0, 0, 0, 0, 0, 0,
193 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
194 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
195 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
196 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
197 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
198 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
199 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
200 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
201 0, 0, 0, 0, 0, 0, 0, 0,
202 0, 0, 0, 0, 0, 0, 0, 0,
203 0, 0, 0, 0, 0, 0, 0, 0,
204 0, 0, 0, 0, 0, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 0,
206 0, 0, 0, 0, 0, 0, 0, 0,
207 0, 0, 0, 0, 0, 0, 0, 0,
208 0, 0, 0, 0, 0, 0, 0, 0
209 };
210
211 private static final byte[] UTF8 = new byte[]
212 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
213 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
214 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
215 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40,
216 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52,
217 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64,
218 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
219 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F };
220
221
222 public static final byte[] EMPTY_BYTES = new byte[0];
223
224
225 public static final String EMPTY_STRING = "";
226
227
228 public static final String[] EMPTY_STRING_ARRAY = new String[]{};
229
230
231
232
233
234 private Strings()
235 {
236 }
237
238
239
240
241
242
243
244
245 public static String dumpBytes( byte[] buffer )
246 {
247 if ( buffer == null )
248 {
249 return "";
250 }
251
252 StringBuilder sb = new StringBuilder();
253
254 for ( int i = 0; i < buffer.length; i++ )
255 {
256 sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( buffer[i] & 0x00F0 ) >> 4] ) ).append(
257 ( char ) ( HEX_CHAR[buffer[i] & 0x000F] ) ).append( " " );
258 }
259
260 return sb.toString();
261 }
262
263
264
265
266
267
268
269
270 public static String dumpByte( byte octet )
271 {
272 return Strings.utf8ToString( new byte[]
273 { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] } );
274 }
275
276
277
278
279
280
281
282
283 public static char dumpHex( byte hex )
284 {
285 return ( char ) HEX_CHAR[hex & 0x000F];
286 }
287
288
289
290
291
292
293
294
295
296 public static String dumpHexPairs( byte[] buffer )
297 {
298 if ( buffer == null )
299 {
300 return "";
301 }
302
303 char[] str = new char[buffer.length << 1];
304 int pos = 0;
305
306 for ( int i = 0; i < buffer.length; i++ )
307 {
308 str[pos++] = ( char ) ( HEX_CHAR[( buffer[i] & 0x00F0 ) >> 4] );
309 str[pos++] = ( char ) ( HEX_CHAR[buffer[i] & 0x000F] );
310 }
311
312 return new String( str );
313 }
314
315
316
317
318
319
320
321
322
323
324 public static String deepTrim( String str, boolean toLowerCase )
325 {
326 if ( ( null == str ) || ( str.length() == 0 ) )
327 {
328 return "";
329 }
330
331 char ch;
332 int length = str.length();
333 char[] newbuf = new char[length];
334 boolean wsSeen = false;
335 boolean isStart = true;
336 int pos = 0;
337
338 for ( int i = 0; i < length; i++ )
339 {
340 ch = str.charAt( i );
341
342
343 if ( toLowerCase && Character.isUpperCase( ch ) )
344 {
345 ch = Character.toLowerCase( ch );
346 }
347
348
349 if ( Character.isWhitespace( ch ) )
350 {
351
352
353
354 if ( wsSeen )
355 {
356 continue;
357 }
358 else
359 {
360 wsSeen = true;
361
362 if ( isStart )
363 {
364 isStart = false;
365 }
366 else
367 {
368 newbuf[pos++] = ch;
369 }
370 }
371 }
372 else
373 {
374
375 wsSeen = false;
376 isStart = false;
377 newbuf[pos++] = ch;
378 }
379 }
380
381 return pos == 0 ? "" : new String( newbuf, 0, wsSeen ? pos - 1 : pos );
382 }
383
384
385
386
387
388
389
390
391
392
393
394
395
396 public static String deepTrimToLower( String string )
397 {
398 return deepTrim( string, true );
399 }
400
401
402
403
404
405
406
407
408
409
410
411
412 public static String deepTrim( String string )
413 {
414 return deepTrim( string, false );
415 }
416
417
418
419
420
421
422
423
424
425 public static String trimConsecutiveToOne( String str, char ch )
426 {
427 if ( ( null == str ) || ( str.length() == 0 ) )
428 {
429 return "";
430 }
431
432 char[] buffer = str.toCharArray();
433 char[] newbuf = new char[buffer.length];
434 int pos = 0;
435 boolean same = false;
436
437 for ( int i = 0; i < buffer.length; i++ )
438 {
439 char car = buffer[i];
440
441 if ( car == ch )
442 {
443 if ( same )
444 {
445 continue;
446 }
447 else
448 {
449 same = true;
450 newbuf[pos++] = car;
451 }
452 }
453 else
454 {
455 same = false;
456 newbuf[pos++] = car;
457 }
458 }
459
460 return new String( newbuf, 0, pos );
461 }
462
463
464
465
466
467
468
469
470
471
472
473
474
475 public static String centerTrunc( String str, int head, int tail )
476 {
477
478
479
480 if ( str.length() <= ( head + tail + 7 + str.length() / 10 ) )
481 {
482 return str;
483 }
484
485 StringBuilder buf = new StringBuilder();
486 buf.append( '[' ).append( str.length() ).append( "][" );
487 buf.append( str.substring( 0, head ) ).append( "..." );
488 buf.append( str.substring( str.length() - tail ) );
489 buf.append( ']' );
490 return buf.toString();
491 }
492
493
494
495
496
497
498
499
500 public static String toHexString( byte[] res )
501 {
502 StringBuilder buf = new StringBuilder( res.length << 1 );
503
504 for ( int ii = 0; ii < res.length; ii++ )
505 {
506 String digit = Integer.toHexString( 0xFF & res[ii] );
507
508 if ( digit.length() == 1 )
509 {
510 digit = '0' + digit;
511 }
512
513 buf.append( digit );
514 }
515
516 return upperCase( buf.toString() );
517 }
518
519
520
521
522
523
524
525
526 public static byte[] toByteArray( String hexString )
527 {
528 int arrLength = hexString.length() >> 1;
529 byte[] buf = new byte[arrLength];
530
531 for ( int ii = 0; ii < arrLength; ii++ )
532 {
533 int index = ii << 1;
534
535 String digit = hexString.substring( index, index + 2 );
536 buf[ii] = ( byte ) Integer.parseInt( digit, 16 );
537 }
538
539 return buf;
540 }
541
542
543
544
545
546
547
548
549
550
551
552
553 public static String formatHtml( String source, boolean replaceNl, boolean replaceTag,
554 boolean replaceQuote )
555 {
556 StringBuilder buf = new StringBuilder();
557 int len = source.length();
558
559 for ( int i = 0; i < len; i++ )
560 {
561 char ch = source.charAt( i );
562
563 switch ( ch )
564 {
565 case '\"':
566 if ( replaceQuote )
567 {
568 buf.append( """ );
569 }
570 else
571 {
572 buf.append( ch );
573 }
574 break;
575
576 case '<':
577 if ( replaceTag )
578 {
579 buf.append( "<" );
580 }
581 else
582 {
583 buf.append( ch );
584 }
585 break;
586
587 case '>':
588 if ( replaceTag )
589 {
590 buf.append( ">" );
591 }
592 else
593 {
594 buf.append( ch );
595 }
596 break;
597
598 case '\n':
599 if ( replaceNl )
600 {
601 if ( replaceTag )
602 {
603 buf.append( "<br>" );
604 }
605 else
606 {
607 buf.append( "<br>" );
608 }
609 }
610 else
611 {
612 buf.append( ch );
613 }
614 break;
615
616 case '\r':
617 break;
618
619 case '&':
620 buf.append( "&" );
621 break;
622
623 default:
624 buf.append( ch );
625 break;
626 }
627 }
628
629 return buf.toString();
630 }
631
632
633
634
635
636
637
638
639
640
641 public static boolean areEquals( String string, int index, String text )
642 {
643 if ( ( string == null ) || ( text == null ) )
644 {
645 return false;
646 }
647
648 int length1 = string.length();
649 int length2 = text.length();
650
651 if ( ( length1 == 0 ) || ( length1 <= index ) || ( index < 0 )
652 || ( length2 == 0 ) || ( length2 > ( length1 + index ) ) )
653 {
654 return false;
655 }
656 else
657 {
658 return string.substring( index ).startsWith( text );
659 }
660 }
661
662
663
664
665
666
667
668
669
670
671
672
673 public static boolean isCharASCII( byte[] byteArray, int index, char car )
674 {
675 if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) || ( index >= byteArray.length ) )
676 {
677 return false;
678 }
679 else
680 {
681 return ( byteArray[index] == car ) ? true : false;
682 }
683 }
684
685
686
687
688
689
690
691
692
693
694 public static boolean isCharASCII( String string, int index, char car )
695 {
696 if ( string == null )
697 {
698 return false;
699 }
700
701 int length = string.length();
702
703 if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
704 {
705 return false;
706 }
707 else
708 {
709 return string.charAt( index ) == car;
710 }
711 }
712
713
714
715
716
717
718
719 public static String utf8ToString( byte[] bytes )
720 {
721 if ( bytes == null )
722 {
723 return "";
724 }
725
726 char[] chars = new char[bytes.length];
727 int pos = 0;
728
729 try
730 {
731 for ( byte b : bytes )
732 {
733 chars[pos++] = ( char ) UTF8[b];
734 }
735 }
736 catch ( ArrayIndexOutOfBoundsException aioobe )
737 {
738 return new String( bytes, StandardCharsets.UTF_8 );
739 }
740
741 return new String( chars );
742 }
743
744
745
746
747
748
749
750
751
752 public static String utf8ToString( byte[] bytes, int length )
753 {
754 if ( bytes == null )
755 {
756 return "";
757 }
758
759 return new String( bytes, 0, length, StandardCharsets.UTF_8 );
760 }
761
762
763
764
765
766
767
768
769
770
771 public static String utf8ToString( byte[] bytes, int start, int length )
772 {
773 if ( bytes == null )
774 {
775 return "";
776 }
777
778 return new String( bytes, start, length, StandardCharsets.UTF_8 );
779 }
780
781
782
783
784
785
786
787
788
789
790 public static int areEquals( byte[] bytes, int index, String text )
791 {
792 if ( ( bytes == null ) || ( bytes.length == 0 ) || ( bytes.length <= index ) || ( index < 0 )
793 || ( text == null ) )
794 {
795 return StringConstants.NOT_EQUAL;
796 }
797 else
798 {
799 byte[] data = text.getBytes( StandardCharsets.UTF_8 );
800
801 return areEquals( bytes, index, data );
802 }
803 }
804
805
806
807
808
809
810
811
812
813
814 public static int areEquals( char[] chars, int index, String text )
815 {
816 return areEquals( chars, index, text, true );
817 }
818
819
820
821
822
823
824
825
826
827
828
829 public static int areEquals( char[] chars, int index, String text, boolean caseSensitive )
830 {
831 if ( ( chars == null ) || ( chars.length == 0 ) || ( chars.length <= index ) || ( index < 0 )
832 || ( text == null ) )
833 {
834 return StringConstants.NOT_EQUAL;
835 }
836 else
837 {
838 char[] data = text.toCharArray();
839
840 return areEquals( chars, index, data, caseSensitive );
841 }
842 }
843
844
845
846
847
848
849
850
851
852
853 public static int areEquals( char[] chars, int index, char[] chars2 )
854 {
855 return areEquals( chars, index, chars2, true );
856 }
857
858
859
860
861
862
863
864
865
866
867
868 public static int areEquals( char[] chars, int index, char[] chars2, boolean caseSensitive )
869 {
870 if ( ( chars == null ) || ( chars.length == 0 ) || ( chars.length <= index ) || ( index < 0 )
871 || ( chars2 == null ) || ( chars2.length == 0 )
872 || ( chars2.length > ( chars.length - index ) ) )
873 {
874 return StringConstants.NOT_EQUAL;
875 }
876 else
877 {
878 for ( int i = 0; i < chars2.length; i++ )
879 {
880 char c1 = chars[index++];
881 char c2 = chars2[i];
882
883 if ( !caseSensitive )
884 {
885 c1 = Character.toLowerCase( c1 );
886 c2 = Character.toLowerCase( c2 );
887 }
888
889 if ( c1 != c2 )
890 {
891 return StringConstants.NOT_EQUAL;
892 }
893 }
894
895 return index;
896 }
897 }
898
899
900
901
902
903
904
905
906
907
908 public static int areEquals( byte[] bytes, int index, byte[] bytes2 )
909 {
910 if ( ( bytes == null ) || ( bytes.length == 0 ) || ( bytes.length <= index ) || ( index < 0 )
911 || ( bytes2 == null ) || ( bytes2.length == 0 )
912 || ( bytes2.length > ( bytes.length - index ) ) )
913 {
914 return StringConstants.NOT_EQUAL;
915 }
916 else
917 {
918 for ( int i = 0; i < bytes2.length; i++ )
919 {
920 if ( bytes[index++] != bytes2[i] )
921 {
922 return StringConstants.NOT_EQUAL;
923 }
924 }
925
926 return index;
927 }
928 }
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952 public static boolean isEmpty( String str )
953 {
954 return ( str == null ) || ( str.length() == 0 );
955 }
956
957
958
959
960
961
962
963
964 public static boolean isEmpty( byte[] bytes )
965 {
966 return ( bytes == null ) || ( bytes.length == 0 );
967 }
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988 public static String trim( String str )
989 {
990 return isEmpty( str ) ? "" : str.trim();
991 }
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013 public static byte[] trim( byte[] bytes )
1014 {
1015 if ( isEmpty( bytes ) )
1016 {
1017 return EMPTY_BYTES;
1018 }
1019
1020 int start = trimLeft( bytes, 0 );
1021 int end = trimRight( bytes, bytes.length - 1 );
1022
1023 int length = end - start + 1;
1024
1025 if ( length != 0 )
1026 {
1027 byte[] newBytes = new byte[end - start + 1];
1028
1029 System.arraycopy( bytes, start, newBytes, 0, length );
1030
1031 return newBytes;
1032 }
1033 else
1034 {
1035 return EMPTY_BYTES;
1036 }
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058 public static String trimLeft( String str )
1059 {
1060 if ( isEmpty( str ) )
1061 {
1062 return "";
1063 }
1064
1065 int start = 0;
1066 int end = str.length();
1067
1068 while ( ( start < end ) && ( str.charAt( start ) == ' ' ) )
1069 {
1070 start++;
1071 }
1072
1073 return start == 0 ? str : str.substring( start );
1074 }
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 public static int trimLeft( char[] chars, int pos )
1098 {
1099 if ( chars == null )
1100 {
1101 return pos;
1102 }
1103
1104 while ( ( pos < chars.length ) && ( chars[pos] == ' ' ) )
1105 {
1106 pos++;
1107 }
1108
1109 return pos;
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131 public static void trimLeft( String string, Position pos )
1132 {
1133 if ( string == null )
1134 {
1135 return;
1136 }
1137
1138 int length = string.length();
1139
1140 while ( ( pos.start < length ) && ( string.charAt( pos.start ) == ' ' ) )
1141 {
1142 pos.start++;
1143 }
1144
1145 pos.end = pos.start;
1146 }
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 public static void trimLeft( byte[] bytes, Position pos )
1168 {
1169 if ( bytes == null )
1170 {
1171 return;
1172 }
1173
1174 int length = bytes.length;
1175
1176 while ( ( pos.start < length ) && ( bytes[pos.start] == ' ' ) )
1177 {
1178 pos.start++;
1179 }
1180
1181 pos.end = pos.start;
1182 }
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205 public static int trimLeft( byte[] bytes, int pos )
1206 {
1207 if ( bytes == null )
1208 {
1209 return pos;
1210 }
1211
1212 while ( ( pos < bytes.length ) && ( bytes[pos] == ' ' ) )
1213 {
1214 pos++;
1215 }
1216
1217 return pos;
1218 }
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 public static String trimRight( String str )
1240 {
1241 if ( isEmpty( str ) )
1242 {
1243 return "";
1244 }
1245
1246 int length = str.length();
1247 int end = length;
1248
1249 while ( ( end > 0 ) && ( str.charAt( end - 1 ) == ' ' ) )
1250 {
1251 if ( ( end > 1 ) && ( str.charAt( end - 2 ) == '\\' ) )
1252 {
1253 break;
1254 }
1255
1256 end--;
1257 }
1258
1259 return end == length ? str : str.substring( 0, end );
1260 }
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282 public static String trimRight( String str, int escapedSpace )
1283 {
1284 if ( isEmpty( str ) )
1285 {
1286 return "";
1287 }
1288
1289 int length = str.length();
1290 int end = length;
1291
1292 while ( ( end > 0 ) && ( str.charAt( end - 1 ) == ' ' ) && ( end > escapedSpace ) )
1293 {
1294 if ( ( end > 1 ) && ( str.charAt( end - 2 ) == '\\' ) )
1295 {
1296 break;
1297 }
1298
1299 end--;
1300 }
1301
1302 return end == length ? str : str.substring( 0, end );
1303 }
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 public static int trimRight( char[] chars, int pos )
1327 {
1328 if ( chars == null )
1329 {
1330 return pos;
1331 }
1332
1333 while ( ( pos >= 0 ) && ( chars[pos - 1] == ' ' ) )
1334 {
1335 pos--;
1336 }
1337
1338 return pos;
1339 }
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362 public static String trimRight( String string, Position pos )
1363 {
1364 if ( string == null )
1365 {
1366 return "";
1367 }
1368
1369 while ( ( pos.end >= 0 ) && ( string.charAt( pos.end - 1 ) == ' ' ) )
1370 {
1371 if ( ( pos.end > 1 ) && ( string.charAt( pos.end - 2 ) == '\\' ) )
1372 {
1373 break;
1374 }
1375
1376 pos.end--;
1377 }
1378
1379 return pos.end == string.length() ? string : string.substring( 0, pos.end );
1380 }
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403 public static String trimRight( byte[] bytes, Position pos )
1404 {
1405 if ( bytes == null )
1406 {
1407 return "";
1408 }
1409
1410 while ( ( pos.end >= 0 ) && ( bytes[pos.end - 1] == ' ' ) )
1411 {
1412 if ( ( pos.end > 1 ) && ( bytes[pos.end - 2] == '\\' ) )
1413 {
1414 break;
1415 }
1416
1417 pos.end--;
1418 }
1419
1420 if ( pos.end == bytes.length )
1421 {
1422 return utf8ToString( bytes );
1423 }
1424 else
1425 {
1426 return utf8ToString( bytes, pos.end );
1427 }
1428 }
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451 public static int trimRight( byte[] bytes, int pos )
1452 {
1453 if ( bytes == null )
1454 {
1455 return pos;
1456 }
1457
1458 while ( ( pos >= 0 ) && ( bytes[pos] == ' ' ) )
1459 {
1460 pos--;
1461 }
1462
1463 return pos;
1464 }
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474 public static char charAt( String string, int index )
1475 {
1476 if ( string == null )
1477 {
1478 return '\0';
1479 }
1480
1481 int length = string.length();
1482
1483 if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
1484 {
1485 return '\0';
1486 }
1487 else
1488 {
1489 return string.charAt( index );
1490 }
1491 }
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501 public static byte byteAt( byte[] bytes, int index )
1502 {
1503 if ( bytes == null )
1504 {
1505 return '\0';
1506 }
1507
1508 int length = bytes.length;
1509
1510 if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
1511 {
1512 return '\0';
1513 }
1514 else
1515 {
1516 return bytes[index];
1517 }
1518 }
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528 public static char charAt( char[] chars, int index )
1529 {
1530 if ( chars == null )
1531 {
1532 return '\0';
1533 }
1534
1535 int length = chars.length;
1536
1537 if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
1538 {
1539 return '\0';
1540 }
1541 else
1542 {
1543 return chars[index];
1544 }
1545 }
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555 public static String asciiBytesToString( byte[] bytes )
1556 {
1557 if ( ( bytes == null ) || ( bytes.length == 0 ) )
1558 {
1559 return "";
1560 }
1561
1562 char[] result = new char[bytes.length];
1563
1564 for ( int i = 0; i < bytes.length; i++ )
1565 {
1566 result[i] = ( char ) bytes[i];
1567 }
1568
1569 return new String( result );
1570 }
1571
1572
1573
1574
1575
1576
1577
1578
1579 public static byte[] getBytesUtf8( String string )
1580 {
1581 if ( string == null )
1582 {
1583 return EMPTY_BYTES;
1584 }
1585
1586 return string.getBytes( StandardCharsets.UTF_8 );
1587 }
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597 public static byte[] getBytesUtf8Ascii( String string )
1598 {
1599 if ( string == null )
1600 {
1601 return EMPTY_BYTES;
1602 }
1603
1604 try
1605 {
1606 char[] chars = string.toCharArray();
1607 byte[] bytes = new byte[chars.length];
1608 int pos = 0;
1609
1610 for ( char c : chars )
1611 {
1612 bytes[pos++] = UTF8[c];
1613 }
1614
1615 return bytes;
1616 }
1617 catch ( ArrayIndexOutOfBoundsException aioobe )
1618 {
1619 return string.getBytes( StandardCharsets.UTF_8 );
1620 }
1621 }
1622
1623
1624
1625
1626
1627
1628
1629 public static String getDefaultCharsetName()
1630 {
1631 return Charset.defaultCharset().name();
1632 }
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659 public static boolean equals( String str1, String str2 )
1660 {
1661 return str1 == null ? str2 == null : str1.equals( str2 );
1662 }
1663
1664
1665
1666
1667
1668
1669
1670
1671 public static String listToString( List<?> list )
1672 {
1673 if ( ( list == null ) || list.isEmpty() )
1674 {
1675 return "";
1676 }
1677
1678 StringBuilder sb = new StringBuilder();
1679 boolean isFirst = true;
1680
1681 for ( Object elem : list )
1682 {
1683 if ( isFirst )
1684 {
1685 isFirst = false;
1686 }
1687 else
1688 {
1689 sb.append( ", " );
1690 }
1691
1692 sb.append( elem );
1693 }
1694
1695 return sb.toString();
1696 }
1697
1698
1699
1700
1701
1702
1703
1704
1705 public static String setToString( Set<?> set )
1706 {
1707 if ( ( set == null ) || set.isEmpty() )
1708 {
1709 return "";
1710 }
1711
1712 StringBuilder sb = new StringBuilder();
1713 boolean isFirst = true;
1714
1715 for ( Object elem : set )
1716 {
1717 if ( isFirst )
1718 {
1719 isFirst = false;
1720 }
1721 else
1722 {
1723 sb.append( ", " );
1724 }
1725
1726 sb.append( elem );
1727 }
1728
1729 return sb.toString();
1730 }
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740 public static String listToString( List<?> list, String tabs )
1741 {
1742 if ( ( list == null ) || list.isEmpty() )
1743 {
1744 return "";
1745 }
1746
1747 StringBuilder sb = new StringBuilder();
1748
1749 for ( Object elem : list )
1750 {
1751 sb.append( tabs );
1752 sb.append( elem );
1753 sb.append( '\n' );
1754 }
1755
1756 return sb.toString();
1757 }
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767 public static String mapToString( Map<?, ?> map )
1768 {
1769 if ( ( map == null ) || map.isEmpty() )
1770 {
1771 return "";
1772 }
1773
1774 StringBuilder sb = new StringBuilder();
1775 boolean isFirst = true;
1776
1777 for ( Map.Entry<?, ?> entry : map.entrySet() )
1778 {
1779 if ( isFirst )
1780 {
1781 isFirst = false;
1782 }
1783 else
1784 {
1785 sb.append( ", " );
1786 }
1787
1788 sb.append( entry.getKey() );
1789 sb.append( " = '" ).append( entry.getValue() ).append( "'" );
1790 }
1791
1792 return sb.toString();
1793 }
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804 public static String mapToString( Map<?, ?> map, String tabs )
1805 {
1806 if ( ( map == null ) || map.isEmpty() )
1807 {
1808 return "";
1809 }
1810
1811 StringBuilder sb = new StringBuilder();
1812
1813 for ( Map.Entry<?, ?> entry : map.entrySet() )
1814 {
1815 sb.append( tabs );
1816 sb.append( entry.getKey() );
1817
1818 sb.append( " = '" ).append( entry.getValue().toString() ).append( "'\n" );
1819 }
1820
1821 return sb.toString();
1822 }
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834 @Deprecated
1835 public static String toLowerCase( String value )
1836 {
1837 if ( ( null == value ) || ( value.length() == 0 ) )
1838 {
1839 return "";
1840 }
1841
1842 char[] chars = value.toCharArray();
1843
1844 for ( int i = 0; i < chars.length; i++ )
1845 {
1846 chars[i] = TO_LOWER_CASE[chars[i]];
1847 }
1848
1849 return new String( chars );
1850 }
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861 public static String toLowerCaseAscii( String value )
1862 {
1863 if ( ( null == value ) || ( value.length() == 0 ) )
1864 {
1865 return "";
1866 }
1867
1868 char[] chars = value.toCharArray();
1869
1870 for ( int i = 0; i < chars.length; i++ )
1871 {
1872 chars[i] = TO_LOWER_CASE[chars[i]];
1873 }
1874
1875 return new String( chars );
1876 }
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887 public static String toLowerCase( byte[] value )
1888 {
1889 if ( ( null == value ) || ( value.length == 0 ) )
1890 {
1891 return "";
1892 }
1893
1894 for ( int i = 0; i < value.length; i++ )
1895 {
1896 value[i] = TO_LOWER_CASE_BYTE[value[i]];
1897 }
1898
1899 return Strings.utf8ToString( value );
1900 }
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912 @Deprecated
1913 public static String toUpperCase( String value )
1914 {
1915 if ( ( null == value ) || ( value.length() == 0 ) )
1916 {
1917 return "";
1918 }
1919
1920 char[] chars = value.toCharArray();
1921
1922 for ( int i = 0; i < chars.length; i++ )
1923 {
1924 chars[i] = UPPER_CASE[chars[i]];
1925 }
1926
1927 return new String( chars );
1928 }
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939 public static String toUpperCaseAscii( String value )
1940 {
1941 if ( ( null == value ) || ( value.length() == 0 ) )
1942 {
1943 return "";
1944 }
1945
1946 char[] chars = value.toCharArray();
1947
1948 for ( int i = 0; i < chars.length; i++ )
1949 {
1950 chars[i] = UPPER_CASE[chars[i]];
1951 }
1952
1953 return new String( chars );
1954 }
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974 public static String upperCase( String str )
1975 {
1976 if ( str == null )
1977 {
1978 return null;
1979 }
1980
1981 return str.toUpperCase( Locale.ROOT );
1982 }
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002 public static String lowerCase( String str )
2003 {
2004 if ( str == null )
2005 {
2006 return null;
2007 }
2008
2009 return str.toLowerCase( Locale.ROOT );
2010 }
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022 public static String lowerCaseAscii( String str )
2023 {
2024 if ( str == null )
2025 {
2026 return null;
2027 }
2028
2029 char[] chars = str.toCharArray();
2030 int pos = 0;
2031
2032 for ( char c : chars )
2033 {
2034 chars[pos++] = TO_LOWER_CASE[c];
2035 }
2036
2037 return new String( chars );
2038 }
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051 public static boolean isPrintableString( String str )
2052 {
2053 if ( ( str == null ) || ( str.length() == 0 ) )
2054 {
2055 return true;
2056 }
2057
2058 for ( char c : str.toCharArray() )
2059 {
2060 if ( ( c > 127 ) || !IS_PRINTABLE_CHAR[c] )
2061 {
2062 return false;
2063 }
2064 }
2065
2066 return true;
2067 }
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086 public static boolean isNotEmpty( String str )
2087 {
2088 return ( str != null ) && ( str.length() > 0 );
2089 }
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101 public static boolean isIA5String( String str )
2102 {
2103 if ( ( str == null ) || ( str.length() == 0 ) )
2104 {
2105 return true;
2106 }
2107
2108
2109 for ( char c : str.toCharArray() )
2110 {
2111 if ( ( c < 0 ) || ( c > 0x7F ) )
2112 {
2113 return false;
2114 }
2115 }
2116
2117 return true;
2118 }
2119
2120
2121
2122
2123
2124
2125
2126
2127 public static boolean isValidUuid( String uuid )
2128 {
2129 if ( uuid.length() < 36 )
2130 {
2131 return false;
2132 }
2133
2134 if ( isHex( uuid.charAt( 0 ) ) && isHex( uuid.charAt( 1 ) ) && isHex( uuid.charAt( 2 ) )
2135 && isHex( uuid.charAt( 3 ) ) && isHex( uuid.charAt( 4 ) ) && isHex( uuid.charAt( 5 ) )
2136 && isHex( uuid.charAt( 6 ) ) && isHex( uuid.charAt( 7 ) ) && ( uuid.charAt( 8 ) == '-' )
2137 && isHex( uuid.charAt( 9 ) ) && isHex( uuid.charAt( 10 ) ) && isHex( uuid.charAt( 11 ) )
2138 && isHex( uuid.charAt( 12 ) ) && ( uuid.charAt( 13 ) == '-' ) && isHex( uuid.charAt( 14 ) )
2139 && isHex( uuid.charAt( 15 ) ) && isHex( uuid.charAt( 16 ) ) && isHex( uuid.charAt( 17 ) )
2140 && ( uuid.charAt( 18 ) == '-' ) && isHex( uuid.charAt( 19 ) ) && isHex( uuid.charAt( 20 ) )
2141 && isHex( uuid.charAt( 21 ) ) && isHex( uuid.charAt( 22 ) ) && ( uuid.charAt( 23 ) == '-' )
2142 && isHex( uuid.charAt( 24 ) ) && isHex( uuid.charAt( 25 ) ) && isHex( uuid.charAt( 26 ) )
2143 && isHex( uuid.charAt( 27 ) ) && isHex( uuid.charAt( 28 ) ) && isHex( uuid.charAt( 29 ) )
2144 && isHex( uuid.charAt( 30 ) ) && isHex( uuid.charAt( 31 ) ) && isHex( uuid.charAt( 32 ) )
2145 && isHex( uuid.charAt( 33 ) ) && isHex( uuid.charAt( 34 ) ) && isHex( uuid.charAt( 35 ) ) )
2146 {
2147
2148 LOG.debug( "Syntax valid for '{}'", uuid );
2149 return true;
2150 }
2151
2152 LOG.debug( "Syntax invalid for '{}'", uuid );
2153 return false;
2154 }
2155
2156
2157
2158
2159
2160
2161
2162
2163 public static String uuidToString( byte[] bytes )
2164 {
2165 if ( ( bytes == null ) || ( bytes.length != 16 ) )
2166 {
2167 return "Invalid UUID";
2168 }
2169
2170 char[] hex = encodeHex( bytes );
2171 StringBuilder sb = new StringBuilder();
2172 sb.append( hex, 0, 8 );
2173 sb.append( '-' );
2174 sb.append( hex, 8, 4 );
2175 sb.append( '-' );
2176 sb.append( hex, 12, 4 );
2177 sb.append( '-' );
2178 sb.append( hex, 16, 4 );
2179 sb.append( '-' );
2180 sb.append( hex, 20, 12 );
2181
2182 return Strings.toLowerCaseAscii( sb.toString() );
2183 }
2184
2185
2186
2187
2188
2189
2190
2191
2192 public static byte[] uuidToBytes( String string )
2193 {
2194 if ( !isValidUuid( string ) )
2195 {
2196 return null;
2197 }
2198
2199 char[] chars = string.toCharArray();
2200 byte[] bytes = new byte[16];
2201 bytes[0] = getHexValue( chars[0], chars[1] );
2202 bytes[1] = getHexValue( chars[2], chars[3] );
2203 bytes[2] = getHexValue( chars[4], chars[5] );
2204 bytes[3] = getHexValue( chars[6], chars[7] );
2205
2206 bytes[4] = getHexValue( chars[9], chars[10] );
2207 bytes[5] = getHexValue( chars[11], chars[12] );
2208
2209 bytes[6] = getHexValue( chars[14], chars[15] );
2210 bytes[7] = getHexValue( chars[16], chars[17] );
2211
2212 bytes[8] = getHexValue( chars[19], chars[20] );
2213 bytes[9] = getHexValue( chars[21], chars[22] );
2214
2215 bytes[10] = getHexValue( chars[24], chars[25] );
2216 bytes[11] = getHexValue( chars[26], chars[27] );
2217 bytes[12] = getHexValue( chars[28], chars[29] );
2218 bytes[13] = getHexValue( chars[30], chars[31] );
2219 bytes[14] = getHexValue( chars[32], chars[33] );
2220 bytes[15] = getHexValue( chars[34], chars[35] );
2221
2222 return bytes;
2223 }
2224
2225
2226
2227
2228
2229
2230
2231
2232 public static byte[] copy( byte[] value )
2233 {
2234 if ( isEmpty( value ) )
2235 {
2236 return EMPTY_BYTES;
2237 }
2238
2239 byte[] copy = new byte[value.length];
2240 System.arraycopy( value, 0, copy, 0, value.length );
2241
2242 return copy;
2243 }
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258 public static String getString( final byte[] data, int offset, int length, String charset )
2259 {
2260 if ( data == null )
2261 {
2262 throw new IllegalArgumentException( I18n.err( I18n.ERR_04411 ) );
2263 }
2264
2265 if ( ( charset == null ) || ( charset.length() == 0 ) )
2266 {
2267 throw new IllegalArgumentException( I18n.err( I18n.ERR_04412 ) );
2268 }
2269
2270 try
2271 {
2272 return new String( data, offset, length, charset );
2273 }
2274 catch ( UnsupportedEncodingException e )
2275 {
2276 return new String( data, offset, length, Charset.defaultCharset() );
2277 }
2278 }
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291 public static String getString( final byte[] data, String charset )
2292 {
2293 return getString( data, 0, data.length, charset );
2294 }
2295
2296
2297
2298
2299
2300
2301
2302
2303 public static String getUUID( long value )
2304 {
2305 return new UUID( 0, value ).toString();
2306 }
2307
2308
2309
2310
2311
2312
2313
2314
2315 public static int parseInt( String value )
2316 {
2317 long res = 0;
2318
2319 for ( char c : value.toCharArray() )
2320 {
2321 if ( ( c >= '0' ) && ( c <= '9' ) )
2322 {
2323 res = res * 10 + ( c - '0' );
2324
2325 if ( res > Integer.MAX_VALUE )
2326 {
2327 throw new NumberFormatException( "Integer " + value + " is too big" );
2328 }
2329 }
2330 else
2331 {
2332 throw new NumberFormatException( "Integer " + value + " is not valid" );
2333 }
2334 }
2335
2336 return ( int ) res;
2337 }
2338 }