diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 8af6deb5a..b1e095675 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 46385 - (also patch 46362) fix serialization of StyleRecord with unicode name 46368 - Fix HSSFRichTextRun and strings longer than 32768 characters Support sheet-level names Fixed XSSFCell to properly handle cell references with column numbers up to XFD diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d939d9011..cc08e74ee 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 46385 - (also patch 46362) fix serialization of StyleRecord with unicode name 46368 - Fix HSSFRichTextRun and strings longer than 32768 characters Support sheet-level names Fixed XSSFCell to properly handle cell references with column numbers up to XFD diff --git a/src/java/org/apache/poi/hssf/record/StandardRecord.java b/src/java/org/apache/poi/hssf/record/StandardRecord.java index b94c3f816..d26f11a10 100644 --- a/src/java/org/apache/poi/hssf/record/StandardRecord.java +++ b/src/java/org/apache/poi/hssf/record/StandardRecord.java @@ -40,7 +40,8 @@ public abstract class StandardRecord extends Record { out.writeShort(dataSize); serialize(out); if (out.getWriteIndex() - offset != recSize) { - throw new IllegalStateException("Incorrect number of bytes written - expected " + throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): " + + "Incorrect number of bytes written - expected " + recSize + " but got " + (out.getWriteIndex() - offset)); } return recSize; diff --git a/src/java/org/apache/poi/hssf/record/StyleRecord.java b/src/java/org/apache/poi/hssf/record/StyleRecord.java index f9541d593..ef2f70361 100644 --- a/src/java/org/apache/poi/hssf/record/StyleRecord.java +++ b/src/java/org/apache/poi/hssf/record/StyleRecord.java @@ -173,7 +173,11 @@ public final class StyleRecord extends StandardRecord { } else { out.writeShort(field_4_name.length()); out.writeByte(field_3_stringHasMultibyte ? 0x01 : 0x00); - StringUtil.putCompressedUnicode(getName(), out); + if (field_3_stringHasMultibyte) { + StringUtil.putUnicodeLE(getName(), out); + } else { + StringUtil.putCompressedUnicode(getName(), out); + } } } diff --git a/src/testcases/org/apache/poi/hssf/record/TestStyleRecord.java b/src/testcases/org/apache/poi/hssf/record/TestStyleRecord.java index bfe11dd3a..cce5dab0a 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestStyleRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestStyleRecord.java @@ -17,17 +17,30 @@ package org.apache.poi.hssf.record; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; + +import org.apache.poi.util.HexRead; + /** - * + * Tests for {@link StyleRecord} */ public final class TestStyleRecord extends TestCase { - public void testUnicodeReadName() { - byte[] data = { - 17, 0, 9, 0, 1, 56, 94, -60, -119, 95, 0, 83, 0, 104, 0, 101, 0, 101, 0, 116, 0, 49, 0, 92, 40, //92, 36 - }; - RecordInputStream in = TestcaseRecordInputStream.create(StyleRecord.sid, data); - StyleRecord sr = new StyleRecord(in); - assertEquals("\u5E38\u89C4_Sheet1", sr.getName()); // "_Sheet1" - } + public void testUnicodeReadName() { + byte[] data = HexRead.readFromString( + "11 00 09 00 01 38 5E C4 89 5F 00 53 00 68 00 65 00 65 00 74 00 31 00"); + RecordInputStream in = TestcaseRecordInputStream.create(StyleRecord.sid, data); + StyleRecord sr = new StyleRecord(in); + assertEquals("\u5E38\u89C4_Sheet1", sr.getName()); // "_Sheet1" + byte[] ser; + try { + ser = sr.serialize(); + } catch (IllegalStateException e) { + if (e.getMessage().equals("Incorrect number of bytes written - expected 27 but got 18")) { + throw new AssertionFailedError("Identified bug 46385"); + } + throw e; + } + TestcaseRecordInputStream.confirmRecordEncoding(StyleRecord.sid, data, ser); + } }