From 015af7c1410010ccf98d2320b7e738ebd80a9336 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 9 Dec 2008 19:36:53 +0000 Subject: [PATCH] Fix bug #46368 - HSSFRichTextRun and strings longer than 32768 characters git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@724848 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/record/UnicodeString.java | 24 +++++++++---- .../hssf/usermodel/HSSFRichTextString.java | 3 +- .../apache/poi/hssf/usermodel/TestBugs.java | 35 +++++++++++++++++++ 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 871d80e0f..8af6deb5a 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 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 44914 - Fixed warning message "WARN. Unread n bytes of record 0xNN" diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8d09549af..d939d9011 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 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 44914 - Fixed warning message "WARN. Unread n bytes of record 0xNN" diff --git a/src/java/org/apache/poi/hssf/record/UnicodeString.java b/src/java/org/apache/poi/hssf/record/UnicodeString.java index fc493d434..a47cd0598 100644 --- a/src/java/org/apache/poi/hssf/record/UnicodeString.java +++ b/src/java/org/apache/poi/hssf/record/UnicodeString.java @@ -202,9 +202,9 @@ public final class UnicodeString implements Comparable { boolean isCompressed = ((field_2_optionflags & 1) == 0); if (isCompressed) { - field_3_string = in.readCompressedUnicode(field_1_charCount); + field_3_string = in.readCompressedUnicode(getCharCount()); } else { - field_3_string = in.readUnicodeLEString(field_1_charCount); + field_3_string = in.readUnicodeLEString(getCharCount()); } @@ -226,15 +226,25 @@ public final class UnicodeString implements Comparable { /** - * get the number of characters in the string - * + * get the number of characters in the string, + * as an un-wrapped int * * @return number of characters - * */ + public int getCharCount() { + if(field_1_charCount < 0) { + return field_1_charCount + 65536; + } + return field_1_charCount; + } - public short getCharCount() - { + /** + * get the number of characters in the string, + * wrapped as needed to fit within a short + * + * @return number of characters + */ + public short getCharCountShort() { return field_1_charCount; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRichTextString.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRichTextString.java index 9d57c99a4..35f91eaef 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRichTextString.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRichTextString.java @@ -198,8 +198,7 @@ public class HSSFRichTextString /** * @return the number of characters in the text. */ - public int length() - { + public int length() { return string.getCharCount(); } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 2c28be650..1766199e4 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -35,6 +35,9 @@ import org.apache.poi.hssf.record.NameRecord; import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; import org.apache.poi.hssf.record.formula.DeletedArea3DPtg; import org.apache.poi.hssf.record.formula.Ptg; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.TempFile; @@ -1540,4 +1543,36 @@ public final class TestBugs extends TestCase { HSSFWorkbook wb = openSample("45290.xls"); assertEquals(1, wb.getNumberOfSheets()); } + + /** + * HSSFRichTextString.length() returns negative for really + * long strings + */ + public void test46368() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + HSSFRow r = s.createRow(0); + for(int i=0; i<15; i++) { + int len = 32760 + i; + HSSFCell c = r.createCell(i); + + StringBuffer sb = new StringBuffer(); + for(int j=0; j