From: Niki Roo Date: Wed, 12 Jul 2017 20:08:43 +0000 (+0200) Subject: VERSION 2.1.0: new options on IOUtils X-Git-Tag: nikiroo-utils-2.2.0~2 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=3f8349b761d29cd3d5381f076d706014574eeb43;p=nikiroo-utils.git VERSION 2.1.0: new options on IOUtils --- diff --git a/VERSION b/VERSION index 227cea2..7ec1d6d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.0 +2.1.0 diff --git a/changelog.md b/changelog.md index 68ef60c..eddc40c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # nikiroo-utils +## Version 2.1.0 + +- Better IOUtils + ## Version 2.0.0 - API change diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index 83dcd50..35481b2 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -4,10 +4,10 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -173,12 +173,34 @@ public class IOUtils { * in case of I/O error */ public static String readSmallFile(File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(file)); + InputStream stream = new FileInputStream(file); + try { + return readSmallStream(stream); + } finally { + stream.close(); + } + } + + /** + * Read the whole {@link InputStream} content into a {@link String}. + * + * @param stream + * the {@link InputStream} + * + * @return the content + * + * @throws IOException + * in case of I/O error + */ + public static String readSmallStream(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader( + new InputStreamReader(stream)); try { StringBuilder builder = new StringBuilder(); for (String line = reader.readLine(); line != null; line = reader .readLine()) { builder.append(line); + builder.append("\n"); } return builder.toString(); } finally { diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index a50ec28..0ceb673 100644 --- a/src/be/nikiroo/utils/StringUtils.java +++ b/src/be/nikiroo/utils/StringUtils.java @@ -2,6 +2,7 @@ package be.nikiroo.utils; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.Normalizer; @@ -205,7 +206,7 @@ public class StringUtils { static public String getMd5Hash(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); - md.update(input.getBytes()); + md.update(input.getBytes("UTF-8")); byte byteData[] = md.digest(); StringBuffer hexString = new StringBuffer(); @@ -219,6 +220,8 @@ public class StringUtils { return hexString.toString(); } catch (NoSuchAlgorithmException e) { return input; + } catch (UnsupportedEncodingException e) { + return input; } }