From a9619a73c04245c77be160e3ed09ac40b959d217 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Tue, 28 Jun 2011 18:27:35 -0500 Subject: [PATCH] some changes to the file reading routine, to speed it up and also to hopefully be more tolerant of user changes to file encoding (intentional or more likely unintentional) from the default UTF-8 encoding --- src/org/mcteam/factions/util/DiscUtil.java | 47 +++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/org/mcteam/factions/util/DiscUtil.java b/src/org/mcteam/factions/util/DiscUtil.java index 4744238f..f8c3864a 100644 --- a/src/org/mcteam/factions/util/DiscUtil.java +++ b/src/org/mcteam/factions/util/DiscUtil.java @@ -3,30 +3,59 @@ package org.mcteam.factions.util; import java.io.*; /** - * Harddisc related methods such as read and write. + * Hard disk related methods such as read and write. */ public class DiscUtil { /** * Convenience function for writing a string to a file. */ public static void write(File file, String content) throws IOException { - BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8")); + BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8")); out.write(content); out.close(); } - + /** * Convenience function for reading a file as a string. */ public static String read(File file) throws IOException { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); - String ret = new String(new byte[0], "UTF-8"); - +// BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); + InputStream inStream = new FileInputStream(file); + BufferedReader in = new BufferedReader(inputStreamToReader(inStream)); + StringBuilder ret = new StringBuilder(); + String line; while ((line = in.readLine()) != null) { - ret += line; + ret.append(line); + } + in.close(); + + return ret.toString(); + } + + /** + * Helper method for determining and using correct encoding when reading data, + * since so many people/text editors seem to mess up the encoding; hopefully this will help + * Adapted from: http://blog.publicobject.com/2010/08/handling-byte-order-mark-in-java.html + */ + public static Reader inputStreamToReader(InputStream in) throws IOException { + if (in.available() < 3) + return new InputStreamReader(in); + int byte1 = in.read(); + int byte2 = in.read(); + if (byte1 == 0xFF && byte2 == 0xFE) { + return new InputStreamReader(in, "UTF-16LE"); + } else if (byte1 == 0xFF && byte2 == 0xFF) { + return new InputStreamReader(in, "UTF-16BE"); + } else { + int byte3 = in.read(); + if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF) { + return new InputStreamReader(in, "UTF-8"); + } else { + byte[] first3 = {(byte)byte1, (byte)byte2, (byte)byte3}; + InputStream firstChars = new ByteArrayInputStream(first3); + return new InputStreamReader(new SequenceInputStream(firstChars, in)); + } } - - return ret; } }