View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.util;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.nio.charset.Charset;
24  import java.nio.charset.CharsetDecoder;
25  
26  /**
27   * Helper class for working with various datatypes.
28   */
29  public final class DatatypeHelper {
30  
31      /** Constructor. */
32      private DatatypeHelper() {
33  
34      }
35  
36      /**
37       * A "safe" null/empty check for strings.
38       * 
39       * @param s The string to check
40       * 
41       * @return true if the string is null or the trimmed string is length zero
42       */
43      public static boolean isEmpty(String s) {
44          if (s != null) {
45              String sTrimmed = s.trim();
46              if (sTrimmed.length() > 0) {
47                  return false;
48              }
49          }
50  
51          return true;
52      }
53  
54      /**
55       * Compares two strings for equality, allowing for nulls.
56       * 
57       * @param <T> type of object to compare
58       * @param s1 The first operand
59       * @param s2 The second operand
60       * 
61       * @return true if both are null or both are non-null and the same strng value
62       */
63      public static <T> boolean safeEquals(T s1, T s2) {
64          if (s1 == null || s2 == null) {
65              return s1 == s2;
66          }
67  
68          return s1.equals(s2);
69      }
70  
71      /**
72       * A safe string trim that handles nulls.
73       * 
74       * @param s the string to trim
75       * 
76       * @return the trimmed string or null if the given string was null
77       */
78      public static String safeTrim(String s) {
79          if (s != null) {
80              return s.trim();
81          }
82  
83          return null;
84      }
85  
86      /**
87       * Removes preceeding or proceeding whitespace from a string or return null if the string is null or of zero length
88       * after trimming (i.e. if the string only contained whitespace).
89       * 
90       * @param s the string to trim
91       * 
92       * @return the trimmed string or null
93       */
94      public static String safeTrimOrNullString(String s) {
95          if (s != null) {
96              String sTrimmed = s.trim();
97              if (sTrimmed.length() > 0) {
98                  return sTrimmed;
99              }
100         }
101 
102         return null;
103     }
104     
105     /**
106      * Converts an integer into an unsigned 4-byte array.
107      * 
108      * @param integer integer to convert
109      * 
110      * @return 4-byte array representing integer
111      */
112     public static byte[] intToByteArray(int integer){
113         byte[] intBytes = new byte[4];
114         intBytes[0]=(byte)((integer & 0xff000000)>>>24);
115         intBytes[1]=(byte)((integer & 0x00ff0000)>>>16);
116         intBytes[2]=(byte)((integer & 0x0000ff00)>>>8);
117         intBytes[3]=(byte)((integer & 0x000000ff));
118 
119         return intBytes;
120     }
121 
122     /**
123      * Reads an input stream into a string. The provide stream is <strong>not</strong> closed.
124      * 
125      * @param input the input stream to read
126      * @param decoder character decoder to use, if null, system default character set is used
127      * 
128      * @return the string read from the stream
129      * 
130      * @throws IOException thrown if there is a problem reading from the stream and decoding it
131      */
132     public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException {
133         CharsetDecoder charsetDecoder = decoder;
134         if (decoder == null) {
135             charsetDecoder = Charset.defaultCharset().newDecoder();
136         }
137 
138         StringBuffer stringBuffer = new StringBuffer(2048);
139         BufferedReader reader = new BufferedReader(new InputStreamReader(input, charsetDecoder));
140 
141         char[] chars = new char[1024];
142         while (reader.read(chars) > -1) {
143             stringBuffer.append(String.valueOf(chars));
144             chars = new char[1024];
145         }
146 
147         reader.close();
148 
149         return stringBuffer.toString();
150     }
151 }