]> source.dussan.org Git - poi.git/commitdiff
Fix bug #46368 - HSSFRichTextRun and strings longer than 32768 characters
authorNick Burch <nick@apache.org>
Tue, 9 Dec 2008 19:36:53 +0000 (19:36 +0000)
committerNick Burch <nick@apache.org>
Tue, 9 Dec 2008 19:36:53 +0000 (19:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@724848 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/record/UnicodeString.java
src/java/org/apache/poi/hssf/usermodel/HSSFRichTextString.java
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java

index 871d80e0f66df9b77007eef71be34965dbdcf6d9..8af6deb5a8d48fbf26babd619386525a4ea0f666 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.5-beta5" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">46368 - Fix HSSFRichTextRun and strings longer than 32768 characters</action>
            <action dev="POI-DEVELOPERS" type="add">Support sheet-level names</action>
            <action dev="POI-DEVELOPERS" type="fix">Fixed XSSFCell to properly handle cell references with column numbers up to XFD</action>
            <action dev="POI-DEVELOPERS" type="fix">44914 - Fixed warning message "WARN. Unread n bytes of record 0xNN"</action>
index 8d09549af99dbe385bb9e93181da4e72ef4c9ec9..d939d90113401750c8982482caab805659b84c8f 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.5-beta5" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">46368 - Fix HSSFRichTextRun and strings longer than 32768 characters</action>
            <action dev="POI-DEVELOPERS" type="add">Support sheet-level names</action>
            <action dev="POI-DEVELOPERS" type="fix">Fixed XSSFCell to properly handle cell references with column numbers up to XFD</action>
            <action dev="POI-DEVELOPERS" type="fix">44914 - Fixed warning message "WARN. Unread n bytes of record 0xNN"</action>
index fc493d434824c6a6bb38906b126a3fcbe8017bdb..a47cd059869e95225ce14a696234166c211078a2 100644 (file)
@@ -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;
     }
 
index 9d57c99a4308c9c59393683b1868b6cd065509be..35f91eaef5d61f04cf3ecc4b788ba0cb0599949d 100644 (file)
@@ -198,8 +198,7 @@ public class HSSFRichTextString
     /**
      * @return  the number of characters in the text.
      */
-    public int length()
-    {
+    public int length() {
         return string.getCharCount();
     }
 
index 2c28be65016d2c92b93c0a6c553a2a94cf127a23..1766199e45c8fb2927a80212626ab0e1d3195806 100644 (file)
@@ -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<len; j++) {
+                       sb.append("x");
+               }
+               HSSFRichTextString rtr = new HSSFRichTextString(sb.toString());
+               assertEquals(len, rtr.length());
+               c.setCellValue(rtr);
+       }
+       
+       // Save and reload
+       wb = writeOutAndReadBack(wb);
+       s = wb.getSheetAt(0);
+       r = s.getRow(0);
+       for(int i=0; i<15; i++) {
+               int len = 32760 + i;
+               HSSFCell c = r.getCell(i);
+               assertEquals(len, c.getRichStringCellValue().length());
+       }
+    }
 }