1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.util;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.nio.charset.Charset;
26 import java.nio.charset.CharsetDecoder;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.StringTokenizer;
31
32
33 public final class DatatypeHelper {
34
35
36 private DatatypeHelper() {
37
38 }
39
40
41
42
43
44
45
46
47 public static boolean isEmpty(String s) {
48 if (s != null) {
49 String sTrimmed = s.trim();
50 if (sTrimmed.length() > 0) {
51 return false;
52 }
53 }
54
55 return true;
56 }
57
58
59
60
61
62
63
64
65
66
67 public static <T> boolean safeEquals(T s1, T s2) {
68 if (s1 == null || s2 == null) {
69 return s1 == s2;
70 }
71
72 return s1.equals(s2);
73 }
74
75
76
77
78
79
80
81
82 public static String safeTrim(String s) {
83 if (s != null) {
84 return s.trim();
85 }
86
87 return null;
88 }
89
90
91
92
93
94
95
96
97
98 public static String safeTrimOrNullString(String s) {
99 if (s != null) {
100 String sTrimmed = s.trim();
101 if (sTrimmed.length() > 0) {
102 return sTrimmed;
103 }
104 }
105
106 return null;
107 }
108
109
110
111
112
113
114
115
116 public static byte[] intToByteArray(int integer) {
117 byte[] intBytes = new byte[4];
118 intBytes[0] = (byte) ((integer & 0xff000000) >>> 24);
119 intBytes[1] = (byte) ((integer & 0x00ff0000) >>> 16);
120 intBytes[2] = (byte) ((integer & 0x0000ff00) >>> 8);
121 intBytes[3] = (byte) ((integer & 0x000000ff));
122
123 return intBytes;
124 }
125
126
127
128
129
130
131
132
133
134 public static byte[] fileToByteArray(File file) throws IOException {
135 long numOfBytes = file.length();
136
137 if (numOfBytes > Integer.MAX_VALUE) {
138 throw new IOException("File is to large to be read in to a byte array");
139 }
140
141 byte[] bytes = new byte[(int) numOfBytes];
142 FileInputStream ins = new FileInputStream(file);
143 int offset = 0;
144 int numRead = 0;
145 do {
146 numRead = ins.read(bytes, offset, bytes.length - offset);
147 offset += numRead;
148 } while (offset < bytes.length && numRead >= 0);
149
150 if (offset < bytes.length) {
151 throw new IOException("Could not completely read file " + file.getName());
152 }
153
154 ins.close();
155 return bytes;
156 }
157
158
159
160
161
162
163
164
165
166
167
168 public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException {
169 CharsetDecoder charsetDecoder = decoder;
170 if (decoder == null) {
171 charsetDecoder = Charset.defaultCharset().newDecoder();
172 }
173
174 BufferedReader reader = new BufferedReader(new InputStreamReader(input, charsetDecoder));
175
176 StringBuilder stringBuffer = new StringBuilder();
177 String line = reader.readLine();
178 while(line != null){
179 stringBuffer.append(line).append("\n");
180 line = reader.readLine();
181 }
182
183 reader.close();
184
185 return stringBuffer.toString();
186 }
187
188
189
190
191
192
193
194
195
196
197
198 public static List<String> stringToList(String string, String delimiter) {
199 if (delimiter == null) {
200 throw new IllegalArgumentException("String delimiter may not be null");
201 }
202
203 ArrayList<String> values = new ArrayList<String>();
204
205 String trimmedString = safeTrimOrNullString(string);
206 if (trimmedString != null) {
207 StringTokenizer tokens = new StringTokenizer(trimmedString, delimiter);
208 while (tokens.hasMoreTokens()) {
209 values.add(tokens.nextToken());
210 }
211 }
212
213 return values;
214 }
215
216
217
218
219
220
221
222
223
224
225 public static String listToStringValue(List<String> values, String delimiter) {
226 if (delimiter == null) {
227 throw new IllegalArgumentException("String delimiter may not be null");
228 }
229
230 StringBuilder stringValue = new StringBuilder();
231 Iterator<String> valueItr = values.iterator();
232 while(valueItr.hasNext()){
233 stringValue.append(valueItr.next());
234 if(valueItr.hasNext()){
235 stringValue.append(delimiter);
236 }
237 }
238
239 return stringValue.toString();
240 }
241 }