summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2011-07-01 12:18:52 (GMT)
committerKevin Smith <git@kismith.co.uk>2011-07-01 12:18:52 (GMT)
commit2169d8cd423c399ceb53e962ad5313e3c28d2297 (patch)
treec581d1ba8b15509c15a8fadfbc2090c915aec1ad /src/com/isode/stroke/base
parent2da71a8a85486a494343f1662d64fb5ae5a2a44e (diff)
downloadstroke-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/com/isode/stroke/base')
-rw-r--r--src/com/isode/stroke/base/ByteArray.java39
1 files changed, 20 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;
}