New beta herochat integration and fixing the slow load time. This may require Java7.

This commit is contained in:
Olof Larsson
2012-08-26 00:54:15 +02:00
parent b0823dc5a8
commit 48c5131ea4
10 changed files with 687 additions and 94 deletions

View File

@@ -4,31 +4,48 @@ import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
public class DiscUtil
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
private final static String UTF8 = "UTF-8";
// -------------------------------------------- //
// BYTE
// -------------------------------------------- //
public static byte[] readBytes(File file) throws IOException
{
return Files.readAllBytes(file.toPath());
}
public static void writeBytes(File file, byte[] bytes) throws IOException
{
Files.write(file.toPath(), bytes);
}
// -------------------------------------------- //
// STRING
// -------------------------------------------- //
public static void write(File file, String content) throws IOException
{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
out.write(content);
out.close();
writeBytes(file, utf8(content));
}
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");
String line;
while ((line = in.readLine()) != null)
{
ret += line;
}
in.close();
return ret;
return utf8(readBytes(file));
}
// -------------------------------------------- //
// CATCH
// -------------------------------------------- //
public static boolean writeCatch(File file, String content)
{
try
@@ -54,6 +71,10 @@ public class DiscUtil
}
}
// -------------------------------------------- //
// DOWNLOAD
// -------------------------------------------- //
public static boolean downloadUrl(String urlstring, File file)
{
try
@@ -61,8 +82,8 @@ public class DiscUtil
URL url = new URL(urlstring);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
return true;
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
return true;
}
catch (Exception e)
{
@@ -75,4 +96,53 @@ public class DiscUtil
{
return downloadUrl(urlstring, new File(filename));
}
}
// -------------------------------------------- //
// FILE DELETION
// -------------------------------------------- //
public static boolean deleteRecursive(File path) throws FileNotFoundException
{
if ( ! path.exists()) throw new FileNotFoundException(path.getAbsolutePath());
boolean ret = true;
if (path.isDirectory())
{
for (File f : path.listFiles())
{
ret = ret && deleteRecursive(f);
}
}
return ret && path.delete();
}
// -------------------------------------------- //
// UTF8 ENCODE AND DECODE
// -------------------------------------------- //
public static byte[] utf8(String string)
{
try
{
return string.getBytes(UTF8);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
return null;
}
}
public static String utf8(byte[] bytes)
{
try
{
return new String(bytes, UTF8);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
return null;
}
}
}