activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r1087672 - /activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
Date Fri, 01 Apr 2011 12:08:47 GMT
Author: chirino
Date: Fri Apr  1 12:08:47 2011
New Revision: 1087672

URL: http://svn.apache.org/viewvc?rev=1087672&view=rev
Log:
Enriching File with (read|write)_(text|bytes) helper methods.

Modified:
    activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala

Modified: activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala?rev=1087672&r1=1087671&r2=1087672&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
(original)
+++ activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
Fri Apr  1 12:08:47 2011
@@ -74,6 +74,30 @@ object FileSupport {
       }
     }
 
+    def read_text(charset:String="UTF-8"): String = {
+      using(new FileInputStream(self)) { in =>
+        FileSupport.read_text(in, charset)
+      }
+    }
+
+    def read_bytes: Array[Byte] = {
+      using(new FileInputStream(self)) { in =>
+        FileSupport.read_bytes(in)
+      }
+    }
+
+    def write_bytes(data:Array[Byte]):Unit = {
+      using(new FileOutputStream(self)) { out =>
+        FileSupport.write_bytes(out, data)
+      }
+    }
+
+    def write_text(data:String, charset:String="UTF-8"):Unit = {
+      using(new FileOutputStream(self)) { out =>
+        FileSupport.write_text(out, data, charset)
+      }
+    }
+
   }
 
   /**
@@ -100,9 +124,21 @@ object FileSupport {
   }
 
   def read_text(in: InputStream, charset:String="UTF-8"): String = {
+    new String(read_bytes(in), charset)
+  }
+
+  def read_bytes(in: InputStream): Array[Byte] = {
     val out = new ByteArrayOutputStream()
     copy(in, out)
-    new String(out.toByteArray, charset)
+    out.toByteArray
+  }
+
+  def write_text(out: OutputStream, value: String, charset:String="UTF-8"): Unit = {
+    write_bytes(out, value.getBytes(charset))
+  }
+
+  def write_bytes(out: OutputStream, data: Array[Byte]): Unit = {
+    copy(new ByteArrayInputStream(data), out)
   }
 
 }



Mime
View raw message