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.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  
28  /** Helper class for working with various datatypes. */
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 the contents of a file in to a byte array.
124      * 
125      * @param file file to read
126      * @return the byte contents of the file
127      * 
128      * @throws IOException throw if there is a problem reading the file in to the byte array
129      */
130     public static byte[] fileToByteArray(File file) throws IOException {
131         long numOfBytes = file.length();
132 
133         if (numOfBytes > Integer.MAX_VALUE) {
134             throw new IOException("File is to large to be read in to a byte array");
135         }
136 
137         byte[] bytes = new byte[(int) numOfBytes];
138         FileInputStream ins = new FileInputStream(file);
139         int offset = 0;
140         int numRead = 0;
141         do{
142             numRead = ins.read(bytes, offset, bytes.length - offset);
143             offset += numRead;
144         }while(offset < bytes.length && numRead >= 0);
145 
146         if (offset < bytes.length) {
147             throw new IOException("Could not completely read file " + file.getName());
148         }
149 
150         ins.close();
151         return bytes;
152     }
153 
154     /**
155      * Reads an input stream into a string. The provide stream is <strong>not</strong> closed.
156      * 
157      * @param input the input stream to read
158      * @param decoder character decoder to use, if null, system default character set is used
159      * 
160      * @return the string read from the stream
161      * 
162      * @throws IOException thrown if there is a problem reading from the stream and decoding it
163      */
164     public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException {
165         CharsetDecoder charsetDecoder = decoder;
166         if (decoder == null) {
167             charsetDecoder = Charset.defaultCharset().newDecoder();
168         }
169 
170         StringBuffer stringBuffer = new StringBuffer(2048);
171         BufferedReader reader = new BufferedReader(new InputStreamReader(input, charsetDecoder));
172 
173         char[] chars = new char[1024];
174         while (reader.read(chars) > -1) {
175             stringBuffer.append(String.valueOf(chars));
176             chars = new char[1024];
177         }
178 
179         reader.close();
180 
181         return stringBuffer.toString();
182     }
183 }