On Thu, Dec 13, 2001 at 10:53:01PM -0800, Ed Korthof wrote:
> I've attached test code as well, to demonstrate this.
<sigh> attachments included, this time.
cheers --
Ed
Index: src/java/org/apache/commons/util/StringUtils.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java,v
retrieving revision 1.18
diff -u -r1.18 StringUtils.java
--- src/java/org/apache/commons/util/StringUtils.java 2001/10/22 18:22:52 1.18
+++ src/java/org/apache/commons/util/StringUtils.java 2001/12/14 07:29:25
@@ -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;
@@ -86,6 +90,10 @@
public class StringUtils
{
/**
+ * The size of the buffer to use when working w/ IO classes.
+ */
+ public static int CHAR_BUFFER_SIZE = 8096;
+ /**
* Trims text safely, dealing with <code>null</code> references by
* converting them to <code>""</code> (the empty string).
*
@@ -1744,6 +1752,49 @@
}
}
return text;
+ }
+
+ /**
+ * Convert a string from unicode to bytes in a native encoding.
+ * The string must be in unicode (as Java always expects this);
+ * convertInputString will convert strings in native encodings into
+ * unicode.
+ * @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
+ */
+ public static String convertOutputString(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.
+ * @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 convertInputString(String input, String charset)
+ throws IOException
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(input.getBytes());
+ InputStreamReader in = new InputStreamReader(bais, charset);
+ StringBuffer output = new StringBuffer();
+ char buff[] = new char[CHAR_BUFFER_SIZE];
+ int count = 0;
+ while ((count = in.read(buff, 0, CHAR_BUFFER_SIZE)) > 0)
+ {
+ output.append(buff, 0, count);
+ }
+ in.close();
+ bais.close();
+ return output.toString();
}
}
import org.apache.commons.util.StringUtils;
public class test
{
public static void main(String argv[]) throws Exception
{
String input = "$B$3$l$OF|K\\8l$N%F%9%H$G$9!#(B";
String unicode = StringUtils.convertInputString(input, "iso-2022-jp");
String iso = StringUtils.convertOutputString(unicode, "iso-2022-jp");
System.err.println(input);
System.err.println(unicode);
System.err.println(iso);
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>