Author: jheight
Date: Tue Jan 17 01:08:23 2006
New Revision: 369729

URL: http://svn.apache.org/viewcvs?rev=369729&view=rev
Log:
BUG 38230 Fixed. Confirmed that the typecast from byte to char caused errors. 
Added testcase.

Modified:
    jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/RecordInputStream.java
    jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/UnicodeString.java
    
jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java

Modified: 
jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/RecordInputStream.java
URL: 
http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/RecordInputStream.java?rev=369729&r1=369728&r2=369729&view=diff
==============================================================================
--- 
jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/RecordInputStream.java 
(original)
+++ 
jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/RecordInputStream.java 
Tue Jan 17 01:08:23 2006
@@ -249,7 +249,10 @@
     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();    

Modified: 
jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/UnicodeString.java
URL: 
http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/UnicodeString.java?rev=369729&r1=369728&r2=369729&view=diff
==============================================================================
--- jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/UnicodeString.java 
(original)
+++ jakarta/poi/trunk/src/java/org/apache/poi/hssf/record/UnicodeString.java 
Tue Jan 17 01:08:23 2006
@@ -230,19 +230,21 @@
         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();

Modified: 
jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java
URL: 
http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java?rev=369729&r1=369728&r2=369729&view=diff
==============================================================================
--- 
jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java
 (original)
+++ 
jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java
 Tue Jan 17 01:08:23 2006
@@ -91,5 +91,41 @@
         c3 = r.getCell((short)3);
         assertEquals(c3.getCellFormula(), formulaString);
     }
+    
+    /** Tests Bug38230
+     *  That a Umlat is written  and then read back.
+     *  It should have been written as a compressed unicode.
+     * 
+     * 
+     *
+     */
+    public void testUmlatReadWrite() throws Exception {
+        HSSFWorkbook wb = new HSSFWorkbook();
+        
+        //Create a unicode sheet name (euro symbol)
+        HSSFSheet s = wb.createSheet("test");
+        
+        HSSFRow r = s.createRow(0);
+        HSSFCell c = r.createCell((short)1);
+        c.setCellValue(new HSSFRichTextString("\u00e4"));
+        
+        //Confirm that the sring will be compressed
+        
assertEquals(c.getRichStringCellValue().getUnicodeString().getOptionFlags(), 0);
+        
+        File tempFile = TempFile.createTempFile("umlat", "test.xls");
+        FileOutputStream stream = new FileOutputStream(tempFile);
+        wb.write(stream);
+        
+        wb = null;
+        FileInputStream in = new FileInputStream(tempFile);
+        wb = new HSSFWorkbook(in);
+
+        //Test the sheetname
+        s = wb.getSheet("test");
+        assertNotNull(s);
+        
+        c = r.getCell((short)1);
+        assertEquals(c.getRichStringCellValue().getString(), "\u00e4");
+    }    
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/

Reply via email to