just check for null as opposed to catching NullPointerException...
authorFrank Maritato <[email protected]>
Thu, 24 Jun 2010 21:50:57 +0000 (24 21:50 +0000)
committerFrank Maritato <[email protected]>
Thu, 24 Jun 2010 21:50:57 +0000 (24 21:50 +0000)
git-svn-id: https://lwes.svn.sourceforge.net/svnroot/lwes/lwes-java/trunk@493 a2f82657-cdd2-4550-bd36-68a8e7111808

src/main/java/org/lwes/util/EncodedString.java

index c6bf99d..e1966fd 100644 (file)
@@ -17,40 +17,39 @@ public class EncodedString {
        private CharacterEncoding myEncoding;
 
        public static String bytesToString(byte[] bytes, CharacterEncoding enc) {
-               String ret = null;
+               if (bytes == null) {
+            return null;
+        }
+
                try {
-                       ret = new String(bytes, enc.getEncodingString());
+                       return new String(bytes, enc.getEncodingString());
                } catch (UnsupportedEncodingException e) {
                        throw new IllegalArgumentException("Unknown Encoding");
-               } catch (NullPointerException e) {
-                       return null;
                }
-               return ret;
        }
 
-       public static String bytesToString(byte[] bytes, int offset,
-                       int length, CharacterEncoding enc) {
-               String ret = null;
+       public static String bytesToString(byte[] bytes, int offset, int length, CharacterEncoding enc) {
+               if (bytes == null) {
+            return null;
+        }
+
                try {
-                       ret = new String(bytes, offset, length, enc.getEncodingString());
+                       return new String(bytes, offset, length, enc.getEncodingString());
                } catch (UnsupportedEncodingException e) {
                        throw new IllegalArgumentException("Unknown Encoding");
-               } catch (NullPointerException e) {
-                       return null;
                }
-               return ret;
        }
 
        public static byte[] getBytes(String string, CharacterEncoding enc) {
-               byte[] ret = null;
+        if (string == null) {
+            return null;
+        }
+
                try {
-                       ret = string.getBytes(enc.getEncodingString());
+                       return string.getBytes(enc.getEncodingString());
                } catch (UnsupportedEncodingException e) {
                        throw new IllegalArgumentException("Unknown Encoding");
-               } catch (NullPointerException e) {
-                       return null;
                }
-               return ret;
        }
 
        public EncodedString(String string, CharacterEncoding enc) {