dlr 01/12/28 16:47:30
Modified: util/src/java/org/apache/commons/util StringUtils.java
Log:
Integrated patch by Ed Korthof <[EMAIL PROTECTED]> to add some encoding
conversion routines, useful when dealing with internationalization
issues (I18N).
Revision Changes Path
1.22 +66 -4
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java
Index: StringUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -u -r1.21 -r1.22
--- StringUtils.java 18 Dec 2001 18:53:25 -0000 1.21
+++ StringUtils.java 29 Dec 2001 00:47:30 -0000 1.22
@@ -54,9 +54,13 @@
* <http://www.apache.org/>.
*/
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
@@ -67,7 +71,6 @@
// CharSet
import java.util.List;
import java.util.LinkedList;
-//import java.util.Iterator;
/**
@@ -81,11 +84,17 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Greg Coladonato</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Bayard</a>
- * @version $Id: StringUtils.java,v 1.21 2001/12/18 18:53:25 bayard Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Ed Korthof</a>
+ * @version $Id: StringUtils.java,v 1.22 2001/12/29 00:47:30 dlr Exp $
*/
public class StringUtils
{
/**
+ * The size of the buffer to use when working with I/O (4 kB).
+ */
+ public static int CHAR_BUFFER_SIZE = 4 * FileUtils.ONE_KB;
+
+ /**
* Trims text safely, dealing with <code>null</code> references by
* converting them to <code>""</code> (the empty string).
*
@@ -1743,6 +1752,58 @@
}
return text;
}
+
+ /**
+ * Convert a string from unicode to bytes in a native encoding.
+ * The string must be in unicode (as Java always expects this);
+ * {@link #convertNativeToUnicode(String, String)} will convert
+ * strings in native encodings into unicode. This method is
+ * generally used to create a <code>String</code> for use as
+ * output, and is useful when dealing with I18N.
+ *
+ * @param source String the unicode string to convert
+ * @param charset String the name of the charset into which to
+ * convert.
+ * @return The string given represented in the native encoding
+ * specified.
+ * @see #convertNativeToUnicode(String, String)
+ */
+ public static String convertUnicodeToNative(String source, String charset)
+ throws IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ OutputStreamWriter out = new OutputStreamWriter(baos, charset);
+ out.write(source);
+ out.close();
+ return baos.toString();
+ }
+
+ /**
+ * Convert a string from a native encoding to unicode. This
+ * method is generally used to create a <code>String</code> for
+ * use as input, and is useful when dealing with I18N.
+ *
+ * @param input String the input to convert from native encoding
+ * to unicode.
+ * @param charset String the charset from which to convert.
+ * @return The string given represented in unicode rather than the
+ * specified native encoding.
+ */
+ public static String convertNativeToUnicode(String input, String charset)
+ throws IOException
+ {
+ InputStreamReader in = new InputStreamReader
+ (new ByteArrayInputStream(input.getBytes()), charset);
+ StringBuffer output = new StringBuffer();
+ char[] buf = new char[CHAR_BUFFER_SIZE];
+ int count = 0;
+ while ((count = in.read(buf, 0, CHAR_BUFFER_SIZE)) > 0)
+ {
+ output.append(buf, 0, count);
+ }
+ in.close();
+ return output.toString();
+ }
}
@@ -1788,8 +1849,9 @@
/**
* Construct a CharRange over a set of characters.
*
- * @param start String start first character is in this range. inclusive
- * @param close String first character is close character in this range.
inclusive
+ * @param start String start first character is in this range (inclusive).
+ * @param close String first character is close character in this
+ * range (inclusive).
*/
public CharRange(String start, String close) {
this.start = start.charAt(0);
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>