mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-23 02:58:00 +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:
parent
bed5b81ee7
commit
c22f731c6d
3 changed files with 1473 additions and 99 deletions
1444
source/org/jivesoftware/smack/util/Base64.java
Normal file
1444
source/org/jivesoftware/smack/util/Base64.java
Normal file
File diff suppressed because it is too large
Load diff
|
@ -147,7 +147,6 @@ public class StringUtils {
|
|||
for (; i < len; i++) {
|
||||
ch = input[i];
|
||||
if (ch > '>') {
|
||||
continue;
|
||||
}
|
||||
else if (ch == '<') {
|
||||
if (i > last) {
|
||||
|
@ -252,24 +251,10 @@ public class StringUtils {
|
|||
}
|
||||
hex.append(Integer.toString((int) bytes[i] & 0xff, 16));
|
||||
}
|
||||
|
||||
|
||||
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.
|
||||
*
|
||||
|
@ -294,38 +279,31 @@ public class StringUtils {
|
|||
* @return a base64 encode String.
|
||||
*/
|
||||
public static String encodeBase64(byte[] data) {
|
||||
int c;
|
||||
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;
|
||||
return encodeBase64(data, false);
|
||||
}
|
||||
|
||||
ret.append(cvt.charAt(c));
|
||||
if (i < len) {
|
||||
c = (data[i] << 2) & 0x3f;
|
||||
if (++i < len)
|
||||
c |= (data[i] >> 6) & 0x03;
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
ret.append(cvt.charAt(c));
|
||||
}
|
||||
else {
|
||||
++i;
|
||||
ret.append((char) fillchar);
|
||||
}
|
||||
|
||||
if (i < len) {
|
||||
c = data[i] & 0x3f;
|
||||
ret.append(cvt.charAt(c));
|
||||
}
|
||||
else {
|
||||
ret.append((char) fillchar);
|
||||
}
|
||||
}
|
||||
return ret.toString();
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -335,54 +313,7 @@ public class StringUtils {
|
|||
* @return the decoded String.
|
||||
*/
|
||||
public static byte[] decodeBase64(String data) {
|
||||
byte [] bytes = null;
|
||||
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();
|
||||
return Base64.decode(data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -414,7 +345,7 @@ public class StringUtils {
|
|||
* @param length the desired length of the random String to return.
|
||||
* @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) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.jivesoftware.smack.PacketCollector;
|
|||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
|
@ -156,8 +157,6 @@ public class IBBTransferNegotiator extends StreamNegotiator {
|
|||
|
||||
final String userID;
|
||||
|
||||
private final int options = Base64.DONT_BREAK_LINES;
|
||||
|
||||
final private IQ closePacket;
|
||||
|
||||
private String messageID;
|
||||
|
@ -222,7 +221,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
|
|||
IBBExtensions.Data ext = new IBBExtensions.Data(sid);
|
||||
template.addExtension(ext);
|
||||
|
||||
String enc = Base64.encodeBytes(buffer, offset, len, options);
|
||||
String enc = StringUtils.encodeBase64(buffer, offset, len, false);
|
||||
|
||||
ext.setData(enc);
|
||||
ext.setSeq(seq);
|
||||
|
@ -346,7 +345,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
|
|||
IBBExtensions.NAMESPACE);
|
||||
|
||||
checkSequence(mess, (int) data.getSeq());
|
||||
buffer = Base64.decode(data.getData());
|
||||
buffer = StringUtils.decodeBase64(data.getData());
|
||||
bufferPointer = 0;
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue