]> source.dussan.org Git - poi.git/commitdiff
BUG 38230 Fixed. Confirmed that the typecast from byte to char caused errors. Added...
authorJason Height <jheight@apache.org>
Tue, 17 Jan 2006 09:08:23 +0000 (09:08 +0000)
committerJason Height <jheight@apache.org>
Tue, 17 Jan 2006 09:08:23 +0000 (09:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@369729 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/RecordInputStream.java
src/java/org/apache/poi/hssf/record/UnicodeString.java
src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java

index 17c98763a428b3b6d18b129e795bfae446bb9f2e..00c9200d5b44706a36e3f932aaf8330a3e113ee5 100755 (executable)
@@ -249,7 +249,10 @@ public class RecordInputStream extends InputStream
     for (int i=0;i<length;i++) {
       if ((remaining() == 0) && (isContinueNext()))
         nextRecord();
-      char ch = (char)readByte();
+      byte b = readByte();
+      //Typecast direct to char from byte with high bit set causes all ones
+      //in the high byte of the char (which is of course incorrect)
+      char ch = (char)( (short)0xff & (short)b );
       buf.append(ch); 
     }
     return buf.toString();    
index e9e0afd7a2b190e9c6505afd8c167313e2145975..ac05276a864528f817ded036796b079d3c477646 100644 (file)
@@ -230,19 +230,21 @@ public class UnicodeString
         in.setAutoContinue(false);
         StringBuffer tmpString = new StringBuffer(field_1_charCount);
         int stringCharCount = field_1_charCount;
-        boolean isUncompressed = ((field_2_optionflags & 1) == 0);
+        boolean isCompressed = ((field_2_optionflags & 1) == 0);
         while (stringCharCount != 0) {
           if (in.remaining() == 0) {
             if (in.isContinueNext()) {
               in.nextRecord();
               //Check if we are now reading, compressed or uncompressed unicode.
               byte optionflags = in.readByte();
-              isUncompressed = ((optionflags & 1) == 0);
+              isCompressed = ((optionflags & 1) == 0);
             } else
               throw new RecordFormatException("Expected continue record.");
           }
-          if (isUncompressed) {
-            char ch = (char)in.readByte();
+          if (isCompressed) {
+            //Typecast direct to char from byte with high bit set causes all ones
+            //in the high byte of the char (which is of course incorrect)
+            char ch = (char)( (short)0xff & (short)in.readByte() );
             tmpString.append(ch);
           } else {
             char ch = (char) in.readShort();
index 5eda00eff85cef48d5724d614ea31a698eae484b..eabf78961f2f9e6f82f311ed1df8eec62792a056 100644 (file)
@@ -91,5 +91,41 @@ public class TestUnicodeWorkbook extends TestCase {
         c3 = r.getCell((short)3);\r
         assertEquals(c3.getCellFormula(), formulaString);\r
     }\r
+    \r
+    /** Tests Bug38230\r
+     *  That a Umlat is written  and then read back.\r
+     *  It should have been written as a compressed unicode.\r
+     * \r
+     * \r
+     *\r
+     */\r
+    public void testUmlatReadWrite() throws Exception {\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        \r
+        //Create a unicode sheet name (euro symbol)\r
+        HSSFSheet s = wb.createSheet("test");\r
+        \r
+        HSSFRow r = s.createRow(0);\r
+        HSSFCell c = r.createCell((short)1);\r
+        c.setCellValue(new HSSFRichTextString("\u00e4"));\r
+        \r
+        //Confirm that the sring will be compressed\r
+        assertEquals(c.getRichStringCellValue().getUnicodeString().getOptionFlags(), 0);\r
+        \r
+        File tempFile = TempFile.createTempFile("umlat", "test.xls");\r
+        FileOutputStream stream = new FileOutputStream(tempFile);\r
+        wb.write(stream);\r
+        \r
+        wb = null;\r
+        FileInputStream in = new FileInputStream(tempFile);\r
+        wb = new HSSFWorkbook(in);\r
+\r
+        //Test the sheetname\r
+        s = wb.getSheet("test");\r
+        assertNotNull(s);\r
+        \r
+        c = r.getCell((short)1);\r
+        assertEquals(c.getRichStringCellValue().getString(), "\u00e4");\r
+    }    \r
 \r
 }\r