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

This commit is contained in:
Brettflan 2011-06-28 18:27:35 -05:00
parent c80f6d803c
commit a9619a73c0
1 changed files with 38 additions and 9 deletions

View File

@ -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;
}
}