svn commit: r891068 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r891068 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java

doogie-3
Author: doogie
Date: Tue Dec 15 23:23:15 2009
New Revision: 891068

URL: http://svn.apache.org/viewvc?rev=891068&view=rev
Log:
Utility methods for reading strings from streams and readers.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java?rev=891068&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java Tue Dec 15 23:23:15 2009
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.base.util;
+
+import java.io.BufferedReader;
+import java.io.Reader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+
+public final class UtilIO {
+    public static final String module = UtilIO.class.getName();
+
+    public static final String readString(InputStream stream) throws IOException {
+        StringBuilder buf = new StringBuilder();
+        BufferedReader br = null;
+        try {
+            br = new BufferedReader(new InputStreamReader(stream));
+
+            String str;
+            while ((str = br.readLine()) != null) {
+                buf.append(str);
+                buf.append(System.getProperty("line.separator"));
+            }
+        } finally {
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    Debug.logError(e, "Error closing after reading text: " + e.toString(), module);
+                }
+            }
+        }
+        return buf.toString();
+    }
+
+    public static final String readString(Reader reader) throws IOException {
+        StringBuilder buf = new StringBuilder();
+        BufferedReader br = null;
+        try {
+            br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
+
+            String str;
+            while ((str = br.readLine()) != null) {
+                buf.append(str);
+                buf.append(System.getProperty("line.separator"));
+            }
+        } finally {
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    Debug.logError(e, "Error closing after reading text: " + e.toString(), module);
+                }
+            }
+        }
+        return buf.toString();
+    }
+}