]> source.dussan.org Git - poi.git/commitdiff
Fix jdk-differences for encryption patch
authorAndreas Beeker <kiwiwings@apache.org>
Tue, 24 Dec 2013 23:39:09 +0000 (23:39 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Tue, 24 Dec 2013 23:39:09 +0000 (23:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1553338 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java
src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java
src/java/org/apache/poi/poifs/crypt/EncryptionInfo.java

index 75bf1e85ea07df4c58c9ae3a86ff80f6cdde3cc3..33876fc975358e6d80c4c6c514e3f3f3b3e8bdd3 100644 (file)
@@ -16,7 +16,7 @@
 ==================================================================== */\r
 package org.apache.poi.poifs.crypt;\r
 \r
-import java.nio.charset.Charset;\r
+import java.io.UnsupportedEncodingException;\r
 import java.security.DigestException;\r
 import java.security.GeneralSecurityException;\r
 import java.security.MessageDigest;\r
@@ -215,8 +215,11 @@ public class CryptoFunctions {
     }\r
     \r
     public static byte[] getUtf16LeString(String str) {\r
-        Charset cs = Charset.forName("UTF-16LE");\r
-        return str.getBytes(cs);\r
+        try {\r
+            return str.getBytes("UTF-16LE");\r
+        } catch (UnsupportedEncodingException e) {\r
+            throw new EncryptedDocumentException(e);\r
+        }\r
     }\r
     \r
     public static MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm) {\r
index 963151ff97f2491deadb1008ffeaa584376bb0f6..9e1f231f82e7099f201ef28a8e4deded4b194cd5 100644 (file)
@@ -18,7 +18,7 @@
 package org.apache.poi.poifs.crypt;\r
 \r
 import java.io.IOException;\r
-import java.nio.charset.Charset;\r
+import java.io.UnsupportedEncodingException;\r
 \r
 import org.apache.poi.EncryptedDocumentException;\r
 import org.apache.poi.poifs.crypt.standard.EncryptionRecord;\r
@@ -295,7 +295,6 @@ public class DataSpaceMapUtils {
     }\r
     \r
     public static String readUnicodeLPP4(LittleEndianInput is) {\r
-        Charset cs = Charset.forName("UTF-16LE");\r
         int length = is.readInt();\r
         byte data[] = new byte[length];\r
         is.readFully(data);\r
@@ -305,16 +304,23 @@ public class DataSpaceMapUtils {
             // 2 bytes long, and each byte MUST be 0x00.            \r
             is.readShort();\r
         }\r
-        return new String(data, 0, data.length, cs);\r
+        try {\r
+            return new String(data, 0, data.length, "UTF-16LE");\r
+        } catch (UnsupportedEncodingException e) {\r
+            throw new EncryptedDocumentException(e);\r
+        }\r
     }\r
     \r
     public static void writeUnicodeLPP4(LittleEndianOutput os, String str) {\r
-        Charset cs = Charset.forName("UTF-16LE");\r
-        byte buf[] = str.getBytes(cs);\r
-        os.writeInt(buf.length);\r
-        os.write(buf);\r
-        if (buf.length%4==2) {\r
-            os.writeShort(0);\r
+        try {\r
+            byte buf[] = str.getBytes("UTF-16LE");\r
+            os.writeInt(buf.length);\r
+            os.write(buf);\r
+            if (buf.length%4==2) {\r
+                os.writeShort(0);\r
+            }\r
+        } catch (UnsupportedEncodingException e) {\r
+            throw new EncryptedDocumentException(e);\r
         }\r
     }\r
 \r
@@ -340,8 +346,11 @@ public class DataSpaceMapUtils {
                 is.readByte();\r
             }\r
         }\r
-        Charset cs = Charset.forName("UTF-8");\r
-        return new String(data, 0, data.length, cs);\r
+        try {\r
+            return new String(data, 0, data.length, "UTF-8");\r
+        } catch (UnsupportedEncodingException e) {\r
+            throw new EncryptedDocumentException(e);\r
+        }\r
     }\r
     \r
     public static void writeUtf8LPP4(LittleEndianOutput os, String str) {\r
@@ -349,15 +358,18 @@ public class DataSpaceMapUtils {
             os.writeInt(str == null ? 0 : 4);\r
             os.writeInt(0);\r
         } else {\r
-            Charset cs = Charset.forName("UTF-8");\r
-            byte buf[] = str.getBytes(cs);\r
-            os.writeInt(buf.length);\r
-            os.write(buf);\r
-            int scratchBytes = buf.length%4;\r
-            if (scratchBytes > 0) {\r
-                for (int i=0; i<(4-scratchBytes); i++) {\r
-                    os.writeByte(0);\r
+            try {\r
+                byte buf[] = str.getBytes("UTF-8");\r
+                os.writeInt(buf.length);\r
+                os.write(buf);\r
+                int scratchBytes = buf.length%4;\r
+                if (scratchBytes > 0) {\r
+                    for (int i=0; i<(4-scratchBytes); i++) {\r
+                        os.writeByte(0);\r
+                    }\r
                 }\r
+            } catch (UnsupportedEncodingException e) {\r
+                throw new EncryptedDocumentException(e);\r
             }\r
         }        \r
     }\r
index f2c160811277f6b84c1b998537e0af94dfc57213..25f9b01e15644b86f81ca4cd39a13fe8aaf905ac 100644 (file)
@@ -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();