diff --git a/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java b/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java index 8304eb0ee..3fbbee12a 100755 --- a/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java +++ b/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java @@ -17,8 +17,6 @@ package org.apache.poi.hssf.record.constant; -import org.apache.poi.hssf.record.UnicodeString; -import org.apache.poi.hssf.record.UnicodeString.UnicodeRecordStats; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutput; import org.apache.poi.util.StringUtil; @@ -65,7 +63,7 @@ public final class ConstantValueParser { case TYPE_NUMBER: return new Double(in.readDouble()); case TYPE_STRING: - return new UnicodeString(StringUtil.readUnicodeString(in)); + return StringUtil.readUnicodeString(in); case TYPE_BOOLEAN: return readBoolean(in); case TYPE_ERROR_CODE: @@ -111,10 +109,8 @@ public final class ConstantValueParser { if(cls == Boolean.class || cls == Double.class || cls == ErrorConstant.class) { return 8; } - UnicodeString strVal = (UnicodeString)object; - UnicodeRecordStats urs = new UnicodeRecordStats(); - strVal.getRecordSize(urs); - return urs.recordSize; + String strVal = (String)object; + return StringUtil.getEncodedSize(strVal); } public static void encode(LittleEndianOutput out, Object[] values) { @@ -142,10 +138,10 @@ public final class ConstantValueParser { out.writeDouble(dVal.doubleValue()); return; } - if (value instanceof UnicodeString) { - UnicodeString usVal = (UnicodeString) value; + if (value instanceof String) { + String val = (String) value; out.writeByte(TYPE_STRING); - StringUtil.writeUnicodeString(out, usVal.getString()); + StringUtil.writeUnicodeString(out, val); return; } if (value instanceof ErrorConstant) { diff --git a/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java b/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java index 3fcc23eda..7d4f65acd 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java @@ -206,8 +206,8 @@ public final class ArrayPtg extends Ptg { if (o == null) { throw new RuntimeException("Array item cannot be null"); } - if (o instanceof UnicodeString) { - return "\"" + ((UnicodeString)o).getString() + "\""; + if (o instanceof String) { + return "\"" + (String)o + "\""; } if (o instanceof Double) { return ((Double)o).toString(); diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index 8a57d2340..263f45e1e 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -162,6 +162,15 @@ public class StringUtil { } } + /** + * @return the number of bytes that would be written by {@link #writeUnicodeString(LittleEndianOutput, String)} + */ + public static int getEncodedSize(String value) { + int result = 2 + 1; + result += value.length() * (StringUtil.hasMultibyte(value) ? 2 : 1); + return result; + } + /** * Takes a unicode (java) string, and returns it as 8 bit data (in ISO-8859-1 * codepage). diff --git a/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java b/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java index a72d0e1ae..6ed2c6245 100644 --- a/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java +++ b/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java @@ -22,7 +22,6 @@ import java.util.Arrays; import junit.framework.TestCase; import org.apache.poi.hssf.record.TestcaseRecordInputStream; -import org.apache.poi.hssf.record.UnicodeString; import org.apache.poi.hssf.usermodel.HSSFErrorConstants; import org.apache.poi.util.HexRead; import org.apache.poi.util.LittleEndianByteArrayOutputStream; @@ -36,7 +35,7 @@ public final class TestConstantValueParser extends TestCase { Boolean.TRUE, null, new Double(1.1), - new UnicodeString("Sample text"), + "Sample text", ErrorConstant.valueOf(HSSFErrorConstants.ERROR_DIV_0), }; private static final byte[] SAMPLE_ENCODING = HexRead.readFromString( diff --git a/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java b/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java index 94a16b382..5dab4cca1 100644 --- a/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java +++ b/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java @@ -66,10 +66,10 @@ public final class TestArrayPtg extends TestCase { assertEquals(Boolean.TRUE, values[0][0]); - assertEquals(new UnicodeString("ABCD"), values[0][1]); + assertEquals("ABCD", values[0][1]); assertEquals(new Double(0), values[1][0]); assertEquals(Boolean.FALSE, values[1][1]); - assertEquals(new UnicodeString("FG"), values[1][2]); + assertEquals("FG", values[1][2]); byte[] outBuf = new byte[ENCODED_CONSTANT_DATA.length]; ptg.writeTokenValueBytes(new LittleEndianByteArrayOutputStream(outBuf, 0));