From 633084ecfdc6f4cfbbaa0a6e64199916a2ae5540 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Tue, 24 Dec 2013 23:39:09 +0000 Subject: [PATCH] Fix jdk-differences for encryption patch git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1553338 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/crypt/CryptoFunctions.java | 9 ++-- .../poi/poifs/crypt/DataSpaceMapUtils.java | 50 ++++++++++++------- .../poi/poifs/crypt/EncryptionInfo.java | 6 +-- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java b/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java index 75bf1e85ea..33876fc975 100644 --- a/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java +++ b/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java @@ -16,7 +16,7 @@ ==================================================================== */ package org.apache.poi.poifs.crypt; -import java.nio.charset.Charset; +import java.io.UnsupportedEncodingException; import java.security.DigestException; import java.security.GeneralSecurityException; import java.security.MessageDigest; @@ -215,8 +215,11 @@ public class CryptoFunctions { } public static byte[] getUtf16LeString(String str) { - Charset cs = Charset.forName("UTF-16LE"); - return str.getBytes(cs); + try { + return str.getBytes("UTF-16LE"); + } catch (UnsupportedEncodingException e) { + throw new EncryptedDocumentException(e); + } } public static MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm) { diff --git a/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java b/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java index 963151ff97..9e1f231f82 100644 --- a/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java +++ b/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java @@ -18,7 +18,7 @@ package org.apache.poi.poifs.crypt; import java.io.IOException; -import java.nio.charset.Charset; +import java.io.UnsupportedEncodingException; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.poifs.crypt.standard.EncryptionRecord; @@ -295,7 +295,6 @@ public class DataSpaceMapUtils { } public static String readUnicodeLPP4(LittleEndianInput is) { - Charset cs = Charset.forName("UTF-16LE"); int length = is.readInt(); byte data[] = new byte[length]; is.readFully(data); @@ -305,16 +304,23 @@ public class DataSpaceMapUtils { // 2 bytes long, and each byte MUST be 0x00. is.readShort(); } - return new String(data, 0, data.length, cs); + try { + return new String(data, 0, data.length, "UTF-16LE"); + } catch (UnsupportedEncodingException e) { + throw new EncryptedDocumentException(e); + } } public static void writeUnicodeLPP4(LittleEndianOutput os, String str) { - Charset cs = Charset.forName("UTF-16LE"); - byte buf[] = str.getBytes(cs); - os.writeInt(buf.length); - os.write(buf); - if (buf.length%4==2) { - os.writeShort(0); + try { + byte buf[] = str.getBytes("UTF-16LE"); + os.writeInt(buf.length); + os.write(buf); + if (buf.length%4==2) { + os.writeShort(0); + } + } catch (UnsupportedEncodingException e) { + throw new EncryptedDocumentException(e); } } @@ -340,8 +346,11 @@ public class DataSpaceMapUtils { is.readByte(); } } - Charset cs = Charset.forName("UTF-8"); - return new String(data, 0, data.length, cs); + try { + return new String(data, 0, data.length, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new EncryptedDocumentException(e); + } } public static void writeUtf8LPP4(LittleEndianOutput os, String str) { @@ -349,15 +358,18 @@ public class DataSpaceMapUtils { os.writeInt(str == null ? 0 : 4); os.writeInt(0); } else { - Charset cs = Charset.forName("UTF-8"); - byte buf[] = str.getBytes(cs); - os.writeInt(buf.length); - os.write(buf); - int scratchBytes = buf.length%4; - if (scratchBytes > 0) { - for (int i=0; i<(4-scratchBytes); i++) { - os.writeByte(0); + try { + byte buf[] = str.getBytes("UTF-8"); + os.writeInt(buf.length); + os.write(buf); + int scratchBytes = buf.length%4; + if (scratchBytes > 0) { + for (int i=0; i<(4-scratchBytes); i++) { + os.writeByte(0); + } } + } catch (UnsupportedEncodingException e) { + throw new EncryptedDocumentException(e); } } } diff --git a/src/java/org/apache/poi/poifs/crypt/EncryptionInfo.java b/src/java/org/apache/poi/poifs/crypt/EncryptionInfo.java index f2c1608112..25f9b01e15 100644 --- a/src/java/org/apache/poi/poifs/crypt/EncryptionInfo.java +++ b/src/java/org/apache/poi/poifs/crypt/EncryptionInfo.java @@ -65,7 +65,7 @@ public class EncryptionInfo { EncryptionInfoBuilder eib; try { eib = getBuilder(encryptionMode); - } catch (ReflectiveOperationException e) { + } catch (Exception e) { throw new IOException(e); } @@ -131,7 +131,7 @@ public class EncryptionInfo { EncryptionInfoBuilder eib; try { eib = getBuilder(encryptionMode); - } catch (ReflectiveOperationException e) { + } catch (Exception e) { throw new EncryptedDocumentException(e); } @@ -144,7 +144,7 @@ public class EncryptionInfo { } protected static EncryptionInfoBuilder getBuilder(EncryptionMode encryptionMode) - throws ReflectiveOperationException { + throws ClassNotFoundException, IllegalAccessException, InstantiationException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); EncryptionInfoBuilder eib; eib = (EncryptionInfoBuilder)cl.loadClass(encryptionMode.builder).newInstance();