1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-25 15:52:06 +01:00

Create smack.util.stringencoder for Base64, Base32,…

Use Android's Base64 implementation when on Android, otherwise, when on
Java7, use the existing one.
This commit is contained in:
Florian Schmaus 2014-09-04 11:04:51 +02:00
parent 90c0064394
commit 5d4aa76d19
32 changed files with 491 additions and 231 deletions

View file

@ -21,12 +21,18 @@ import java.util.List;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SimpleSmackInitializer;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smack.util.stringencoder.Base64UrlSafeEncoder;
import org.jivesoftware.smack.util.stringencoder.android.AndroidBase64Encoder;
import org.jivesoftware.smack.util.stringencoder.android.AndroidBase64UrlSafeEncoder;
public class AndroidSmackInitializer extends SimpleSmackInitializer {
@Override
public List<Exception> initialize() {
SmackConfiguration.setDefaultHostnameVerifier(new StrictHostnameVerifier());
Base64.setEncoder(AndroidBase64Encoder.getInstance());
Base64UrlSafeEncoder.setEncoder(AndroidBase64UrlSafeEncoder.getInstance());
return null;
}

View file

@ -0,0 +1,57 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder.android;
import android.util.Base64;
/**
* A Base 64 encoding implementation based on android.util.Base64
* @author Florian Schmaus
*/
public class AndroidBase64Encoder implements org.jivesoftware.smack.util.stringencoder.Base64.Encoder {
private static AndroidBase64Encoder instance = new AndroidBase64Encoder();
private AndroidBase64Encoder() {
// Use getInstance()
}
public static AndroidBase64Encoder getInstance() {
return instance;
}
@Override
public byte[] decode(String string) {
return Base64.decode(string, Base64.DEFAULT);
}
@Override
public byte[] decode(byte[] input, int offset, int len) {
return Base64.decode(input, offset, len, 0);
}
@Override
public String encodeToString(byte[] input, int offset, int len) {
return Base64.encodeToString(input, offset, len, Base64.DEFAULT);
}
@Override
public byte[] encode(byte[] input, int offset, int len) {
return Base64.encode(input, offset, len, Base64.DEFAULT);
}
}

View file

@ -0,0 +1,61 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder.android;
import java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
import android.util.Base64;
/**
*
*/
public class AndroidBase64UrlSafeEncoder implements StringEncoder {
private static AndroidBase64UrlSafeEncoder instance = new AndroidBase64UrlSafeEncoder();
private AndroidBase64UrlSafeEncoder() {
// Use getInstance()
}
public static AndroidBase64UrlSafeEncoder getInstance() {
return instance;
}
@Override
public String encode(String string) {
try {
return Base64.encodeToString(string.getBytes(StringUtils.UTF8), Base64.URL_SAFE);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
@Override
public String decode(String string) {
byte[] bytes = Base64.decode(string, Base64.URL_SAFE);
try {
return new String(bytes, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
}

View file

@ -27,7 +27,7 @@ import java.net.UnknownHostException;
import javax.net.SocketFactory;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -92,7 +92,7 @@ class HTTPProxySocketFactory
else
{
String password = proxy.getProxyPassword();
proxyLine = "\r\nProxy-Authorization: Basic " + StringUtils.encodeBase64(username + ":" + password);
proxyLine = "\r\nProxy-Authorization: Basic " + Base64.encode(username + ":" + password);
}
socket.getOutputStream().write((hostport + " HTTP/1.1\r\nHost: "
+ hostport + proxyLine + "\r\n\r\n").getBytes("UTF-8"));

View file

@ -28,9 +28,9 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base32;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -298,7 +298,7 @@ public class DirectoryRosterStore implements RosterStore {
private File getBareJidFile(String bareJid) {
String encodedJid = Base32Encoder.getInstance().encode(bareJid);
String encodedJid = Base32.encode(bareJid);
return new File(fileDir, ENTRY_PREFIX + encodedJid);
}

View file

@ -22,6 +22,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.sasl.packet.SaslStanzas.AuthMechanism;
import org.jivesoftware.smack.sasl.packet.SaslStanzas.Response;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import javax.security.auth.callback.CallbackHandler;
@ -172,7 +173,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
byte[] authenticationBytes = getAuthenticationText();
String authenticationText;
if (authenticationBytes != null) {
authenticationText = StringUtils.encodeBase64(authenticationBytes);
authenticationText = Base64.encodeToString(authenticationBytes);
} else {
// RFC6120 6.4.2 "If the initiating entity needs to send a zero-length initial response,
// it MUST transmit the response as a single equals sign character ("="), which
@ -202,7 +203,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
* @throws SmackException
*/
public final void challengeReceived(String challengeString, boolean finalChallenge) throws SmackException, NotConnectedException {
byte[] challenge = StringUtils.decodeBase64(challengeString);
byte[] challenge = Base64.decode(challengeString);
byte[] response = evaluateChallenge(challenge);
if (finalChallenge) {
return;
@ -213,7 +214,7 @@ public abstract class SASLMechanism implements Comparable<SASLMechanism> {
responseStanza = new Response();
}
else {
responseStanza = new Response(StringUtils.encodeBase64(response, false));
responseStanza = new Response(Base64.encodeToString(response));
}
// Send the authentication to the server

View file

@ -1,44 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
/**
* A Base 64 encoding implementation.
* @author Florian Schmaus
*/
public class Base64Encoder implements StringEncoder {
private static Base64Encoder instance = new Base64Encoder();
private Base64Encoder() {
// Use getInstance()
}
public static Base64Encoder getInstance() {
return instance;
}
public String encode(String s) {
return Base64.encodeBytes(s.getBytes());
}
public String decode(String s) {
return new String(Base64.decode(s));
}
}

View file

@ -31,6 +31,7 @@ public class StringUtils {
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA-1";
public static final String UTF8 = "UTF-8";
public static final String USASCII = "US-ASCII";
public static final String QUOTE_ENCODE = "&quot;";
public static final String APOS_ENCODE = "&apos;";
@ -161,67 +162,6 @@ public class StringUtils {
}
}
/**
* Encodes a String as a base64 String.
*
* @param data a String to encode.
* @return a base64 encoded String.
*/
public static String encodeBase64(String data) {
byte [] bytes = toBytes(data);
return encodeBase64(bytes);
}
/**
* Encodes a byte array into a base64 String.
*
* @param data a byte array to encode.
* @return a base64 encode String.
*/
public static String encodeBase64(byte[] data) {
return encodeBase64(data, false);
}
/**
* Encodes a byte array into a bse64 String.
*
* @param data The byte arry to encode.
* @param lineBreaks True if the encoding should contain line breaks and false if it should not.
* @return A base64 encoded String.
*/
public static String encodeBase64(byte[] data, boolean lineBreaks) {
return encodeBase64(data, 0, data.length, lineBreaks);
}
/**
* Encodes a byte array into a bse64 String.
*
* @param data The byte arry to encode.
* @param offset the offset of the bytearray to begin encoding at.
* @param len the length of bytes to encode.
* @param lineBreaks True if the encoding should contain line breaks and false if it should not.
* @return A base64 encoded String.
*/
public static String encodeBase64(byte[] data, int offset, int len, boolean lineBreaks) {
return Base64.encodeBytes(data, offset, len, (lineBreaks ? Base64.NO_OPTIONS : Base64.DONT_BREAK_LINES));
}
/**
* Decodes a base64 String.
* Unlike Base64.decode() this method does not try to detect and decompress a gzip-compressed input.
*
* @param data a base64 encoded String to decode.
* @return the decoded String.
*/
public static byte[] decodeBase64(String data) {
byte[] bytes = toBytes(data);
return decodeBase64(bytes);
}
public static byte[] decodeBase64(byte[] data) {
return Base64.decode(data, 0, data.length, Base64.NO_OPTIONS);
}
/**
* Pseudo-random number generator object for use with randomString().
* The Random class is not considered to be cryptographically secure, so

View file

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.util.stringencoder;
import java.io.ByteArrayOutputStream;
@ -30,21 +30,28 @@ import java.io.IOException;
* @see <a href="http://en.wikipedia.org/wiki/Base32">Base32 Wikipedia entry</a>
*
*/
public class Base32Encoder implements StringEncoder {
public class Base32 {
private static Base32Encoder instance = new Base32Encoder();
private static final StringEncoder base32Stringencoder = new StringEncoder() {
@Override
public String encode(String string) {
return Base32.encode(string);
}
@Override
public String decode(String string) {
return Base32.decode(string);
}
};
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678";
private Base32Encoder() {
// Use getInstance()
public static StringEncoder getStringEncoder() {
return base32Stringencoder;
}
public static Base32Encoder getInstance() {
return instance;
}
@Override
public String decode(String str) {
public static String decode(String str) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte[] raw = str.getBytes();
for (int i = 0; i < raw.length; i++) {
@ -102,8 +109,7 @@ public class Base32Encoder implements StringEncoder {
return new String(bs.toByteArray());
}
@Override
public String encode(String str) {
public static String encode(String str) {
byte[] b = str.getBytes();
ByteArrayOutputStream os = new ByteArrayOutputStream();

View file

@ -0,0 +1,107 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder;
import java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.StringUtils;
public class Base64 {
private static Base64.Encoder base64encoder;
public static void setEncoder(Base64.Encoder encoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder must no be null");
}
base64encoder = encoder;
}
public static final String encode(String string) {
try {
return encodeToString(string.getBytes(StringUtils.UTF8));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
public static final String encodeToString(byte[] input) {
byte[] bytes = encode(input);
try {
return new String(bytes, StringUtils.USASCII);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
public static final String encodeToString(byte[] input, int offset, int len) {
byte[] bytes = encode(input, offset, len);
try {
return new String(bytes, StringUtils.USASCII);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
public static final byte[] encode(byte[] input) {
return encode(input, 0, input.length);
}
public static final byte[] encode(byte[] input, int offset, int len) {
return base64encoder.encode(input, offset, len);
}
public static final String decodeToString(String string) {
byte[] bytes = decode(string);
try {
return new String(bytes, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
public static final String decodeToString(byte[] input, int offset, int len) {
byte[] bytes = decode(input, offset, len);
try {
return new String(bytes, StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
}
}
public static final byte[] decode(String string) {
return base64encoder.decode(string);
}
public static final byte[] decode(byte[] input) {
return base64encoder.decode(input, 0, input.length);
}
public static final byte[] decode(byte[] input, int offset, int len) {
return base64encoder.decode(input, offset, len);
}
public interface Encoder {
byte[] decode(String string);
byte[] decode(byte[] input, int offset, int len);
String encodeToString(byte[] input, int offset, int len);
byte[] encode(byte[] input, int offset, int len);
}
}

View file

@ -0,0 +1,42 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder;
public class Base64UrlSafeEncoder {
private static StringEncoder base64UrlSafeEncoder;
public static void setEncoder(StringEncoder encoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder must no be null");
}
base64UrlSafeEncoder = encoder;
}
public static StringEncoder getStringEncoder() {
return base64UrlSafeEncoder;
}
public static final String encode(String string) {
return base64UrlSafeEncoder.encode(string);
}
public static final String decode(String string) {
return base64UrlSafeEncoder.decode(string);
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Florian Schmaus
* Copyright 2013-2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.util.stringencoder;
/**
* @author Florian Schmaus

View file

@ -136,51 +136,6 @@ public class StringUtilsTest {
new String(output.getBytes()));
}
/**
* This method tests 2 StringUtil methods - encodeBase64(String) and encodeBase64(byte[]).
*/
@Test
public void testEncodeBase64() {
String input = "";
String output = "";
assertEquals(StringUtils.encodeBase64(input), output);
input = "foo bar 123";
output = "Zm9vIGJhciAxMjM=";
assertEquals(StringUtils.encodeBase64(input), output);
input = "=";
output = "PQ==";
assertEquals(StringUtils.encodeBase64(input), output);
input = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
assertEquals(StringUtils.encodeBase64(input), output);
}
/***
* This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]).
*/
/*
public void testDecodeBase64() {
String input = "";
String output = "";
assertEquals(StringUtils.decodeBase64(input), output);
input = "Zm9vIGJhciAxMjM=";
output = "foo bar 123";
assertEquals(StringUtils.decodeBase64(input), output);
input = "PQ==";
output = "=";
assertEquals(StringUtils.decodeBase64(input), output);
input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
assertEquals(StringUtils.decodeBase64(input), output);
}
*/
@Test
public void testRandomString() {
// Boundary test

View file

@ -0,0 +1,65 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder;
import static org.junit.Assert.assertEquals;
// TODO those tests should be run with the Java7 and Android impl
public class Base64Test {
/**
* This method tests 2 StringUtil methods - encodeBase64(String) and encodeBase64(byte[]).
*/
public void testEncodeBase64() {
String input = "";
String output = "";
assertEquals(Base64.encode(input), output);
input = "foo bar 123";
output = "Zm9vIGJhciAxMjM=";
assertEquals(Base64.encode(input), output);
input = "=";
output = "PQ==";
assertEquals(Base64.encode(input), output);
input = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
assertEquals(Base64.encode(input), output);
}
/***
* This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]).
*/
public void testDecodeBase64() {
String input = "";
String output = "";
assertEquals(Base64.decodeToString(input), output);
input = "Zm9vIGJhciAxMjM=";
output = "foo bar 123";
assertEquals(Base64.decodeToString(input), output);
input = "PQ==";
output = "=";
assertEquals(Base64.decodeToString(input), output);
input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
assertEquals(Base64.decodeToString(input), output);
}
}

View file

@ -8,4 +8,5 @@ Classes and methods that implement support for the various XMPP XEPs
dependencies {
compile project(':smack-core')
testCompile project(':smack-core').sourceSets.test.runtimeClasspath
testCompile project(':smack-java7')
}

View file

@ -35,7 +35,7 @@ import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Close;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
@ -708,7 +708,7 @@ public class InBandBytestreamSession implements BytestreamSession {
}
// create data packet
String enc = StringUtils.encodeBase64(buffer, 0, bufferPointer, false);
String enc = Base64.encodeToString(buffer, 0, bufferPointer);
DataPacketExtension data = new DataPacketExtension(byteStreamRequest.getSessionID(),
this.seq, enc);

View file

@ -17,8 +17,8 @@
package org.jivesoftware.smackx.bytestreams.ibb.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64;
/**
* Represents a chunk of data of an In-Band Bytestream within an IQ stanza or a
@ -121,7 +121,7 @@ public class DataPacketExtension implements PacketExtension {
}
// decodeBase64 will return null if bad characters are included
this.decodedData = StringUtils.decodeBase64(data);
this.decodedData = Base64.decode(data);
return this.decodedData;
}

View file

@ -35,8 +35,8 @@ import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.util.Base64;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.caps.cache.EntityCapsPersistentCache;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.jivesoftware.smackx.disco.NodeInformationProvider;
@ -663,7 +663,7 @@ public class EntityCapsManager extends Manager {
// (note: the Base64 output MUST NOT include whitespace and MUST set
// padding bits to zero).
byte[] digest = md.digest(sb.toString().getBytes());
return Base64.encodeBytes(digest);
return Base64.encodeToString(digest);
}
private static void formFieldValuesToCaps(List<String> i, StringBuilder sb) {

View file

@ -25,9 +25,9 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringEncoder;
import org.jivesoftware.smack.util.stringencoder.Base32;
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
/**
@ -48,14 +48,14 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
* Creates a new SimpleDirectoryPersistentCache Object. Make sure that the
* cacheDir exists and that it's an directory.
* <p>
* Default filename encoder {@link Base32Encoder}, as this will work on all
* Default filename encoder {@link Base32}, as this will work on all
* file systems, both case sensitive and case insensitive. It does however
* produce longer filenames.
*
* @param cacheDir
*/
public SimpleDirectoryPersistentCache(File cacheDir) {
this(cacheDir, Base32Encoder.getInstance());
this(cacheDir, Base32.getStringEncoder());
}
/**
@ -63,7 +63,7 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
* cacheDir exists and that it's an directory.
*
* If your cacheDir is case insensitive then make sure to set the
* StringEncoder to {@link Base32Encoder} (which is the default).
* StringEncoder to {@link Base32} (which is the default).
*
* @param cacheDir The directory where the cache will be stored.
* @param filenameEncoder Encodes the node string into a filename.

View file

@ -28,8 +28,8 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64;
/**
* Properties provide an easy mechanism for clients to share data. Each property has a
@ -182,7 +182,7 @@ public class JivePropertiesExtension implements PacketExtension {
out = new ObjectOutputStream(byteStream);
out.writeObject(value);
type = "java-object";
valueStr = StringUtils.encodeBase64(byteStream.toByteArray());
valueStr = Base64.encodeToString(byteStream.toByteArray());
}
catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error encoding java object", e);

View file

@ -25,7 +25,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
import org.xmlpull.v1.XmlPullParser;
@ -94,7 +94,7 @@ public class JivePropertiesExtensionProvider implements PacketExtensionProvider
else if ("java-object".equals(type)) {
if (JivePropertiesManager.isJavaObjectEnabled()) {
try {
byte[] bytes = StringUtils.decodeBase64(valueText);
byte[] bytes = Base64.decode(valueText);
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bytes));
value = in.readObject();

View file

@ -38,6 +38,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.vcardtemp.VCardManager;
/**
@ -372,7 +373,7 @@ public class VCard extends IQ {
}
// Otherwise, add to mappings.
String encodedImage = StringUtils.encodeBase64(bytes);
String encodedImage = Base64.encodeToString(bytes);
setAvatar(encodedImage, mimeType);
}
@ -425,7 +426,7 @@ public class VCard extends IQ {
if (photoBinval == null) {
return null;
}
return StringUtils.decodeBase64(photoBinval);
return Base64.decode(photoBinval);
}
/**

View file

@ -20,12 +20,12 @@ import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.jivesoftware.smackx.vcardtemp.provider.VCardProvider;
import org.junit.Test;
public class VCardUnitTest {
public class VCardUnitTest extends InitExtensions {
@Test
public void testNoWorkHomeSpecifier_EMAIL() throws Throwable {
@ -67,7 +67,7 @@ public class VCardUnitTest {
}
public static byte[] getAvatarBinary() {
return StringUtils.decodeBase64(getAvatarEncoded());
return Base64.decode(getAvatarEncoded());
}
private static String getAvatarEncoded() {
return "/9j/4AAQSkZJRgABAQEASABIAAD/4QAWRXhpZgAATU0AKgAAAAgAAAAAAAD/2wBDAAUDBAQEAwUE\n" +

View file

@ -31,7 +31,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
@ -263,7 +263,7 @@ public class InBandBytestreamSessionMessageTest {
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
// build invalid packet with out of order sequence
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 123, base64Data);
Message dataMessage = new Message();
dataMessage.addExtension(dpe);
@ -304,8 +304,7 @@ public class InBandBytestreamSessionMessageTest {
// verify data packet and notify listener
for (int i = 0; i < controlData.length / blockSize; i++) {
String base64Data = StringUtils.encodeBase64(controlData, i * blockSize, blockSize,
false);
String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
Message dataMessage = new Message();
dataMessage.addExtension(dpe);
@ -350,8 +349,7 @@ public class InBandBytestreamSessionMessageTest {
// verify data packet and notify listener
for (int i = 0; i < controlData.length / blockSize; i++) {
String base64Data = StringUtils.encodeBase64(controlData, i * blockSize, blockSize,
false);
String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
Message dataMessage = new Message();
dataMessage.addExtension(dpe);

View file

@ -31,7 +31,7 @@ import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
@ -308,7 +308,7 @@ public class InBandBytestreamSessionTest {
// insert data to read
InputStream inputStream = session.getInputStream();
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);
listener.processPacket(data);
@ -343,7 +343,7 @@ public class InBandBytestreamSessionTest {
InputStream inputStream = session.getInputStream();
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);
@ -381,7 +381,7 @@ public class InBandBytestreamSessionTest {
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
// build data packets
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data1 = new Data(dpe);
Data data2 = new Data(dpe);
@ -453,7 +453,7 @@ public class InBandBytestreamSessionTest {
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
// build invalid packet with out of order sequence
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 123, base64Data);
Data data = new Data(dpe);
@ -496,8 +496,7 @@ public class InBandBytestreamSessionTest {
// set data packet acknowledgment and notify listener
for (int i = 0; i < controlData.length / blockSize; i++) {
protocol.addResponse(resultIQ);
String base64Data = StringUtils.encodeBase64(controlData, i * blockSize, blockSize,
false);
String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
Data data = new Data(dpe);
listener.processPacket(data);
@ -544,8 +543,7 @@ public class InBandBytestreamSessionTest {
// set data packet acknowledgment and notify listener
for (int i = 0; i < controlData.length / blockSize; i++) {
protocol.addResponse(resultIQ);
String base64Data = StringUtils.encodeBase64(controlData, i * blockSize, blockSize,
false);
String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
Data data = new Data(dpe);
listener.processPacket(data);
@ -584,7 +582,7 @@ public class InBandBytestreamSessionTest {
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
// build data packet
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);
@ -627,7 +625,7 @@ public class InBandBytestreamSessionTest {
PacketListener listener = Whitebox.getInternalState(inputStream, PacketListener.class);
// build data packet
String base64Data = StringUtils.encodeBase64("Data");
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);

View file

@ -24,8 +24,8 @@ import static org.mockito.Mockito.when;
import java.util.Properties;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.junit.Test;
import com.jamesmurty.utils.XMLBuilder;
@ -56,7 +56,7 @@ public class DataTest {
@Test
public void shouldReturnValidIQStanzaXML() throws Exception {
String encodedData = StringUtils.encodeBase64("Test");
String encodedData = Base64.encode("Test");
String control = XMLBuilder.create("iq")
.a("from", "romeo@montague.lit/orchard")

View file

@ -26,9 +26,8 @@ import java.util.Collection;
import java.util.LinkedList;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.Base64FileUrlEncoder;
import org.jivesoftware.smack.util.StringEncoder;
import org.jivesoftware.smack.util.stringencoder.Base32;
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.caps.cache.EntityCapsPersistentCache;
import org.jivesoftware.smackx.caps.cache.SimpleDirectoryPersistentCache;
@ -52,16 +51,10 @@ public class EntityCapsManagerTest extends InitExtensions {
assertEquals("q07IKJEyjvHSyhy//CH0CxmKi8w=", ver);
}
@Test
public void testSimpleDirectoryCacheBase64() throws IOException {
EntityCapsManager.persistentCache = null;
testSimpleDirectoryCache(Base64FileUrlEncoder.getInstance());
}
@Test
public void testSimpleDirectoryCacheBase32() throws IOException {
EntityCapsManager.persistentCache = null;
testSimpleDirectoryCache(Base32Encoder.getInstance());
testSimpleDirectoryCache(Base32.getStringEncoder());
}
@Test

View file

@ -20,12 +20,18 @@ import java.util.List;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SimpleSmackInitializer;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smack.util.stringencoder.Base64UrlSafeEncoder;
import org.jivesoftware.smack.util.stringencoder.java7.Java7Base64Encoder;
import org.jivesoftware.smack.util.stringencoder.java7.Java7Base64UrlSafeEncoder;
public class Java7SmackInitializer extends SimpleSmackInitializer {
@Override
public List<Exception> initialize() {
SmackConfiguration.setDefaultHostnameVerifier(new Java7HostnameVerifier());
Base64.setEncoder(Java7Base64Encoder.getInstance());
Base64UrlSafeEncoder.setEncoder(Java7Base64UrlSafeEncoder.getInstance());
return null;
}

View file

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.util.stringencoder.java7;
import java.util.logging.Level;
import java.util.logging.Logger;

View file

@ -0,0 +1,64 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util.stringencoder.java7;
import java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.StringUtils;
/**
* A Base 64 encoding implementation.
* @author Florian Schmaus
*/
public class Java7Base64Encoder implements org.jivesoftware.smack.util.stringencoder.Base64.Encoder {
private static Java7Base64Encoder instance = new Java7Base64Encoder();
private Java7Base64Encoder() {
// Use getInstance()
}
public static Java7Base64Encoder getInstance() {
return instance;
}
@Override
public byte[] decode(String string) {
return Base64.decode(string);
}
@Override
public byte[] decode(byte[] input, int offset, int len) {
return Base64.decode(input, offset, len, 0);
}
@Override
public String encodeToString(byte[] input, int offset, int len) {
return Base64.encodeBytes(input, offset, len);
}
@Override
public byte[] encode(byte[] input, int offset, int len) {
String string = encodeToString(input, offset, len);
try {
return string.getBytes(StringUtils.USASCII);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
}

View file

@ -14,7 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.util.stringencoder.java7;
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
/**
@ -24,18 +26,19 @@ package org.jivesoftware.smack.util;
* Note: This does NOT produce standard Base 64 encodings, but a variant as defined in
* Section 4 of RFC3548:
* <a href="http://www.faqs.org/rfcs/rfc3548.html">http://www.faqs.org/rfcs/rfc3548.html</a>.
* </p>
*
* @author Robin Collier
*/
public class Base64FileUrlEncoder implements StringEncoder {
public class Java7Base64UrlSafeEncoder implements StringEncoder {
private static Base64FileUrlEncoder instance = new Base64FileUrlEncoder();
private static Java7Base64UrlSafeEncoder instance = new Java7Base64UrlSafeEncoder();
private Base64FileUrlEncoder() {
private Java7Base64UrlSafeEncoder() {
// Use getInstance()
}
public static Base64FileUrlEncoder getInstance() {
public static Java7Base64UrlSafeEncoder getInstance() {
return instance;
}

View file

@ -19,7 +19,7 @@ package org.jivesoftware.smackx.workgroup.settings;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.xmlpull.v1.XmlPullParser;
public class SoundSettings extends IQ {
@ -36,11 +36,11 @@ public class SoundSettings extends IQ {
}
public byte[] getIncomingSoundBytes() {
return StringUtils.decodeBase64(incomingSound);
return Base64.decode(incomingSound);
}
public byte[] getOutgoingSoundBytes() {
return StringUtils.decodeBase64(outgoingSound);
return Base64.decode(outgoingSound);
}