From 2169d8cd423c399ceb53e962ad5313e3c28d2297 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Fri, 1 Jul 2011 13:18:52 +0100 Subject: 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. diff --git a/build.xml b/build.xml index 820b482..94ef93b 100644 --- a/build.xml +++ b/build.xml @@ -15,9 +15,10 @@ + - + 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(); + dataCopy_ = null; } - private byte[] data_ = {}; - + Vector data_ = new Vector(); + 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; } } -- cgit v0.10.2-6-g49f6