Making use of Brett Flannigans Java 6 compatible solution.

This commit is contained in:
Olof Larsson 2012-08-30 20:11:01 +02:00
parent e8ec7fb852
commit bdb34e6d8e

View File

@ -4,7 +4,6 @@ import java.io.*;
import java.net.URL; import java.net.URL;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
public class DiscUtil public class DiscUtil
{ {
@ -20,12 +19,24 @@ public class DiscUtil
public static byte[] readBytes(File file) throws IOException public static byte[] readBytes(File file) throws IOException
{ {
return Files.readAllBytes(file.toPath()); int length = (int) file.length();
byte[] output = new byte[length];
InputStream in = new FileInputStream(file);
int offset = 0;
// normally it should be able to read the entire file with just a single iteration below, but it depends on the whims of the FileInputStream
while (offset < length)
{
offset += in.read(output, offset, (length - offset));
}
in.close();
return output;
} }
public static void writeBytes(File file, byte[] bytes) throws IOException public static void writeBytes(File file, byte[] bytes) throws IOException
{ {
Files.write(file.toPath(), bytes); FileOutputStream out = new FileOutputStream(file);
out.write(bytes);
out.close();
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -82,8 +93,8 @@ public class DiscUtil
URL url = new URL(urlstring); URL url = new URL(urlstring);
ReadableByteChannel rbc = Channels.newChannel(url.openStream()); ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, 1 << 24); fos.getChannel().transferFrom(rbc, 0, 1 << 24);
return true; return true;
} }
catch (Exception e) catch (Exception e)
{ {
@ -101,19 +112,19 @@ public class DiscUtil
// FILE DELETION // FILE DELETION
// -------------------------------------------- // // -------------------------------------------- //
public static boolean deleteRecursive(File path) throws FileNotFoundException public static boolean deleteRecursive(File path) throws FileNotFoundException
{ {
if ( ! path.exists()) throw new FileNotFoundException(path.getAbsolutePath()); if ( ! path.exists()) throw new FileNotFoundException(path.getAbsolutePath());
boolean ret = true; boolean ret = true;
if (path.isDirectory()) if (path.isDirectory())
{ {
for (File f : path.listFiles()) for (File f : path.listFiles())
{ {
ret = ret && deleteRecursive(f); ret = ret && deleteRecursive(f);
} }
} }
return ret && path.delete(); return ret && path.delete();
} }
// -------------------------------------------- // // -------------------------------------------- //
// UTF8 ENCODE AND DECODE // UTF8 ENCODE AND DECODE
@ -121,7 +132,7 @@ public class DiscUtil
public static byte[] utf8(String string) public static byte[] utf8(String string)
{ {
try try
{ {
return string.getBytes(UTF8); return string.getBytes(UTF8);
} }
@ -134,7 +145,7 @@ public class DiscUtil
public static String utf8(byte[] bytes) public static String utf8(byte[] bytes)
{ {
try try
{ {
return new String(bytes, UTF8); return new String(bytes, UTF8);
} }