001package ca.uhn.hl7v2.hoh.util;
002
003import java.io.ByteArrayInputStream;
004import java.io.InputStreamReader;
005import java.nio.charset.Charset;
006
007public class StringUtils {
008
009        public static final String LINE_SEP = System.getProperty("line.separator");
010
011        /**
012         * Null safe equals comparison
013         */
014        public static boolean equals(String theString1, String theString2) {
015                if (theString1 == null && theString2 == null) {
016                        return true;
017                }
018                if (theString1 == null || theString2 == null) {
019                        return false;
020                }
021                return theString1.equals(theString2);
022        }
023
024        public static boolean isNotBlank(String theString) {
025                return !isBlank(theString);
026        }
027
028        public static boolean isBlank(String theString) {
029                int strLen;
030                if (theString == null || (strLen = theString.length()) == 0) {
031                        return true;
032                }
033                for (int i = 0; i < strLen; i++) {
034                        if ((Character.isWhitespace(theString.charAt(i)) == false)) {
035                                return false;
036                        }
037                }
038                return true;
039        }
040
041        public static String defaultString(String theString) {
042                if (theString == null) {
043                        return "";
044                }
045                return theString;
046        }
047
048        public static String asciiEscape(byte[] theBytes, Charset theCharset) {
049                StringBuilder b = new StringBuilder();
050
051                try {
052                        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(theBytes), theCharset);
053                        while (reader.ready()) {
054                                char read = (char) reader.read();
055                                if (read < 32) {
056                                        b.append('[').append((int) read).append(']');
057                                } else {
058                                        b.append(read);
059                                }
060                        }
061                } catch (Exception e) {
062                        b.append("ERROR: ").append(e.toString());
063                }
064                return b.toString();
065        }
066
067}