From 1ab0f1d4d991464b906c34befefe2ecaf485d485 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Tue, 19 Aug 2014 15:03:42 +0200 Subject: add interfederation without attributequery request which use encrypted bPKs (this functionality is required for federation with USP) --- .../at/gv/egovernment/moa/util/Base64Utils.java | 36 +++++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java') diff --git a/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java b/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java index 27f12ab0f..66bf50316 100644 --- a/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java +++ b/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java @@ -50,12 +50,12 @@ public class Base64Utils { * @return byte[] The raw bytes contained in the base64String. * @throws IOException Failed to read the Base64 data. */ - public static byte[] decode(String base64String, boolean ignoreInvalidChars) + public static byte[] decode(String base64String, boolean ignoreInvalidChars, String encoding) throws IOException { Base64InputStream in = new Base64InputStream( - new ByteArrayInputStream(base64String.getBytes("UTF-8")), + new ByteArrayInputStream(base64String.getBytes(encoding)), ignoreInvalidChars); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] bytes = new byte[256]; @@ -64,10 +64,15 @@ public class Base64Utils { while ((bytesRead = in.read(bytes)) > 0) { out.write(bytes, 0, bytesRead); } - + in.close(); + return out.toByteArray(); } + public static byte[] decode(String base64String, boolean ignoreInvalidChars) throws IOException { + return decode(base64String, ignoreInvalidChars, "UTF-8"); + } + /** * Read the bytes encoded in a Base64 encoded String and provide * them via an InputStream. @@ -80,11 +85,12 @@ public class Base64Utils { */ public static InputStream decodeToStream( String base64String, - boolean ignoreInvalidChars) { + boolean ignoreInvalidChars, + String encoding) { try { ByteArrayInputStream bin = - new ByteArrayInputStream(base64String.getBytes("UTF-8")); + new ByteArrayInputStream(base64String.getBytes(encoding)); Base64InputStream in = new Base64InputStream(bin, ignoreInvalidChars); return in; @@ -94,6 +100,13 @@ public class Base64Utils { } } + public static InputStream decodeToStream( + String base64String, + boolean ignoreInvalidChars) { + return decodeToStream(base64String, ignoreInvalidChars, "UTF-8"); + + } + /** * Convert a byte array to a Base64 encoded String. * @@ -102,9 +115,16 @@ public class Base64Utils { * @throws IOException Failed to write the bytes as Base64 data. */ public static String encode(byte[] bytes) throws IOException { - return encode(new ByteArrayInputStream(bytes)); + return encode(new ByteArrayInputStream(bytes), "UTF-8"); } + public static String encode(byte[] bytes, String encoding) throws IOException { + return encode(new ByteArrayInputStream(bytes), encoding); + } + + public static String encode(InputStream inputStream) throws IOException { + return encode(inputStream, "UTF-8"); + } /** * Convert the data contained in the given stream to a Base64 encoded * String. @@ -114,7 +134,7 @@ public class Base64Utils { * String. * @throws IOException Failed to convert the data in the stream. */ - public static String encode(InputStream inputStream) throws IOException { + public static String encode(InputStream inputStream, String encoding) throws IOException { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Base64OutputStream base64Stream = new Base64OutputStream(byteStream, "\n".getBytes()); byte[] bytes = new byte[256]; @@ -127,7 +147,7 @@ public class Base64Utils { base64Stream.close(); inputStream.close(); - return byteStream.toString("UTF-8"); + return byteStream.toString(encoding); } } -- cgit v1.2.3