You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2012/11/04 17:25:32 UTC

svn commit: r1405581 - in /openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common: Files.java IO.java

Author: dblevins
Date: Sun Nov  4 16:25:31 2012
New Revision: 1405581

URL: http://svn.apache.org/viewvc?rev=1405581&view=rev
Log:
Handful of useful IO and Files util methods for properties, paths and more

Modified:
    openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Files.java
    openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/IO.java

Modified: openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Files.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Files.java?rev=1405581&r1=1405580&r2=1405581&view=diff
==============================================================================
--- openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Files.java (original)
+++ openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Files.java Sun Nov  4 16:25:31 2012
@@ -26,6 +26,27 @@ import java.util.List;
  */
 public class Files {
 
+    public static File path(String... parts) {
+        File dir = null;
+        for (String part : parts) {
+            if (dir == null) {
+                dir = new File(part);
+            } else {
+                dir = new File(dir, part);
+            }
+        }
+
+        return dir;
+    }
+
+    public static File path(File dir, String... parts) {
+        for (String part : parts) {
+            dir = new File(dir, part);
+        }
+
+        return dir;
+    }
+
     public static File createTempDir() throws IOException {
         return createTempDir("tomee", ".conf");
     }

Modified: openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/IO.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/IO.java?rev=1405581&r1=1405580&r2=1405581&view=diff
==============================================================================
--- openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/IO.java (original)
+++ openejb/trunk/openejb/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/IO.java Sun Nov  4 16:25:31 2012
@@ -21,6 +21,7 @@ import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
@@ -35,6 +36,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.URL;
+import java.util.Properties;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
@@ -43,6 +45,63 @@ import java.util.zip.ZipOutputStream;
  */
 public class IO {
 
+    public static Properties readProperties(URL resource) throws IOException {
+        return readProperties(resource, new Properties());
+    }
+
+    public static Properties readProperties(URL resource, Properties properties) throws IOException {
+        return readProperties(read(resource), properties);
+    }
+
+    public static Properties readProperties(final File resource) throws IOException {
+        return readProperties(resource, new Properties());
+    }
+
+    public static Properties readProperties(File resource, Properties properties) throws IOException {
+        return readProperties(read(resource), properties);
+    }
+
+    public static Properties writeProperties(File resource, Properties properties) throws IOException {
+        return writeProperties(write(resource), properties);
+    }
+
+    /**
+     * Reads and closes the input stream
+     * @param in
+     * @param properties
+     * @return
+     * @throws IOException
+     */
+    public static Properties readProperties(InputStream in, Properties properties) throws IOException {
+        if (in == null) throw new NullPointerException("InputStream is null");
+        if (properties == null) throw new NullPointerException("Properties is null");
+        try {
+            properties.load(in);
+        } finally{
+            close(in);
+        }
+        return properties;
+    }
+
+
+    /**
+     * @param outputStream
+     * @param properties
+     * @return
+     * @throws IOException
+     */
+    public static Properties writeProperties(OutputStream outputStream, Properties properties) throws IOException {
+        if (outputStream == null) throw new NullPointerException("OutputStream is null");
+        if (properties == null) throw new NullPointerException("Properties is null");
+        try {
+            properties.store(outputStream, "");
+        } finally{
+            close(outputStream);
+        }
+        return properties;
+    }
+
+
     public static String readString(URL url) throws IOException {
         final InputStream in = url.openStream();
         try {
@@ -74,9 +133,13 @@ public class IO {
     }
 
     public static String slurp(InputStream in) throws IOException {
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        copy(in, out);
-        return new String(out.toByteArray(), "UTF-8");
+        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        int i = -1;
+        while ((i = in.read()) != -1) {
+            outputStream.write(i);
+        }
+
+        return new String(outputStream.toByteArray(), "UTF-8");
     }
 
     public static String slurp(URL url) throws IOException {
@@ -169,6 +232,24 @@ public class IO {
         return null;
     }
 
+
+    public static void copy(final File from, final File to) throws IOException {
+        final FileOutputStream fos = new FileOutputStream(to);
+        try {
+            copy(from, fos);
+        } finally {
+            close(fos);
+        }
+    }
+
+    public static void copy(byte[] from, File to) throws IOException {
+        copy(new ByteArrayInputStream(from), to);
+    }
+
+    public static void copy(byte[] from, OutputStream to) throws IOException {
+        copy(new ByteArrayInputStream(from), to);
+    }
+
     public static void closeSilently(final Closeable closeable) {
         try {
             close(closeable);
@@ -201,4 +282,13 @@ public class IO {
         final InputStream in = new FileInputStream(source);
         return new BufferedInputStream(in, 32768);
     }
+
+    public static InputStream read(byte[] content) {
+        return new ByteArrayInputStream(content);
+    }
+
+    public static InputStream read(URL url) throws IOException {
+        return url.openStream();
+    }
+
 }