summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2012-04-07 15:22:50 +0200
committerSven Gothel <[email protected]>2012-04-07 15:22:50 +0200
commit6b5332374ff238b09d44858873d600e4290302f6 (patch)
tree6b2beb6bd5c08ee52fea7e469b52b2704fdbbaec
parent924e2eefd99b2c93d50c19db146253c85e04fe6d (diff)
IOUtil: Add fail-safe getFileOutputStream(..) (inspired by pngj helper class)
-rw-r--r--src/java/com/jogamp/common/util/IOUtil.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java
index 8fa4373..523b6bc 100644
--- a/src/java/com/jogamp/common/util/IOUtil.java
+++ b/src/java/com/jogamp/common/util/IOUtil.java
@@ -37,6 +37,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.AccessControlContext;
+import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
@@ -58,6 +59,19 @@ public class IOUtil {
/** Std. temporary directory property key <code>java.io.tmpdir</code> */
public static final String java_io_tmpdir_propkey = "java.io.tmpdir";
+
+ private static final Constructor<?> fosCtor;
+
+ static {
+ Constructor<?> _fosCtor;
+ try {
+ _fosCtor = ReflectionUtil.getConstructor("java.io.FileOutputStream", new Class<?>[] { File.class }, IOUtil.class.getClassLoader());
+ } catch (Throwable t) {
+ if(DEBUG) { t.printStackTrace(); }
+ _fosCtor = null;
+ }
+ fosCtor = _fosCtor;
+ }
private IOUtil() {}
@@ -278,6 +292,28 @@ public class IOUtil {
return toLowerCase(filename.substring(lastDot + 1));
}
+ /***
+ * @param file
+ * @param allowOverwrite
+ * @return outputStream The resulting output stream
+ * @throws IOException if the file already exists and <code>allowOverwrite</code> is false,
+ * the class <code>java.io.FileOutputStream</code> is not accessible or
+ * the user does not have sufficient rights to access the local filesystem.
+ */
+ public static FileOutputStream getFileOutputStream(File file, boolean allowOverwrite) throws IOException {
+ if(null == fosCtor) {
+ throw new IOException("Cannot open file (" + file + ") for writing, feature not available.");
+ }
+ if (file.exists() && !allowOverwrite) {
+ throw new IOException("File already exists (" + file + ") and overwrite=false");
+ }
+ try {
+ return (FileOutputStream) fosCtor.newInstance(new Object[] { file });
+ } catch (Exception e) {
+ throw new IOException("error opening " + file + " for write. ", e);
+ }
+ }
+
public static String getClassFileName(String clazzBinName) throws IOException {
// or return clazzBinName.replace('.', File.pathSeparatorChar) + ".class"; ?
return clazzBinName.replace('.', '/') + ".class";