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:42:52 UTC
svn commit: r908702 - in /ofbiz/trunk/framework/base: build.xml
src/org/ofbiz/base/util/UtilIO.java src/org/ofbiz/base/util/test/
src/org/ofbiz/base/util/test/UtilIOTests.java testdef/basetests.xml
Author: doogie
Date: Wed Feb 10 22:42:51 2010
New Revision: 908702
URL: http://svn.apache.org/viewvc?rev=908702&view=rev
Log:
UtilIO.readString enhancements.
Added:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java
Modified:
ofbiz/trunk/framework/base/build.xml
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java
ofbiz/trunk/framework/base/testdef/basetests.xml
Modified: ofbiz/trunk/framework/base/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=908702&r1=908701&r2=908702&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/build.xml (original)
+++ ofbiz/trunk/framework/base/build.xml Wed Feb 10 22:42:51 2010
@@ -39,6 +39,7 @@
<filelist id="test.classes" dir="${src.dir}">
<file name="org/ofbiz/base/conversion/test/MiscTests.java"/>
+ <file name="org/ofbiz/base/util/test/UtilIOTests.java"/>
<file name="org/ofbiz/base/test/BaseUnitTests.java"/>
<file name="org/ofbiz/base/util/collections/test/GenericMapTest.java"/>
<file name="org/ofbiz/base/concurrent/test/SyncTTLObjectTest.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=908702&r1=908701&r2=908702&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:42:51 2010
@@ -18,58 +18,177 @@
*******************************************************************************/
package org.ofbiz.base.util;
+import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
public final class UtilIO {
+ public static final Charset UTF8 = Charset.forName("UTF-8");
public static final String module = UtilIO.class.getName();
+ /** Convert a byte array to a string; consistently uses \n line endings
+ * in java. This uses a default {@link Charset UTF-8} charset.
+ *
+ * @param bytes the array of bytes to convert
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(byte[] bytes) throws IOException {
+ return readString(bytes, 0, bytes.length, UTF8);
+ }
+
+ /** Convert a byte array to a string; consistently uses \n line endings
+ * in java. The conversion is limited to the specified offset/length
+ * pair, and uses a default {@link Charset UTF-8} charset.
+ *
+ * @param bytes the array of bytes to convert
+ * @param offset the start of the conversion
+ * @param length how many bytes to convert
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(byte[] bytes, int offset, int length) throws IOException {
+ return readString(bytes, offset, length, UTF8);
+ }
+
+ /** Convert a byte array to a string; consistently uses \n line endings
+ * in java. The conversion is limited to the specified offset/length
+ * pair, and uses the requested charset to decode the bytes.
+ *
+ * @param bytes the array of bytes to convert
+ * @param charset the charset to use to convert the raw bytes
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(byte[] bytes, String charset) throws IOException {
+ return readString(bytes, 0, bytes.length, Charset.forName(charset));
+ }
+
+ /** Convert a byte array to a string; consistently uses \n line
+ * endings in java. The conversion is limited to the specified
+ * offset/length pair, and uses the requested charset to decode the
+ * bytes.
+ *
+ * @param bytes the array of bytes to convert
+ * @param offset the start of the conversion
+ * @param length how many bytes to convert
+ * @param charset the charset to use to convert the raw bytes
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(byte[] bytes, int offset, int length, String charset) throws IOException {
+ return readString(bytes, 0, bytes.length, Charset.forName(charset));
+ }
+
+ /** Convert a byte array to a string, consistently uses \n line
+ * endings in java. The specified {@link Charset charset} is used
+ * to decode the bytes.
+ * @param bytes the array of bytes to convert
+ * @param charset the charset to use to convert the raw bytes
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(byte[] bytes, Charset charset) throws IOException {
+ return readString(bytes, 0, bytes.length, charset);
+ }
+
+ /** Convert a byte array to a string, consistently uses \n line
+ * endings in java. The conversion is limited to the specified
+ * offset/length pair, and uses the requested {@link Charset
+ * charset} to decode the bytes.
+ *
+ * @param bytes the array of bytes to convert
+ * @param offset the start of the conversion
+ * @param length how many bytes to convert
+ * @param charset the charset to use to convert the raw bytes
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(byte[] bytes, int offset, int length, Charset charset) throws IOException {
+ ByteBuffer buf = ByteBuffer.allocate(length);
+ buf.put(bytes, offset, length);
+ buf.flip();
+ return filterLineEndings(new StringBuilder(charset.decode(buf).toString())).toString();
+ }
+
+ /** Convert an {@link InputStream} to a string; consistently uses \n line endings
+ * in java. This uses a default {@link Charset UTF-8} charset.
+ *
+ * @param stream the stream of bytes to convert
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
public static final String readString(InputStream stream) throws IOException {
- StringBuilder buf = new StringBuilder();
- BufferedReader br = null;
- try {
- br = new BufferedReader(new InputStreamReader(stream));
+ return readString(stream, UTF8);
+ }
- 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();
+ /** Convert an {@link InputStream} to a string; consistently uses \n line endings
+ * in java. This uses a default {@link Charset UTF-8} charset.
+ *
+ * @param stream the stream of bytes to convert
+ * @param charset the charset to use to convert the raw bytes
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(InputStream stream, String charset) throws IOException {
+ return readString(stream, Charset.forName(charset));
}
+ /** Convert an {@link InputStream} to a string; consistently uses \n line endings
+ * in java. This uses a default {@link Charset UTF-8} charset.
+ *
+ * @param stream the stream of bytes to convert
+ * @param charset the charset to use to convert the raw bytes
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
+ public static final String readString(InputStream stream, Charset charset) throws IOException {
+ return readString(new InputStreamReader(new BufferedInputStream(stream), charset));
+ }
+
+ /** Convert an {@link Reader} to a string; consistently uses \n line endings
+ * in java.
+ *
+ * @param reader the stream of characters convert
+ * @return the converted string, with platform line endings converted
+ * to \n
+ */
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"));
+ StringBuilder sb = new StringBuilder();
+ char[] buf = new char[4096];
+ int r;
+ while ((r = reader.read(buf, 0, 4096)) != -1) {
+ sb.append(buf, 0, r);
}
+ return filterLineEndings(sb).toString();
} finally {
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- Debug.logError(e, "Error closing after reading text: " + e.toString(), module);
+ try {
+ reader.close();
+ } catch (IOException e) {
+ Debug.logError(e, "Error closing after reading text: " + e.toString(), module);
+ }
+ }
+ }
+
+ private static StringBuilder filterLineEndings(StringBuilder sb) {
+ String nl = System.getProperty("line.separator");
+ if (!nl.equals("\n")) {
+ int r = 0;
+ while (r < sb.length()) {
+ int i = sb.indexOf(nl, r);
+ if (i == -1) {
+ break;
}
+ sb.replace(i, i + nl.length(), "\n");
+ r = i + 1;
}
}
- return buf.toString();
+ return sb;
}
}
Added: 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=908702&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilIOTests.java Wed Feb 10 22:42:51 2010
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ * 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.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.ofbiz.base.util.UtilIO;
+import org.ofbiz.base.test.GenericTestCaseBase;
+
+public class UtilIOTests extends GenericTestCaseBase {
+ private static final byte[] trademarkBytes = new byte[] {
+ (byte) 0xE2, (byte) 0x84, (byte) 0xA2
+ };
+ public UtilIOTests(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testReadString() throws Exception {
+ readStringTest_0("unix line ending", "\n", new byte[] { 0x0A });
+ readStringTest_0("mac line ending", "\r", new byte[] { 0x0D });
+ readStringTest_0("windows line ending", "\r\n", new byte[] { 0x0D, 0x0A });
+ }
+
+ private static byte[] join(byte[]... parts) {
+ int count = 0;
+ for (byte[] part: parts) {
+ count += part.length;
+ }
+ byte[] result = new byte[count];
+ int i = 0;
+ for (byte[] part: parts) {
+ System.arraycopy(part, 0, result, i, part.length);
+ i += part.length;
+ }
+ return result;
+ }
+
+ private static void readStringTest_0(String label, String lineSeparator, byte[] extra) throws IOException {
+ String originalLineSeparator = System.getProperty("line.separator");
+ try {
+ System.getProperties().put("line.separator", lineSeparator);
+ readStringTest_1(label + ":mark", "\u2122", join(trademarkBytes));
+ readStringTest_1(label + ":mark NL", "\u2122\n", join(trademarkBytes, extra));
+ readStringTest_1(label + ":NL mark", "\n\u2122", join(extra, trademarkBytes));
+ } finally {
+ System.getProperties().put("line.separator", originalLineSeparator);
+ }
+ }
+
+ private static void readStringTest_1(String label, String wanted, byte[] toRead) throws IOException {
+ assertEquals("readString bytes default:" + label, wanted, UtilIO.readString(toRead));
+ assertEquals("readString bytes UTF-8:" + label, wanted, UtilIO.readString(toRead, "UTF-8"));
+ assertEquals("readString bytes UTF8:" + label, wanted, UtilIO.readString(toRead, UtilIO.UTF8));
+ assertEquals("readString bytes offset/length default:" + label, wanted, UtilIO.readString(toRead, 0, toRead.length));
+ assertEquals("readString bytes offset/length UTF-8:" + label, wanted, UtilIO.readString(toRead, 0, toRead.length, "UTF-8"));
+ assertEquals("readString bytes offset/length UTF8:" + label, wanted, UtilIO.readString(toRead, 0, toRead.length, UtilIO.UTF8));
+ assertEquals("readString stream default:" + label, wanted, UtilIO.readString(new ByteArrayInputStream(toRead)));
+ 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));
+ }
+}
Modified: ofbiz/trunk/framework/base/testdef/basetests.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/testdef/basetests.xml?rev=908702&r1=908701&r2=908702&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/trunk/framework/base/testdef/basetests.xml Wed Feb 10 22:42:51 2010
@@ -22,6 +22,7 @@
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/test-suite.xsd">
<test-case case-name="basetests">
<junit-test-suite class-name="org.ofbiz.base.conversion.test.MiscTests.java"/>
+ <junit-test-suite class-name="org.ofbiz.base.util.test.UtilIOTests"/>
<junit-test-suite class-name="org.ofbiz.base.test.BaseUnitTests"/>
</test-case>
</test-suite>