From 3f8349b761d29cd3d5381f076d706014574eeb43 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 12 Jul 2017 22:08:43 +0200 Subject: [PATCH] VERSION 2.1.0: new options on IOUtils --- VERSION | 2 +- changelog.md | 4 ++++ src/be/nikiroo/utils/IOUtils.java | 26 ++++++++++++++++++++++++-- src/be/nikiroo/utils/StringUtils.java | 5 ++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index 227cea21..7ec1d6db 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.0 +2.1.0 diff --git a/changelog.md b/changelog.md index 68ef60cc..eddc40c5 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 83dcd502..35481b2c 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 a50ec28f..0ceb6731 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; } } -- 2.27.0