1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02: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

@ -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