From 51083b014eb3000386b288c61cf93f9f32c5cf29 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 5 Sep 2014 23:19:27 +0200 Subject: [PATCH] Use NO_WRAP/DONT_BREAK_LINES when doing Base64 encoding --- .../util/stringencoder/android/AndroidBase64Encoder.java | 6 ++++-- .../stringencoder/android/AndroidBase64UrlSafeEncoder.java | 6 ++++-- .../smack/util/stringencoder/java7/Java7Base64Encoder.java | 4 +++- .../util/stringencoder/java7/Java7Base64UrlSafeEncoder.java | 6 ++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64Encoder.java b/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64Encoder.java index b3f396da5..2bab9613c 100644 --- a/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64Encoder.java +++ b/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64Encoder.java @@ -26,6 +26,8 @@ public class AndroidBase64Encoder implements org.jivesoftware.smack.util.stringe private static AndroidBase64Encoder instance = new AndroidBase64Encoder(); + private static final int BASE64_ENCODER_FLAGS = Base64.NO_WRAP; + private AndroidBase64Encoder() { // Use getInstance() } @@ -46,12 +48,12 @@ public class AndroidBase64Encoder implements org.jivesoftware.smack.util.stringe @Override public String encodeToString(byte[] input, int offset, int len) { - return Base64.encodeToString(input, offset, len, Base64.DEFAULT); + return Base64.encodeToString(input, offset, len, BASE64_ENCODER_FLAGS); } @Override public byte[] encode(byte[] input, int offset, int len) { - return Base64.encode(input, offset, len, Base64.DEFAULT); + return Base64.encode(input, offset, len, BASE64_ENCODER_FLAGS); } } diff --git a/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64UrlSafeEncoder.java b/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64UrlSafeEncoder.java index 47f1e6537..49e86a990 100644 --- a/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64UrlSafeEncoder.java +++ b/smack-android/src/main/java/org/jivesoftware/smack/util/stringencoder/android/AndroidBase64UrlSafeEncoder.java @@ -31,6 +31,8 @@ public class AndroidBase64UrlSafeEncoder implements StringEncoder { private static AndroidBase64UrlSafeEncoder instance = new AndroidBase64UrlSafeEncoder(); + private static final int BASE64_ENCODER_FLAGS = Base64.URL_SAFE | Base64.NO_WRAP; + private AndroidBase64UrlSafeEncoder() { // Use getInstance() } @@ -42,7 +44,7 @@ public class AndroidBase64UrlSafeEncoder implements StringEncoder { @Override public String encode(String string) { try { - return Base64.encodeToString(string.getBytes(StringUtils.UTF8), Base64.URL_SAFE); + return Base64.encodeToString(string.getBytes(StringUtils.UTF8), BASE64_ENCODER_FLAGS); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("UTF-8 not supported", e); } @@ -50,7 +52,7 @@ public class AndroidBase64UrlSafeEncoder implements StringEncoder { @Override public String decode(String string) { - byte[] bytes = Base64.decode(string, Base64.URL_SAFE); + byte[] bytes = Base64.decode(string, BASE64_ENCODER_FLAGS); try { return new String(bytes, StringUtils.UTF8); } catch (UnsupportedEncodingException e) { diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java b/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java index 20fe2f50e..ad9eff72a 100644 --- a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java +++ b/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java @@ -28,6 +28,8 @@ public class Java7Base64Encoder implements org.jivesoftware.smack.util.stringenc private static Java7Base64Encoder instance = new Java7Base64Encoder(); + private static int BASE64_ENCODER_FLAGS = Base64.URL_SAFE; + private Java7Base64Encoder() { // Use getInstance() } @@ -48,7 +50,7 @@ public class Java7Base64Encoder implements org.jivesoftware.smack.util.stringenc @Override public String encodeToString(byte[] input, int offset, int len) { - return Base64.encodeBytes(input, offset, len); + return Base64.encodeBytes(input, offset, len, BASE64_ENCODER_FLAGS); } @Override diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java b/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java index d892bf301..d5e6e9750 100644 --- a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java +++ b/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java @@ -34,6 +34,8 @@ public class Java7Base64UrlSafeEncoder implements StringEncoder { private static Java7Base64UrlSafeEncoder instance = new Java7Base64UrlSafeEncoder(); + private static int BASE64_ENCODER_FLAGS = Base64.URL_SAFE | Base64.DONT_BREAK_LINES; + private Java7Base64UrlSafeEncoder() { // Use getInstance() } @@ -43,11 +45,11 @@ public class Java7Base64UrlSafeEncoder implements StringEncoder { } public String encode(String s) { - return Base64.encodeBytes(s.getBytes(), Base64.URL_SAFE); + return Base64.encodeBytes(s.getBytes(), BASE64_ENCODER_FLAGS); } public String decode(String s) { - return new String(Base64.decode(s, Base64.URL_SAFE)); + return new String(Base64.decode(s, BASE64_ENCODER_FLAGS)); } }