summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-07-28 02:31:12 (EDT)
committerEike Stepper2007-07-28 02:31:12 (EDT)
commitd3ead5b0db6802d075881e8cf06fd2155c8920ac (patch)
tree4032d82bf28275a6882a4ec44fa8a69787801841
parente596a724b0786a9fea76342f595c7f411e590e05 (diff)
downloadcdo-d3ead5b0db6802d075881e8cf06fd2155c8920ac.zip
cdo-d3ead5b0db6802d075881e8cf06fd2155c8920ac.tar.gz
cdo-d3ead5b0db6802d075881e8cf06fd2155c8920ac.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java4
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java156
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/NIOUtil.java62
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/TempUtil.java49
4 files changed, 245 insertions, 26 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java
index b54ed07..5517b1b 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java
@@ -59,7 +59,7 @@ public class Preferences extends Notifier implements OMPreferences
File file = getFile();
if (file.exists())
{
- IOUtil.input(file, new IORunnable<FileInputStream>()
+ IOUtil.safeInput(file, new IORunnable<FileInputStream>()
{
public void run(FileInputStream io) throws IOException
{
@@ -105,7 +105,7 @@ public class Preferences extends Notifier implements OMPreferences
}
else
{
- IOUtil.output(file, new IORunnable<FileOutputStream>()
+ IOUtil.safeOutput(file, new IORunnable<FileOutputStream>()
{
public void run(FileOutputStream io) throws IOException
{
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java
index f8752e2..fc6e517 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java
@@ -10,6 +10,8 @@
**************************************************************************/
package org.eclipse.net4j.util.io;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
@@ -18,6 +20,7 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.PrintStream;
/**
@@ -54,7 +57,55 @@ public final class IOUtil
print(t, System.err);
}
- public static IOException closeSilent(Closeable closeable)
+ public static FileInputStream openInputStream(File file) throws IORuntimeException
+ {
+ try
+ {
+ return new FileInputStream(file);
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public static FileOutputStream openOutputStream(File file) throws IORuntimeException
+ {
+ try
+ {
+ return new FileOutputStream(file);
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public static FileReader openReader(File file) throws IORuntimeException
+ {
+ try
+ {
+ return new FileReader(file);
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public static FileWriter openWriter(File file) throws IORuntimeException
+ {
+ try
+ {
+ return new FileWriter(file);
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public static Exception closeSilent(Closeable closeable)
{
try
{
@@ -65,7 +116,7 @@ public final class IOUtil
return null;
}
- catch (IOException ex)
+ catch (Exception ex)
{
return ex;
}
@@ -86,72 +137,129 @@ public final class IOUtil
}
}
- public static void run(Closeable io, IORunnable runnable) throws IORuntimeException
+ public static void copy(InputStream input, OutputStream output, byte buffer[]) throws IORuntimeException
{
try
{
- runnable.run(io);
+ int n = 0;
+ while ((n = input.read(buffer)) != -1)
+ {
+ output.write(buffer, 0, n);
+ }
}
catch (IOException ex)
{
throw new IORuntimeException(ex);
}
- finally
- {
- close(io);
- }
}
- public static void read(File file, IORunnable<FileReader> runnable) throws IORuntimeException
+ public static void copy(InputStream input, OutputStream output, int bufferSize) throws IORuntimeException
{
+ copy(input, output, new byte[bufferSize]);
+ }
+
+ public static void copy(InputStream input, OutputStream output) throws IORuntimeException
+ {
+ copy(input, output, 4096);
+ }
+
+ /**
+ * @see NIOUtil#copyFile(File, File)
+ */
+ public static void copyFile(File source, File target) throws IORuntimeException
+ {
+ FileInputStream input = null;
+ FileOutputStream output = null;
+
try
{
- FileReader io = new FileReader(file);
- run(io, runnable);
+ input = new FileInputStream(source);
+ output = new FileOutputStream(target);
+
+ copy(input, output);
}
catch (IOException ex)
{
throw new IORuntimeException(ex);
}
+ finally
+ {
+ closeSilent(input);
+ closeSilent(output);
+ }
}
- public static void write(File file, IORunnable<FileWriter> runnable) throws IORuntimeException
+ public static byte[] readFile(File file) throws IORuntimeException
{
+ if (file.length() > Integer.MAX_VALUE)
+ {
+ throw new IllegalArgumentException("File too long: " + file.length());
+ }
+
+ int size = (int)file.length();
+ FileInputStream input = openInputStream(file);
+
try
{
- FileWriter io = new FileWriter(file);
- run(io, runnable);
+ ByteArrayOutputStream output = new ByteArrayOutputStream(size);
+ copy(input, output);
+ return output.toByteArray();
}
- catch (IOException ex)
+ finally
{
- throw new IORuntimeException(ex);
+ closeSilent(input);
}
}
- public static void input(File file, IORunnable<FileInputStream> runnable) throws IORuntimeException
+ public static void writeFile(File file, byte[] bytes) throws IORuntimeException
{
+ FileOutputStream output = openOutputStream(file);
+
try
{
- FileInputStream io = new FileInputStream(file);
- run(io, runnable);
+ ByteArrayInputStream input = new ByteArrayInputStream(bytes);
+ copy(input, output);
}
- catch (IOException ex)
+ finally
{
- throw new IORuntimeException(ex);
+ closeSilent(output);
}
}
- public static void output(File file, IORunnable<FileOutputStream> runnable) throws IORuntimeException
+ public static void safeRun(Closeable io, IORunnable runnable) throws IORuntimeException
{
try
{
- FileOutputStream io = new FileOutputStream(file);
- run(io, runnable);
+ runnable.run(io);
}
catch (IOException ex)
{
throw new IORuntimeException(ex);
}
+ finally
+ {
+ close(io);
+ }
+ }
+
+ public static void safeInput(File file, IORunnable<FileInputStream> runnable) throws IORuntimeException
+ {
+ safeRun(openInputStream(file), runnable);
+ }
+
+ public static void safeOutput(File file, IORunnable<FileOutputStream> runnable) throws IORuntimeException
+ {
+ safeRun(openOutputStream(file), runnable);
+ }
+
+ public static void safeRead(File file, IORunnable<FileReader> runnable) throws IORuntimeException
+ {
+ safeRun(openReader(file), runnable);
+ }
+
+ public static void safeWrite(File file, IORunnable<FileWriter> runnable) throws IORuntimeException
+ {
+ safeRun(openWriter(file), runnable);
}
public static boolean equals(InputStream stream1, InputStream stream2) throws IORuntimeException
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/NIOUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/NIOUtil.java
new file mode 100644
index 0000000..1de881a
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/NIOUtil.java
@@ -0,0 +1,62 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.io;
+
+import org.eclipse.net4j.util.ImplementationError;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+/**
+ * @author Eike Stepper
+ */
+public final class NIOUtil
+{
+ private NIOUtil()
+ {
+ }
+
+ public static void copyFile(File source, File target)
+ {
+ FileChannel sourceChannel = null;
+ FileChannel targetChannel = null;
+
+ try
+ {
+ if (!target.exists())
+ {
+ target.createNewFile();
+ }
+
+ sourceChannel = new FileInputStream(source).getChannel();
+ targetChannel = new FileOutputStream(target).getChannel();
+
+ long size = sourceChannel.size();
+ long transfered = sourceChannel.transferTo(0, size, targetChannel);
+ if (transfered != size)
+ {
+ throw new ImplementationError("Seems as if a loop must be implemented here");
+ }
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ finally
+ {
+ IOUtil.closeSilent(sourceChannel);
+ IOUtil.closeSilent(targetChannel);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/TempUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/TempUtil.java
new file mode 100644
index 0000000..4f82c2a
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/TempUtil.java
@@ -0,0 +1,49 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.io;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public final class TempUtil
+{
+ private TempUtil()
+ {
+ }
+
+ public static File createTempFolder() throws IOException
+ {
+ return createTempFolder("tmp");
+ }
+
+ public static File createTempFolder(String prefix) throws IOException
+ {
+ return createTempFolder(prefix, "");
+ }
+
+ public static File createTempFolder(String prefix, String suffix) throws IOException
+ {
+ return createTempFolder(prefix, suffix, null);
+ }
+
+ public static File createTempFolder(String prefix, String suffix, File directory) throws IOException
+ {
+ File tmp = File.createTempFile(prefix, suffix, directory);
+ String tmpPath = tmp.getAbsolutePath();
+ tmp.delete();
+ tmp = new File(tmpPath);
+ tmp.mkdirs();
+ return tmp;
+ }
+}