From 5ec57813ec28b75f654c7a0af82e6c0e522de9d9 Mon Sep 17 00:00:00 2001
From: Rainer Klute Converts a string into its hexadecimal notation. FIXME: If this method is called frequently,
- * it should directly implement the algorithm in the called method
- * in order to avoid creating a string instance.
null
*/
diff --git a/src/java/org/apache/poi/hpsf/VariantSupport.java b/src/java/org/apache/poi/hpsf/VariantSupport.java
index 29360420dc..d85a588f33 100644
--- a/src/java/org/apache/poi/hpsf/VariantSupport.java
+++ b/src/java/org/apache/poi/hpsf/VariantSupport.java
@@ -230,7 +230,6 @@ public class VariantSupport extends Variant
final int first = o1 + LittleEndian.INT_SIZE;
long last = first + LittleEndian.getUInt(src, o1) - 1;
o1 += LittleEndian.INT_SIZE;
- final int rawLength = (int) (last - first + 1);
while (src[(int) last] == 0 && first <= last)
last--;
final int l = (int) (last - first + 1);
@@ -454,13 +453,6 @@ public class VariantSupport extends Variant
}
}
- /* Add 0x00 characters to write a multiple of four bytes: */
- // FIXME (1) Try this!
-// while (length % 4 != 0)
-// {
-// out.write(0);
-// length++;
-// }
return length;
}
diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java
index 95b78ea66b..609f0358af 100644
--- a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java
+++ b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java
@@ -65,6 +65,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -402,7 +403,6 @@ public class TestWrite extends TestCase
{
Throwable t = null;
final int codepage = -1;
- /* FIXME (2): Add tests for various codepages! */
try
{
check(Variant.VT_EMPTY, null, codepage);
@@ -414,8 +414,8 @@ public class TestWrite extends TestCase
check(Variant.VT_CF, new byte[]{0, 1, 2, 3}, codepage);
check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4}, codepage);
check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5}, codepage);
- check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- codepage);
+ check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6}, codepage);
+ check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, codepage);
check(Variant.VT_I2, new Integer(27), codepage);
check(Variant.VT_I4, new Long(28), codepage);
check(Variant.VT_FILETIME, new Date(), codepage);
@@ -445,21 +445,80 @@ public class TestWrite extends TestCase
t = ex;
}
if (t != null)
+ fail(org.apache.poi.hpsf.Util.toString(t));
+ }
+
+
+
+ /**
+ * Writes and reads back strings using several different codepages and + * checks whether the stuff that has been read back equals the stuff that + * was written.
+ */ + public void testCodepages() + { + Throwable t = null; + final int[] validCodepages = new int[] {-1, 1252, 1200, 65001}; + for (int i = 0; i < validCodepages.length; i++) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - t.printStackTrace(pw); - pw.close(); + int codepage = validCodepages[i]; + try + { + check(Variant.VT_LPSTR, "", codepage); + check(Variant.VT_LPSTR, "ä", codepage); + check(Variant.VT_LPSTR, "äö", codepage); + check(Variant.VT_LPSTR, "äöü", codepage); + check(Variant.VT_LPSTR, "äöüÄ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜß", codepage); + check(Variant.VT_LPSTR, "\u79D1\u5B78", codepage); + } + catch (Exception ex) + { + t = ex; + } + catch (Error ex) + { + t = ex; + } + if (t != null) + fail(org.apache.poi.hpsf.Util.toString(t)); + } + + final int[] invalidCodepages = new int[] {0, 1, 2, 4711, 815}; + for (int i = 0; i < invalidCodepages.length; i++) + { + int codepage = invalidCodepages[i]; try { - sw.close(); + check(Variant.VT_LPSTR, "", codepage); + check(Variant.VT_LPSTR, "ä", codepage); + check(Variant.VT_LPSTR, "äö", codepage); + check(Variant.VT_LPSTR, "äöü", codepage); + check(Variant.VT_LPSTR, "äöüÄ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜß", codepage); + fail("UnsupportedEncodingException for codepage " + codepage + + " expected."); } - catch (IOException ex2) + catch (UnsupportedEncodingException ex) { - t.printStackTrace(); + /* This is the expected behaviour. */ } - fail(sw.toString()); + catch (Exception ex) + { + t = ex; + } + catch (Error ex) + { + t = ex; + } + if (t != null) + fail(org.apache.poi.hpsf.Util.toString(t)); } + } @@ -482,11 +541,9 @@ public class TestWrite extends TestCase final byte[] b = out.toByteArray(); final Object objRead = VariantSupport.read(b, 0, b.length + LittleEndian.INT_SIZE, - variantType, -1); + variantType, codepage); if (objRead instanceof byte[]) { -// final int diff = diff(org.apache.poi.hpsf.Util.pad4 -// ((byte[]) value), (byte[]) objRead); final int diff = diff((byte[]) value, (byte[]) objRead); if (diff >= 0) fail("Byte arrays are different. First different byte is at " + -- 2.39.5