From 10d31845674599e74f4323c0e863496cdc88108d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 16 May 2009 19:12:30 +0000 Subject: [PATCH] 47179 - Fix string encoding issues with HSMF chunks on non-windows platforms git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@775508 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../poi/hsmf/datatypes/StringChunk.java | 19 ++++++++++++++++++- .../poi/hsmf/model/TestBlankFileRead.java | 6 +++--- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 34b132702..c378bc5a4 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 47179 - Fix string encoding issues with HSMF chunks on non-windows platforms 47183 - Attachment support for HSMF 47154 - Handle the cell format @ as the same as General 47048 - Fixed evaluation of defined names with the 'complex' flag set diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index bfbefcfab..1a00c9ee0 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 47179 - Fix string encoding issues with HSMF chunks on non-windows platforms 47183 - Attachment support for HSMF 47154 - Handle the cell format @ as the same as General 47048 - Fixed evaluation of defined names with the 'complex' flag set diff --git a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java index fe90bd688..ddeb16829 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java @@ -18,6 +18,9 @@ package org.apache.poi.hsmf.datatypes; import java.io.ByteArrayOutputStream; +import java.io.UnsupportedEncodingException; + +import org.apache.poi.hsmf.datatypes.Types; /** * A Chunk made up of a single string. @@ -61,7 +64,21 @@ public class StringChunk extends Chunk { * @see org.apache.poi.hsmf.Chunk.Chunk#setValue(java.io.ByteArrayOutputStream) */ public void setValue(ByteArrayOutputStream value) { - this.value = value.toString().replaceAll("\0", ""); + String tmpValue; + if (type == Types.NEW_STRING) { + try { + tmpValue = new String(value.toByteArray(), "UTF-16LE"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Core encoding not found, JVM broken?", e); + } + } else { + try { + tmpValue = new String(value.toByteArray(), "CP1252"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Core encoding not found, JVM broken?", e); + } + } + this.value = tmpValue.replace("\0", ""); } public String toString() { diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/model/TestBlankFileRead.java b/src/scratchpad/testcases/org/apache/poi/hsmf/model/TestBlankFileRead.java index 314cc506c..5cb99cbf8 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/model/TestBlankFileRead.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/model/TestBlankFileRead.java @@ -68,7 +68,7 @@ public class TestBlankFileRead extends TestCase { String obtained = mapiMessage.getDisplayCC(); String expected = ""; - TestCase.assertEquals(obtained, expected); + TestCase.assertEquals(expected, obtained); } /** @@ -80,7 +80,7 @@ public class TestBlankFileRead extends TestCase { String obtained = mapiMessage.getDisplayTo(); String expected = ""; - TestCase.assertEquals(obtained, expected); + TestCase.assertEquals(expected, obtained); } /** @@ -107,7 +107,7 @@ public class TestBlankFileRead extends TestCase { String obtained = mapiMessage.getDisplayBCC(); String expected = ""; - TestCase.assertEquals(obtained, expected); + TestCase.assertEquals(expected, obtained); }