From b8f9260d23d5c9c29e852b67aa572821606ee1a9 Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Fri, 8 Nov 2002 02:34:08 +0000 Subject: [PATCH] think I may have fixed the encoding thing... maybe..... I hope.. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352907 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/record/UnicodeString.java | 9 ++++++++- src/java/org/apache/poi/util/StringUtil.java | 7 +++++++ .../org/apache/poi/util/TestStringUtil.java | 12 ++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/UnicodeString.java b/src/java/org/apache/poi/hssf/record/UnicodeString.java index af594a667..7e95e7654 100644 --- a/src/java/org/apache/poi/hssf/record/UnicodeString.java +++ b/src/java/org/apache/poi/hssf/record/UnicodeString.java @@ -55,6 +55,8 @@ package org.apache.poi.hssf.record; +import java.io.UnsupportedEncodingException; + import org.apache.poi.util.LittleEndian; import org.apache.poi.util.StringUtil; @@ -153,7 +155,12 @@ public class UnicodeString field_2_optionflags = data[ 2 ]; if ((field_2_optionflags & 1) == 0) { - field_3_string = new String(data, 3, getCharCount()); + try { + field_3_string = new String(data, 3, getCharCount(), + StringUtil.getPreferredEncoding()); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } } else { diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index a8c868b4c..cf313129a 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -75,6 +75,8 @@ import java.text.FieldPosition; */ public class StringUtil { + + private final static String ENCODING="ISO-8859-1"; /** * Constructor for the StringUtil object */ @@ -105,6 +107,7 @@ public class StringUtil { public static String getFromUnicodeHigh(final byte[] string, final int offset, final int len) throws ArrayIndexOutOfBoundsException, IllegalArgumentException { + if ((offset < 0) || (offset >= string.length)) { throw new ArrayIndexOutOfBoundsException("Illegal offset"); } @@ -331,4 +334,8 @@ public class StringUtil { numberFormat.format(number, outputTo, new FieldPosition(0)); return 1; } + + public static String getPreferredEncoding() { + return ENCODING; + } } diff --git a/src/testcases/org/apache/poi/util/TestStringUtil.java b/src/testcases/org/apache/poi/util/TestStringUtil.java index 02b7142a5..6dd6f407a 100644 --- a/src/testcases/org/apache/poi/util/TestStringUtil.java +++ b/src/testcases/org/apache/poi/util/TestStringUtil.java @@ -206,7 +206,7 @@ public class TestStringUtil * Test putCompressedUnicode */ - public void testPutCompressedUnicode() + public void testPutCompressedUnicode() throws Exception { byte[] output = new byte[ 100 ]; byte[] expected_output = @@ -215,7 +215,7 @@ public class TestStringUtil ( byte ) 'o', ( byte ) ' ', ( byte ) 'W', ( byte ) 'o', ( byte ) 'r', ( byte ) 'l', ( byte ) 'd', ( byte ) 0xAE }; - String input = new String(expected_output); + String input = new String(expected_output,StringUtil.getPreferredEncoding()); StringUtil.putCompressedUnicode(input, output, 0); for (int j = 0; j < expected_output.length; j++) @@ -355,4 +355,12 @@ public class TestStringUtil System.out.println("Testing util.StringUtil functionality"); junit.textui.TestRunner.run(TestStringUtil.class); } + /** + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + // System.setProperty() + } + }