VERSION 2.1.0: new options on IOUtils
authorNiki Roo <niki@nikiroo.be>
Wed, 12 Jul 2017 20:08:43 +0000 (22:08 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 12 Jul 2017 20:08:43 +0000 (22:08 +0200)
VERSION
changelog.md
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/StringUtils.java

diff --git a/VERSION b/VERSION
index 227cea215648b1af34a87c9acf5b707fe02d2072..7ec1d6db40877765247db18e7f9a4e36a0def4ad 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.0.0
+2.1.0
index 68ef60cc34f89d54726009141de9e1ec9d161408..eddc40c5b7dccd31974497e3c8d14851c2a86726 100644 (file)
@@ -1,5 +1,9 @@
 # nikiroo-utils
 
+## Version 2.1.0
+
+- Better IOUtils
+
 ## Version 2.0.0
 
 - API change
index 83dcd502b242fb26d88b611b1d7ff4e7d1a30c17..35481b2cf0d1576e68814191296b3d8fdafca25d 100644 (file)
@@ -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 {
index a50ec28f19481363c9c09af476507b4cc5901826..0ceb673104902170a6860294a4aa37b19547a610 100644 (file)
@@ -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;
                }
        }