[Mulgara-svn] r1938 - trunk/src/jar/util/java/org/mulgara/util
pag at mulgara.org
pag at mulgara.org
Sat Apr 17 16:16:04 UTC 2010
Author: pag
Date: 2010-04-17 09:16:03 -0700 (Sat, 17 Apr 2010)
New Revision: 1938
Modified:
trunk/src/jar/util/java/org/mulgara/util/StringUtil.java
Log:
Added a utility for extracting a data from a Reader into a String
Modified: trunk/src/jar/util/java/org/mulgara/util/StringUtil.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/StringUtil.java 2010-04-16 16:38:10 UTC (rev 1937)
+++ trunk/src/jar/util/java/org/mulgara/util/StringUtil.java 2010-04-17 16:16:03 UTC (rev 1938)
@@ -27,8 +27,10 @@
package org.mulgara.util;
+import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.io.Reader;
import java.util.*;
/**
@@ -57,6 +59,9 @@
/** Quote chars and space char used by {@link #parseString(String)}. */
private final static String SEPARATORS = "\\\"' ";
+ /** Buffer size when reading chars or bytes for a string. */
+ private final static int BUFFER_SIZE = 10240;
+
/**
* Return the first prefix element of a string.
*
@@ -626,6 +631,23 @@
/**
+ * Converts the data available through a {@link java.io.Reader} into a String.
+ * @param reader The {@link java.io.Reader} to be converted.
+ * @return The string that comes from the reader.
+ * @throws IOException If there was an error accessing the Reader.
+ */
+ public static String toString(Reader reader) throws IOException {
+ char[] buffer = new char[BUFFER_SIZE];
+ int len;
+ StringBuilder result = new StringBuilder();
+ while ((len = reader.read(buffer)) >= 0) {
+ result.append(buffer, 0, len);
+ }
+ return result.toString();
+ }
+
+
+ /**
* Quotes the specified chars in the source string by replacing them with the
* corresponding XML entities.
*
More information about the Mulgara-svn
mailing list