From fdc5f9a25cb7a1c3800d16d653fe395cab1b65cb Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Tue, 16 Dec 2008 05:44:32 +0000 Subject: [PATCH] Fix for bug 46385 (by patch 46362 from Matsuyama Tomohiro) fixed serialization of StyleRecord with unicode name git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@726969 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/record/StandardRecord.java | 3 +- .../apache/poi/hssf/record/StyleRecord.java | 6 +++- .../poi/hssf/record/TestStyleRecord.java | 31 +++++++++++++------ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 8af6deb5a8..b1e0956750 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 d939d90113..cc08e74ee1 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 b94c3f8164..d26f11a10b 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 f9541d5939..ef2f703617 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 bfe11dd3a8..cce5dab0a8 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); + } } -- 2.39.5