aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java')
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java36
1 files changed, 28 insertions, 8 deletions
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 <code>base64String</code>.
* @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 <code>String</code> and provide
* them via an <code>InputStream</code>.
@@ -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 <code>String</code>.
*
@@ -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
* <code>String</code>.
@@ -114,7 +134,7 @@ public class Base64Utils {
* <code>String</code>.
* @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);
}
}