diff options
author | Alan Young <consult.awy@gmail.com> | 2015-07-08 10:43:39 (GMT) |
---|---|---|
committer | Kevin Smith <kevin.smith@isode.com> | 2015-08-13 08:08:30 (GMT) |
commit | 563ab00f95281f61a0aa5ae96fb5aacedac9b900 (patch) | |
tree | fc8fd98d13d4a8c616fabcacb95b87201c49f018 /src/com/isode/stroke/base/SafeByteArray.java | |
parent | 9bf162922691c1f45813da6a21f2e274bfad3114 (diff) | |
download | stroke-563ab00f95281f61a0aa5ae96fb5aacedac9b900.zip stroke-563ab00f95281f61a0aa5ae96fb5aacedac9b900.tar.bz2 |
Rework ByteArray implementation
Use and ArrayList<byte[]> as the internal storage, instead of
Vector<Byte>.
Constructors and methods that supply a byte[] to add to the instance
will have that value copied, rather than just taking ownership of the
byte[]. There is an argument, on efficiency grounds, for providing
methods that take ownership of the supplied argument.
The earlier discussions about use of synchronization in the class are
revisited. This is a low-level container that should be thread-safe or
take precautions against non-thread-safe use. It is anticipated that the
vast majority of uses will be thread-safe. Use of synchronization
permits occasional deviations from this without imposing any significant
overhead for the normal case - uncontended synchronization is close to
free.
Change-Id: Ifb7b2915d5c96345c53aec8e33673d892c430431
Diffstat (limited to 'src/com/isode/stroke/base/SafeByteArray.java')
-rw-r--r-- | src/com/isode/stroke/base/SafeByteArray.java | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/com/isode/stroke/base/SafeByteArray.java b/src/com/isode/stroke/base/SafeByteArray.java index ac8960f..e193299 100644 --- a/src/com/isode/stroke/base/SafeByteArray.java +++ b/src/com/isode/stroke/base/SafeByteArray.java @@ -6,7 +6,6 @@ package com.isode.stroke.base; import com.isode.stroke.base.SafeByteArray; import com.isode.stroke.base.ByteArray; -import java.io.UnsupportedEncodingException; /** * It's currently not actually secure, @@ -23,7 +22,7 @@ public class SafeByteArray extends ByteArray { } public SafeByteArray(ByteArray b) { - this.append(b.getData()); + super(b); } /** @@ -46,7 +45,7 @@ public class SafeByteArray extends ByteArray { * followed by all the elements of <em>b</em>. */ public static SafeByteArray plus(SafeByteArray a, SafeByteArray b) { - SafeByteArray x = new SafeByteArray().append(a.getData()); + SafeByteArray x = new SafeByteArray(a); x.append(b); return x; } @@ -58,7 +57,7 @@ public class SafeByteArray extends ByteArray { * @return a reference to the updated object */ public SafeByteArray append(ByteArray b) { - append(b.getData()); + super.append(b); return this; } @@ -72,14 +71,13 @@ public class SafeByteArray extends ByteArray { * @return a reference to the updated object */ public SafeByteArray append(byte[] b) { - return append(b, b.length); + super.append(b); + return this; } /** Mutable add */ public SafeByteArray append(byte[] b, int len) { - for (int i = 0; i < len; i++) { - append(b[i]); - } + super.append(b, len); return this; } @@ -90,8 +88,7 @@ public class SafeByteArray extends ByteArray { * @return a reference to the updated object */ public SafeByteArray append(byte b) { - dataCopy_ = null; /* Invalidate cache */ - data_.add(Byte.valueOf(b)); + super.append(b); return this; } @@ -102,13 +99,7 @@ public class SafeByteArray extends ByteArray { * @return a reference to the updated object. */ public SafeByteArray append(String s) { - byte[] bytes; - try { - bytes = s.getBytes("UTF-8"); - } catch (UnsupportedEncodingException ex) { - throw new IllegalStateException("JVM has no 'UTF-8' encoding"); - } - append(bytes); + super.append(s); return this; } }
\ No newline at end of file |