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;
* 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 {
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.Normalizer;
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();
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
return input;
+ } catch (UnsupportedEncodingException e) {
+ return input;
}
}