You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2010/02/10 23:43:47 UTC

svn commit: r908705 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: UtilIO.java test/UtilIOTests.java

Author: doogie
Date: Wed Feb 10 22:43:46 2010
New Revision: 908705

URL: http://svn.apache.org/viewvc?rev=908705&view=rev
Log:
Add UtilIO.writeString() variants.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java

Modified: 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=908705&r1=908704&r2=908705&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java Wed Feb 10 22:43:46 2010
@@ -20,10 +20,15 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.Reader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 
@@ -191,4 +196,59 @@
         }
         return sb;
     }
+
+    /** Convert a \n string to a platform encoding.  This uses a default
+     * {@link Charset UTF-8} charset.
+     *
+     * @param file where to write the converted bytes to
+     * @param value the value to write
+     */
+    public static void writeString(File file, String value) throws IOException {
+        writeString(new FileOutputStream(file), UTF8, value);
+    }
+
+    /** Convert a \n string to a platform encoding.  This uses a default
+     * {@link Charset UTF-8} charset.
+     *
+     * @param out where to write the converted bytes to
+     * @param value the value to write
+     */
+    public static void writeString(OutputStream out, String value) throws IOException {
+        writeString(out, UTF8, value);
+    }
+
+    /** Convert a \n string to a platform encoding.  This uses the
+     * specified charset to extract the raw bytes.
+     *
+     * @param out where to write the converted bytes to
+     * @param charset the charset to use to convert the raw bytes
+     * @param value the value to write
+     */
+    public static void writeString(OutputStream out, String charset, String value) throws IOException {
+        writeString(out, Charset.forName(charset), value);
+    }
+
+    /** Convert a \n string to a platform encoding.  This uses the
+     * specified charset to extract the raw bytes.
+     *
+     * @param out where to write the converted bytes to
+     * @param charset the charset to use to convert the raw bytes
+     * @param value the value to write
+     */
+    public static void writeString(OutputStream out, Charset charset, String value) throws IOException {
+        Writer writer = new OutputStreamWriter(out, charset);
+        String nl = System.getProperty("line.separator");
+        int r = 0;
+        while (r < value.length()) {
+            int i = value.indexOf("\n", r);
+            if (i == -1) {
+                break;
+            }
+            writer.write(value.substring(r, i));
+            writer.write(nl);
+            r = i + 1;
+        }
+        writer.write(value.substring(r));
+        writer.close();
+    }
 }

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java?rev=908705&r1=908704&r2=908705&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java Wed Feb 10 22:43:46 2010
@@ -84,4 +84,34 @@
         assertEquals("readString stream UTF-8:" + label, wanted, UtilIO.readString(new ByteArrayInputStream(toRead), "UTF-8"));
         assertEquals("readString stream UTF8:" + label, wanted, UtilIO.readString(new ByteArrayInputStream(toRead), UtilIO.UTF8));
     }
+
+    public void testWriteString() throws Exception {
+        writeStringTest_0("unix line ending", "\n", new byte[] { 0x0A });
+        writeStringTest_0("mac line ending", "\r", new byte[] { 0x0D });
+        writeStringTest_0("windows line ending", "\r\n", new byte[] { 0x0D, 0x0A });
+    }
+
+    private static void writeStringTest_0(String label, String lineSeparator, byte[] extra) throws IOException {
+        String originalLineSeparator = System.getProperty("line.separator");
+        try {
+            System.getProperties().put("line.separator", lineSeparator);
+            writeStringTest_1(label + ":mark", join(trademarkBytes), "\u2122");
+            writeStringTest_1(label + ":mark NL", join(trademarkBytes, extra), "\u2122\n");
+            writeStringTest_1(label + ":NL mark", join(extra, trademarkBytes), "\n\u2122");
+        } finally {
+            System.getProperties().put("line.separator", originalLineSeparator);
+        }
+    }
+
+    private static void writeStringTest_1(String label, byte[] wanted, String toWrite) throws IOException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        UtilIO.writeString(baos, toWrite);
+        assertEquals("writeString default:" + label, wanted, baos.toByteArray());
+        baos = new ByteArrayOutputStream();
+        UtilIO.writeString(baos, "UTF-8", toWrite);
+        assertEquals("writeString UTF-8:" + label, wanted, baos.toByteArray());
+        baos = new ByteArrayOutputStream();
+        UtilIO.writeString(baos, UtilIO.UTF8, toWrite);
+        assertEquals("writeString UTF8:" + label, wanted, baos.toByteArray());
+    }
 }