1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.common.utility;
21
22
23
24
25 public final class StringUtils {
26
27
28
29 public static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
30
31 public static final String LINE_SEP;
32
33 static {
34 LINE_SEP = SystemPropertyUtil.get("line.separator");
35 }
36
37 private StringUtils() {
38
39 }
40
41
42
43
44
45
46 public static byte[] getRandom(final int length) {
47 if (length <= 0) {
48 return SingletonUtils.getSingletonByteArray();
49 }
50 final byte[] result = new byte[length];
51 for (int i = 0; i < result.length; i++) {
52 result[i] = (byte) (RANDOM.nextInt(95) + 32);
53 }
54 return result;
55 }
56
57
58
59
60
61
62
63
64
65
66 public static byte[] getBytesFromArraysToString(final String bytesString) {
67 ParametersChecker.checkParameter("Should not be null or empty",
68 bytesString);
69 final String[] strings =
70 bytesString.replace("[", "").replace("]", "").split(", ");
71 final byte[] result = new byte[strings.length];
72 try {
73 for (int i = 0; i < result.length; i++) {
74 result[i] = (byte) (Integer.parseInt(strings[i]) & 0xFF);
75 }
76 } catch (final NumberFormatException e) {
77 throw new IllegalArgumentException(e);
78 }
79 return result;
80 }
81
82
83
84
85
86
87 public static String getClassName(final Object object) {
88 final Class<?> clasz = object.getClass();
89 String name = clasz.getSimpleName();
90 if (ParametersChecker.isNotEmpty(name)) {
91 return name;
92 } else {
93 name = clasz.getName();
94 final int pos = name.lastIndexOf('.');
95 if (pos < 0) {
96 return name;
97 }
98 return name.substring(pos + 1);
99 }
100 }
101
102 }
103