diff options
author | Kevin Smith <git@kismith.co.uk> | 2011-07-01 12:18:52 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2011-07-01 12:18:52 (GMT) |
commit | 2169d8cd423c399ceb53e962ad5313e3c28d2297 (patch) | |
tree | c581d1ba8b15509c15a8fadfbc2090c915aec1ad /src | |
parent | 2da71a8a85486a494343f1662d64fb5ae5a2a44e (diff) | |
download | stroke-2169d8cd423c399ceb53e962ad5313e3c28d2297.zip stroke-2169d8cd423c399ceb53e962ad5313e3c28d2297.tar.bz2 |
Update the ByteArray to not copy excessively on appends.
Also updates build.xml so the path to the xpp library can be specified,
rather than needing the same layout as my build tree.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/isode/stroke/base/ByteArray.java | 39 | ||||
-rw-r--r-- | src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java | 1 | ||||
-rw-r--r-- | src/com/isode/stroke/stringcodecs/HMACSHA1.java | 2 | ||||
-rw-r--r-- | src/com/isode/stroke/stringcodecs/PBKDF2.java | 1 |
4 files changed, 24 insertions, 19 deletions
diff --git a/src/com/isode/stroke/base/ByteArray.java b/src/com/isode/stroke/base/ByteArray.java index a36e305..a715d36 100644 --- a/src/com/isode/stroke/base/ByteArray.java +++ b/src/com/isode/stroke/base/ByteArray.java @@ -10,6 +10,7 @@ package com.isode.stroke.base; import java.io.UnsupportedEncodingException; +import java.util.Vector; /** * @@ -36,8 +37,8 @@ public class ByteArray { } private void fromBytes(final byte[] b) { - data_ = new byte[b.length]; - System.arraycopy(b, 0, data_, 0, b.length); + clear(); + append(b); } /*public ByteArray(char[] c, int n) { @@ -47,15 +48,20 @@ public class ByteArray { }*/ /** - * These are the raw, modifyable data! - * @return + * @return array copy of internal data. */ public byte[] getData() { - return data_; + if (dataCopy_ == null) { + dataCopy_ = new byte[getSize()]; + for (int i = 0; i < data_.size(); i++) { + dataCopy_[i] = data_.get(i); + } + } + return dataCopy_; } public int getSize() { - return data_.length; + return data_.size(); } public boolean isEmpty() { @@ -87,22 +93,16 @@ public class ByteArray { /** Mutable add */ private ByteArray append(byte[] b) { - int newLength = data_.length + b.length; - byte[] newData = new byte[newLength]; - for (int i = 0; i < data_.length; i++) { - newData[i] = data_[i]; - } for (int i = 0; i < b.length; i++) { - newData[i + data_.length] = b[i]; + append(b[i]); } - data_ = newData; return this; } /** Mutable add */ public ByteArray append(byte b) { - byte[] bytes = {b}; - append(bytes); + dataCopy_ = null; /* Invalidate cache */ + data_.add(b); return this; } @@ -144,7 +144,7 @@ public class ByteArray { @Override public String toString() { try { - return new String(data_, "UTF-8"); + return new String(getData(), "UTF-8"); } catch (UnsupportedEncodingException ex) { throw new IllegalStateException("JVM has no 'UTF-8' encoding"); } @@ -155,8 +155,9 @@ public class ByteArray { } public void clear() { - data_ = new byte[]{}; + data_ = new Vector<Byte>(); + dataCopy_ = null; } - private byte[] data_ = {}; - + Vector<Byte> data_ = new Vector<Byte>(); + byte[] dataCopy_ = null; } diff --git a/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java b/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java index da44d85..09a3ac7 100644 --- a/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java +++ b/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java @@ -60,6 +60,7 @@ public class SCRAMSHA1ClientAuthenticator extends ClientAuthenticator { for (int i = 0; i < clientProofData.length; ++i) { clientProofData[i] ^= clientSignature.getData()[i]; } + clientProof = new ByteArray(clientProofData); ByteArray result = getFinalMessageWithoutProof().append(",p=").append(Base64.encode(clientProof)); return result; } else { diff --git a/src/com/isode/stroke/stringcodecs/HMACSHA1.java b/src/com/isode/stroke/stringcodecs/HMACSHA1.java index 1d8c9b7..8d3f5c5 100644 --- a/src/com/isode/stroke/stringcodecs/HMACSHA1.java +++ b/src/com/isode/stroke/stringcodecs/HMACSHA1.java @@ -34,6 +34,7 @@ public class HMACSHA1 { for (int i = 0; i < xInner.length; ++i) { xInner[i] ^= 0x36; } + x = new ByteArray(xInner); x.append(data); // Create the second value @@ -42,6 +43,7 @@ public class HMACSHA1 { for (int i = 0; i < yInner.length; ++i) { yInner[i] ^= 0x5c; } + y = new ByteArray(yInner); y.append(SHA1.getHash(x)); return SHA1.getHash(y); diff --git a/src/com/isode/stroke/stringcodecs/PBKDF2.java b/src/com/isode/stroke/stringcodecs/PBKDF2.java index 547cbec..8e1a28e 100644 --- a/src/com/isode/stroke/stringcodecs/PBKDF2.java +++ b/src/com/isode/stroke/stringcodecs/PBKDF2.java @@ -24,6 +24,7 @@ public class PBKDF2 { } ++i; } + result = new ByteArray(resultData); return result; } } |