From ba633729407d885e5b1c627eebf9b6aa11ac05bb Mon Sep 17 00:00:00 2001 From: jwat Date: Thu, 7 Jul 2011 15:09:03 +0900 Subject: [PATCH] Fix some bugs on BIT_STRING. --- jp/bitmeister/asn1/type/builtin/BIT_STRING.java | 51 ++++++++++++++++--------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/jp/bitmeister/asn1/type/builtin/BIT_STRING.java b/jp/bitmeister/asn1/type/builtin/BIT_STRING.java index 86b2a7e..7a4bb37 100644 --- a/jp/bitmeister/asn1/type/builtin/BIT_STRING.java +++ b/jp/bitmeister/asn1/type/builtin/BIT_STRING.java @@ -152,7 +152,6 @@ public class BIT_STRING extends PrimitiveType implements * Instantiates an empty {@code BIT_STRING}. */ public BIT_STRING() { - set(new boolean[0]); } /** @@ -182,12 +181,10 @@ public class BIT_STRING extends PrimitiveType implements * specified by the indexes. * * @param indexes - * The value to be assigned. + * Indexes of bits to be set true. */ public BIT_STRING(int... indexes) { - for (int e : indexes) { - set(e); - } + set(indexes); } /** @@ -201,31 +198,37 @@ public class BIT_STRING extends PrimitiveType implements } /** - * Sets {@code true} to a bit specified by the index number. If the + * Sets {@code true} to bits specified by index numbers. If the * {@code boolean} array of the value of this data does not have enough size - * to hold the bit specified by the index, it will be expanded + * to hold the bit specified by the biggest index, it will be expanded * automatically. * * @param index - * The index of a bit. + * Indexes of bits to be set true. */ - public void set(int index) { - expand(index); - value()[index] = true; + public void set(int ...indexes) { + Arrays.sort(indexes); + expand(indexes[indexes.length - 1]); + for (int e: indexes) { + value()[e] = true; + } } /** - * Sets {@code false} to a bit specified by the index number. If the + * Sets {@code false} to bits specified by index numbers. If the * {@code boolean} array of the value of this data does not have enough size - * to hold the bit specified by the index, it will be expanded + * to hold the bit specified by the biggest index, it will be expanded * automatically. * * @param index - * The index of a bit. + * Indexes of bits to be set false. */ - public void unset(int index) { - expand(index); - value()[index] = false; + public void unset(int ...indexes) { + Arrays.sort(indexes); + expand(indexes[indexes.length - 1]); + for (int e: indexes) { + value()[e] = false; + } } /** @@ -233,6 +236,10 @@ public class BIT_STRING extends PrimitiveType implements * size to hold the last {@code true} bit. */ public void contract() { + if (value() == null) { + set(new boolean[0]); + return; + } int index = size(); for (index--; index >= 0; index--) { if (value()[index]) { @@ -290,7 +297,9 @@ public class BIT_STRING extends PrimitiveType implements private void expand(int index) { if (size() <= index) { boolean[] newValue = new boolean[index + 1]; - System.arraycopy(value(), 0, newValue, 0, size()); + if (size() > 0) { + System.arraycopy(value(), 0, newValue, 0, size()); + } set(newValue); } } @@ -335,6 +344,9 @@ public class BIT_STRING extends PrimitiveType implements */ @Override public int size() { + if (value() == null) { + return 0; + } return value().length; } @@ -345,6 +357,9 @@ public class BIT_STRING extends PrimitiveType implements */ @Override protected boolean[] cloneValue() { + if (value() == null) { + return null; + } return value().clone(); } -- 2.11.0