1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 14:02:06 +01:00

Changed base64 implementation. SMACK-135

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@3828 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-05-03 21:13:25 +00:00 committed by alex
parent bed5b81ee7
commit c22f731c6d
3 changed files with 1473 additions and 99 deletions

File diff suppressed because it is too large Load diff

View file

@ -147,7 +147,6 @@ public class StringUtils {
for (; i < len; i++) { for (; i < len; i++) {
ch = input[i]; ch = input[i];
if (ch > '>') { if (ch > '>') {
continue;
} }
else if (ch == '<') { else if (ch == '<') {
if (i > last) { if (i > last) {
@ -252,24 +251,10 @@ public class StringUtils {
} }
hex.append(Integer.toString((int) bytes[i] & 0xff, 16)); hex.append(Integer.toString((int) bytes[i] & 0xff, 16));
} }
return hex.toString(); return hex.toString();
} }
//*********************************************************************
//* Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************
private static final int fillchar = '=';
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
/** /**
* Encodes a String as a base64 String. * Encodes a String as a base64 String.
* *
@ -294,38 +279,31 @@ public class StringUtils {
* @return a base64 encode String. * @return a base64 encode String.
*/ */
public static String encodeBase64(byte[] data) { public static String encodeBase64(byte[] data) {
int c; return encodeBase64(data, false);
int len = data.length; }
StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);
for (int i = 0; i < len; ++i) {
c = (data[i] >> 2) & 0x3f;
ret.append(cvt.charAt(c));
c = (data[i] << 4) & 0x3f;
if (++i < len)
c |= (data[i] >> 4) & 0x0f;
ret.append(cvt.charAt(c)); /**
if (i < len) { * Encodes a byte array into a bse64 String.
c = (data[i] << 2) & 0x3f; *
if (++i < len) * @param data The byte arry to encode.
c |= (data[i] >> 6) & 0x03; * @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);
}
ret.append(cvt.charAt(c)); /**
} * Encodes a byte array into a bse64 String.
else { *
++i; * @param data The byte arry to encode.
ret.append((char) fillchar); * @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.
if (i < len) { * @return A base64 encoded String.
c = data[i] & 0x3f; */
ret.append(cvt.charAt(c)); 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));
else {
ret.append((char) fillchar);
}
}
return ret.toString();
} }
/** /**
@ -335,54 +313,7 @@ public class StringUtils {
* @return the decoded String. * @return the decoded String.
*/ */
public static byte[] decodeBase64(String data) { public static byte[] decodeBase64(String data) {
byte [] bytes = null; return Base64.decode(data);
try {
bytes = data.getBytes("ISO-8859-1");
return decodeBase64(bytes).getBytes("ISO-8859-1");
}
catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
return new byte[] { };
}
/**
* Decodes a base64 aray of bytes.
*
* @param data a base64 encode byte array to decode.
* @return the decoded String.
*/
private static String decodeBase64(byte[] data) {
int c, c1;
int len = data.length;
StringBuffer ret = new StringBuffer((len * 3) / 4);
for (int i = 0; i < len; ++i) {
c = cvt.indexOf(data[i]);
++i;
c1 = cvt.indexOf(data[i]);
c = ((c << 2) | ((c1 >> 4) & 0x3));
ret.append((char) c);
if (++i < len) {
c = data[i];
if (fillchar == c)
break;
c = cvt.indexOf(c);
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.append((char) c1);
}
if (++i < len) {
c1 = data[i];
if (fillchar == c1)
break;
c1 = cvt.indexOf(c1);
c = ((c << 6) & 0xc0) | c1;
ret.append((char) c);
}
}
return ret.toString();
} }
/** /**
@ -414,7 +345,7 @@ public class StringUtils {
* @param length the desired length of the random String to return. * @param length the desired length of the random String to return.
* @return a random String of numbers and letters of the specified length. * @return a random String of numbers and letters of the specified length.
*/ */
public static final String randomString(int length) { public static String randomString(int length) {
if (length < 1) { if (length < 1) {
return null; return null;
} }

View file

@ -23,6 +23,7 @@ import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.filter.*; import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
@ -156,8 +157,6 @@ public class IBBTransferNegotiator extends StreamNegotiator {
final String userID; final String userID;
private final int options = Base64.DONT_BREAK_LINES;
final private IQ closePacket; final private IQ closePacket;
private String messageID; private String messageID;
@ -222,7 +221,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
IBBExtensions.Data ext = new IBBExtensions.Data(sid); IBBExtensions.Data ext = new IBBExtensions.Data(sid);
template.addExtension(ext); template.addExtension(ext);
String enc = Base64.encodeBytes(buffer, offset, len, options); String enc = StringUtils.encodeBase64(buffer, offset, len, false);
ext.setData(enc); ext.setData(enc);
ext.setSeq(seq); ext.setSeq(seq);
@ -346,7 +345,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
IBBExtensions.NAMESPACE); IBBExtensions.NAMESPACE);
checkSequence(mess, (int) data.getSeq()); checkSequence(mess, (int) data.getSeq());
buffer = Base64.decode(data.getData()); buffer = StringUtils.decodeBase64(data.getData());
bufferPointer = 0; bufferPointer = 0;
return true; return true;
} }