View Javadoc

1   package ca.uhn.hl7v2.hoh.util;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.InputStreamReader;
5   import java.nio.charset.Charset;
6   
7   public class StringUtils {
8   
9   	public static final String LINE_SEP = System.getProperty("line.separator");
10  
11  	/**
12  	 * Null safe equals comparison
13  	 */
14  	public static boolean equals(String theString1, String theString2) {
15  		if (theString1 == null && theString2 == null) {
16  			return true;
17  		}
18  		if (theString1 == null || theString2 == null) {
19  			return false;
20  		}
21  		return theString1.equals(theString2);
22  	}
23  
24  	public static boolean isNotBlank(String theString) {
25  		return !isBlank(theString);
26  	}
27  
28  	public static boolean isBlank(String theString) {
29  		int strLen;
30  		if (theString == null || (strLen = theString.length()) == 0) {
31  			return true;
32  		}
33  		for (int i = 0; i < strLen; i++) {
34  			if ((Character.isWhitespace(theString.charAt(i)) == false)) {
35  				return false;
36  			}
37  		}
38  		return true;
39  	}
40  
41  	public static String defaultString(String theString) {
42  		if (theString == null) {
43  			return "";
44  		}
45  		return theString;
46  	}
47  
48  	public static String asciiEscape(byte[] theBytes, Charset theCharset) {
49  		StringBuilder b = new StringBuilder();
50  
51  		try {
52  			InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(theBytes), theCharset);
53  			while (reader.ready()) {
54  				char read = (char) reader.read();
55  				if (read < 32) {
56  					b.append('[').append((int) read).append(']');
57  				} else {
58  					b.append(read);
59  				}
60  			}
61  		} catch (Exception e) {
62  			b.append("ERROR: ").append(e.toString());
63  		}
64  		return b.toString();
65  	}
66  
67  }