All getBytes("UTF-8") -> StringUtils
authorNiki Roo <niki@nikiroo.be>
Mon, 29 Apr 2019 19:31:52 +0000 (21:31 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 29 Apr 2019 19:31:52 +0000 (21:31 +0200)
src/be/nikiroo/utils/CryptUtils.java
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/StringUtils.java
src/be/nikiroo/utils/serial/SerialUtils.java
src/be/nikiroo/utils/serial/server/ConnectAction.java
src/be/nikiroo/utils/serial/server/ServerBridge.java
src/be/nikiroo/utils/streams/Base64.java
src/be/nikiroo/utils/streams/BufferedInputStream.java
src/be/nikiroo/utils/streams/ReplaceInputStream.java
src/be/nikiroo/utils/streams/ReplaceOutputStream.java
src/be/nikiroo/utils/streams/StreamUtils.java

index 72c9c44b449a5e0ef782cb560dfe044c0c6e8840..9ef91015ad14f0ecc5c3d45f3f242628160146cc 100644 (file)
@@ -293,13 +293,7 @@ public class CryptUtils {
         *             it was)
         */
        public byte[] encrypt(String data) throws SSLException {
-               try {
-                       return encrypt(data.getBytes("UTF8"));
-               } catch (UnsupportedEncodingException e) {
-                       // UTF-8 is required in all confirm JVMs
-                       e.printStackTrace();
-                       return null;
-               }
+               return encrypt(StringUtils.getBytes(data));
        }
 
        /**
@@ -319,13 +313,7 @@ public class CryptUtils {
         *             it was)
         */
        public String encrypt64(String data) throws SSLException {
-               try {
-                       return encrypt64(data.getBytes("UTF8"));
-               } catch (UnsupportedEncodingException e) {
-                       // UTF-8 is required in all confirm JVMs
-                       e.printStackTrace();
-                       return null;
-               }
+               return encrypt64(StringUtils.getBytes(data));
        }
 
        /**
index fa18d0de5c868f411d2d7da88e2cf39fd0e47f23..0004a4e7d79008d303a5c00abf238f1ec65352fc 100644 (file)
@@ -232,7 +232,7 @@ public class IOUtils {
                        throws IOException {
                FileOutputStream out = new FileOutputStream(file);
                try {
-                       out.write(content.getBytes("UTF-8"));
+                       out.write(StringUtils.getBytes(content));
                } finally {
                        out.close();
                }
index 82bdcd2f99a6e868bc249c4e1e37eb0ca2333a56..8717364c72ce055352aa810925bdd662c17ba458 100644 (file)
@@ -432,7 +432,7 @@ public class StringUtils {
        static public String getMd5Hash(String input) {
                try {
                        MessageDigest md = MessageDigest.getInstance("MD5");
-                       md.update(input.getBytes("UTF-8"));
+                       md.update(getBytes(input));
                        byte byteData[] = md.digest();
 
                        StringBuffer hexString = new StringBuffer();
@@ -446,8 +446,6 @@ public class StringUtils {
                        return hexString.toString();
                } catch (NoSuchAlgorithmException e) {
                        return input;
-               } catch (UnsupportedEncodingException e) {
-                       return input;
                }
        }
 
@@ -528,7 +526,7 @@ public class StringUtils {
         */
        public static String zip64s(String data) throws IOException {
                try {
-                       return zip64(data.getBytes("UTF-8"));
+                       return zip64(getBytes(data));
                } catch (UnsupportedEncodingException e) {
                        // All conforming JVM are required to support UTF-8
                        e.printStackTrace();
@@ -601,7 +599,7 @@ public class StringUtils {
         */
        public static byte[] unzip64(String data) throws IOException {
                InputStream in = new Base64InputStream(new ByteArrayInputStream(
-                               data.getBytes("UTF-8")), false);
+                               getBytes(data)), false);
                try {
                        in = new GZIPInputStream(in);
                        return IOUtils.toByteArray(in);
@@ -622,7 +620,7 @@ public class StringUtils {
         *             in case of I/O errors
         */
        public static String base64(String data) throws IOException {
-               return base64(data.getBytes("UTF-8"));
+               return base64(getBytes(data));
        }
 
        /**
@@ -659,7 +657,7 @@ public class StringUtils {
         */
        public static byte[] unbase64(String data) throws IOException {
                Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(
-                               data.getBytes("UTF-8")), false);
+                               getBytes(data)), false);
                try {
                        return IOUtils.toByteArray(in);
                } finally {
@@ -832,6 +830,24 @@ public class StringUtils {
                return count;
        }
 
+       /**
+        * Return the bytes array representation of the given {@link String} in
+        * UTF-8.
+        * 
+        * @param str
+        *            the {@link String} to transform into bytes
+        * @return the content in bytes
+        */
+       static public byte[] getBytes(String str) {
+               try {
+                       return str.getBytes("UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       // All conforming JVM must support UTF-8
+                       e.printStackTrace();
+                       return null;
+               }
+       }
+
        /**
         * The "remove accents" pattern.
         * 
index 06fb765f7d085c8753fafea4abac859ef8ef4e9e..6f851731fd034541b912d71046b0375e8ee7422b 100644 (file)
@@ -5,7 +5,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.NotSerializableException;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -142,7 +141,7 @@ public class SerialUtils {
                                        val = ((URL) value).toString();
                                }
 
-                               out.write(val.getBytes("UTF-8"));
+                               out.write(StringUtils.getBytes(val));
                        }
 
                        @Override
@@ -481,7 +480,7 @@ public class SerialUtils {
                                if (customTypes.containsKey(type)) {
                                        // TODO: we should start with a stream
                                        InputStream streamEncodedValue = new ByteArrayInputStream(
-                                                       encodedValue.getBytes("UTF-8"));
+                                                       StringUtils.getBytes(encodedValue));
                                        try {
                                                return customTypes.get(type).decode(streamEncodedValue);
                                        } finally {
@@ -536,12 +535,7 @@ public class SerialUtils {
         *             in case of I/O error
         */
        static void write(OutputStream out, Object data) throws IOException {
-               try {
-                       out.write(data.toString().getBytes("UTF-8"));
-               } catch (UnsupportedEncodingException e) {
-                       // A conforming JVM is required to support UTF-8
-                       e.printStackTrace();
-               }
+               out.write(StringUtils.getBytes(data.toString()));
        }
 
        /**
index 50d9ffcf61abdc8991094769ab8b6fd3cf3d68aa..25bde2458ee4789d8436918621c791e2085c0ae6 100644 (file)
@@ -9,6 +9,7 @@ import javax.net.ssl.SSLException;
 
 import be.nikiroo.utils.CryptUtils;
 import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.serial.Exporter;
 import be.nikiroo.utils.serial.Importer;
 import be.nikiroo.utils.streams.BufferedOutputStream;
@@ -301,7 +302,7 @@ abstract class ConnectAction {
 
                        try {
                                if (asString) {
-                                       sub.write(data.toString().getBytes("UTF-8"));
+                                       sub.write(StringUtils.getBytes(data.toString()));
                                } else {
                                        new Exporter(sub).append(data);
                                }
index 2c5d8bf1250fc559f507995d73d9711b56fd8640..84433fe9d4f9516c90acaf39d6359fce1e1e25b8 100644 (file)
@@ -234,7 +234,7 @@ public class ServerBridge extends Server {
                                }
 
                                InputStream stream = new ByteArrayInputStream(
-                                               data.getBytes("UTF-8"));
+                                               StringUtils.getBytes(data));
                                try {
                                        Object obj = new Importer().read(stream).getValue();
                                        if (obj == null) {
index 76b4be49f731e9b2ee1387b0a3190fe9555dbd34..d54794b0a4ee2284bb1c691cb329aa52dd65fb47 100644 (file)
@@ -23,6 +23,8 @@ package be.nikiroo.utils.streams;
 
 import java.io.UnsupportedEncodingException;
 
+import be.nikiroo.utils.StringUtils;
+
 /**
  * Utilities for encoding and decoding the Base64 representation of
  * binary data.  See RFCs <a
@@ -120,12 +122,7 @@ class Base64 {
      * incorrect padding
      */
     public static byte[] decode(String str, int flags) {
-       try{
-               return decode(str.getBytes("UTF-8"), flags);
-       } catch (UnsupportedEncodingException e) {
-               // All conforming JVM are expected to support UTF-8
-               return null;
-       }
+               return decode(StringUtils.getBytes(str), flags);
     }
 
     /**
index 42f0d9d24c123be0fad89cce35f43bb9b5831855..da862e3f6802aa22f47586196a680d53d89d302f 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
 
+import be.nikiroo.utils.StringUtils;
+
 /**
  * A simple {@link InputStream} that is buffered with a bytes array.
  * <p>
@@ -142,7 +144,7 @@ public class BufferedInputStream extends InputStream {
         *             greater than the internal buffer
         */
        public boolean startsWiths(String search) throws IOException {
-               return startsWith(search.getBytes("UTF-8"));
+               return startsWith(StringUtils.getBytes(search));
        }
 
        /**
index 5332e72d11651cb7da0588e817bbb10310a39449..1cc5139beae1e012053f62dece957076add4dfee 100644 (file)
@@ -3,6 +3,8 @@ package be.nikiroo.utils.streams;
 import java.io.IOException;
 import java.io.InputStream;
 
+import be.nikiroo.utils.StringUtils;
+
 /**
  * This {@link InputStream} will change some of its content by replacing it with
  * something else.
@@ -40,7 +42,7 @@ public class ReplaceInputStream extends BufferedInputStream {
         *            the {@link String} to replace with
         */
        public ReplaceInputStream(InputStream in, String from, String to) {
-               this(in, StreamUtils.bytes(from), StreamUtils.bytes(to));
+               this(in, StringUtils.getBytes(from), StringUtils.getBytes(to));
        }
 
        /**
@@ -73,7 +75,7 @@ public class ReplaceInputStream extends BufferedInputStream {
         *            the values to replace with
         */
        public ReplaceInputStream(InputStream in, String[] froms, String[] tos) {
-               this(in, StreamUtils.bytes(froms), StreamUtils.bytes(tos));
+               this(in, StreamUtils.getBytes(froms), StreamUtils.getBytes(tos));
        }
 
        /**
index e7e6c9f5a7c03456902e471864722af00478de48..c6679cc3b3eef4ad15e50e022013663c3b75dade 100644 (file)
@@ -3,6 +3,8 @@ package be.nikiroo.utils.streams;
 import java.io.IOException;
 import java.io.OutputStream;
 
+import be.nikiroo.utils.StringUtils;
+
 /**
  * This {@link OutputStream} will change some of its content by replacing it
  * with something else.
@@ -25,7 +27,7 @@ public class ReplaceOutputStream extends BufferedOutputStream {
         *            the {@link String} to replace with
         */
        public ReplaceOutputStream(OutputStream out, String from, String to) {
-               this(out, StreamUtils.bytes(from), StreamUtils.bytes(to));
+               this(out, StringUtils.getBytes(from), StringUtils.getBytes(to));
        }
 
        /**
@@ -58,7 +60,7 @@ public class ReplaceOutputStream extends BufferedOutputStream {
         *            the values to replace with
         */
        public ReplaceOutputStream(OutputStream out, String[] froms, String[] tos) {
-               this(out, StreamUtils.bytes(froms), StreamUtils.bytes(tos));
+               this(out, StreamUtils.getBytes(froms), StreamUtils.getBytes(tos));
        }
 
        /**
index 9b9ffe653c8d7823dbfdc9f279b950711a51e066..dc75090858fb56f36ecc808eb48a9819056871d5 100644 (file)
@@ -1,6 +1,6 @@
 package be.nikiroo.utils.streams;
 
-import java.io.UnsupportedEncodingException;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * Some non-public utilities used in the stream classes.
@@ -50,24 +50,6 @@ class StreamUtils {
                return same;
        }
 
-       /**
-        * Return the bytes array representation of the given {@link String} in
-        * UTF-8.
-        * 
-        * @param str
-        *            the {@link String} to transform into bytes
-        * @return the content in bytes
-        */
-       static public byte[] bytes(String str) {
-               try {
-                       return str.getBytes("UTF-8");
-               } catch (UnsupportedEncodingException e) {
-                       // All conforming JVM must support UTF-8
-                       e.printStackTrace();
-                       return null;
-               }
-       }
-
        /**
         * Return the bytes array representation of the given {@link String} in
         * UTF-8.
@@ -76,17 +58,12 @@ class StreamUtils {
         *            the {@link String}s to transform into bytes
         * @return the content in bytes
         */
-       static public byte[][] bytes(String[] strs) {
-               try {
-                       byte[][] bytes = new byte[strs.length][];
-                       for (int i = 0; i < strs.length; i++) {
-                               bytes[i] = strs[i].getBytes("UTF-8");
-                       }
-                       return bytes;
-               } catch (UnsupportedEncodingException e) {
-                       // All conforming JVM must support UTF-8
-                       e.printStackTrace();
-                       return null;
+       static public byte[][] getBytes(String[] strs) {
+               byte[][] bytes = new byte[strs.length][];
+               for (int i = 0; i < strs.length; i++) {
+                       bytes[i] = StringUtils.getBytes(strs[i]);
                }
+
+               return bytes;
        }
 }