diff options
Diffstat (limited to 'logo/src/xlogo/utils/Utils.java')
-rw-r--r-- | logo/src/xlogo/utils/Utils.java | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/logo/src/xlogo/utils/Utils.java b/logo/src/xlogo/utils/Utils.java index 612d4b8..991cc8e 100644 --- a/logo/src/xlogo/utils/Utils.java +++ b/logo/src/xlogo/utils/Utils.java @@ -40,13 +40,13 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.nio.charset.Charset; -import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -499,9 +499,36 @@ public class Utils { return readFile(src, Charset.forName("UTF8")); } - public static String readFile(File src, Charset charset) throws IOException { - byte[] encoded = Files.readAllBytes(src.toPath()); - return new String(encoded, charset); + public static String readFile(File file, Charset charset) throws IOException { + InputStream is = new FileInputStream(file); + + // Get the size of the file + long length = file.length(); + + if (length > Integer.MAX_VALUE) { + // File is too large + } + + // Create the byte array to hold the data + byte[] bytes = new byte[(int)length]; + + // Read in the bytes + int offset = 0; + int numRead = 0; + while (offset < bytes.length + && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { + offset += numRead; + } + + // Ensure all the bytes have been read in + if (offset < bytes.length) { + is.close(); + throw new IOException("Could not completely read file "+file.getName()); + } + + // Close the input stream and return bytes + is.close(); + return new String(bytes, charset); } /** |