X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FStringUtils.java;h=8717364c72ce055352aa810925bdd662c17ba458;hb=f8147a0ee57317e96d9ff0bf19573f7168d0354c;hp=82bdcd2f99a6e868bc249c4e1e37eb0ca2333a56;hpb=7194ac50b29064a013f177fc9cfc5aaa131a8ec4;p=fanfix.git 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. *