From: Niki Roo Date: Mon, 29 Apr 2019 19:31:52 +0000 (+0200) Subject: All getBytes("UTF-8") -> StringUtils X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=f8147a0ee57317e96d9ff0bf19573f7168d0354c;p=fanfix-jexer.git All getBytes("UTF-8") -> StringUtils --- diff --git a/src/be/nikiroo/utils/CryptUtils.java b/src/be/nikiroo/utils/CryptUtils.java index 72c9c44..9ef9101 100644 --- a/src/be/nikiroo/utils/CryptUtils.java +++ b/src/be/nikiroo/utils/CryptUtils.java @@ -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)); } /** diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index fa18d0d..0004a4e 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -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(); } diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index 82bdcd2..8717364 100644 --- a/src/be/nikiroo/utils/StringUtils.java +++ b/src/be/nikiroo/utils/StringUtils.java @@ -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. * diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 06fb765..6f85173 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -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())); } /** diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index 50d9ffc..25bde24 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -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); } diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java index 2c5d8bf..84433fe 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -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) { diff --git a/src/be/nikiroo/utils/streams/Base64.java b/src/be/nikiroo/utils/streams/Base64.java index 76b4be4..d54794b 100644 --- a/src/be/nikiroo/utils/streams/Base64.java +++ b/src/be/nikiroo/utils/streams/Base64.java @@ -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 @@ -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)); } /** diff --git a/src/be/nikiroo/utils/streams/ReplaceInputStream.java b/src/be/nikiroo/utils/streams/ReplaceInputStream.java index 5332e72..1cc5139 100644 --- a/src/be/nikiroo/utils/streams/ReplaceInputStream.java +++ b/src/be/nikiroo/utils/streams/ReplaceInputStream.java @@ -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)); } /** diff --git a/src/be/nikiroo/utils/streams/ReplaceOutputStream.java b/src/be/nikiroo/utils/streams/ReplaceOutputStream.java index e7e6c9f..c6679cc 100644 --- a/src/be/nikiroo/utils/streams/ReplaceOutputStream.java +++ b/src/be/nikiroo/utils/streams/ReplaceOutputStream.java @@ -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)); } /** diff --git a/src/be/nikiroo/utils/streams/StreamUtils.java b/src/be/nikiroo/utils/streams/StreamUtils.java index 9b9ffe6..dc75090 100644 --- a/src/be/nikiroo/utils/streams/StreamUtils.java +++ b/src/be/nikiroo/utils/streams/StreamUtils.java @@ -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; } }