Issue 361
[zxing.git] / core / src / com / google / zxing / common / BitArray.java
index bab2a3a..6c150bf 100644 (file)
@@ -28,12 +28,14 @@ public final class BitArray {
   // resulting binary at runtime on Android. If we find a solution to this, these should be changed\r
   // back to private.\r
   public int[] bits;\r
-  public final int size;\r
+  public int size;\r
+\r
+  public BitArray() {\r
+    this.size = 0;\r
+    this.bits = new int[1];\r
+  }\r
 \r
   public BitArray(int size) {\r
-    if (size < 1) {\r
-      throw new IllegalArgumentException("size must be at least 1");\r
-    }\r
     this.size = size;\r
     this.bits = makeArray(size);\r
   }\r
@@ -42,6 +44,18 @@ public final class BitArray {
     return size;\r
   }\r
 \r
+  public int getSizeInBytes() {\r
+    return (size + 7) >> 3;\r
+  }\r
+\r
+  private void ensureCapacity(int size) {\r
+    if (size > bits.length << 5) {\r
+      int[] newBits = makeArray(size);\r
+      System.arraycopy(bits, 0, newBits, 0, bits.length);\r
+      this.bits = newBits;\r
+    }\r
+  }\r
+\r
   /**\r
    * @param i bit to get\r
    * @return true iff bit i is set\r
@@ -130,6 +144,69 @@ public final class BitArray {
     return true;\r
   }\r
 \r
+  public void appendBit(boolean bit) {\r
+    ensureCapacity(size + 1);\r
+    if (bit) {\r
+      bits[size >> 5] |= (1 << (size & 0x1F));\r
+    }\r
+    size++;\r
+  }\r
+\r
+  /**\r
+   * Appends the least-significant bits, from value, in order from most-significant to\r
+   * least-significant. For example, appending 6 bits from 0x000001E will append the bits\r
+   * 0, 1, 1, 1, 1, 0 in that order.\r
+   */\r
+  public void appendBits(int value, int numBits) {\r
+    if (numBits < 0 || numBits > 32) {\r
+      throw new IllegalArgumentException("Num bits must be between 0 and 32");\r
+    }\r
+    ensureCapacity(size + numBits);\r
+    for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) {\r
+      appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1);\r
+    }\r
+  }\r
+\r
+  public void appendBitArray(BitArray other) {\r
+    int otherSize = other.getSize();\r
+    ensureCapacity(size + otherSize);\r
+    for (int i = 0; i < otherSize; i++) {\r
+      appendBit(other.get(i));\r
+    }\r
+  }\r
+\r
+  public void xor(BitArray other) {\r
+    if (bits.length != other.bits.length) {\r
+      throw new IllegalArgumentException("Sizes don't match");\r
+    }\r
+    for (int i = 0; i < bits.length; i++) {\r
+      // The last byte could be incomplete (i.e. not have 8 bits in\r
+      // it) but there is no problem since 0 XOR 0 == 0.\r
+      bits[i] ^= other.bits[i];\r
+    }\r
+  }\r
+\r
+  /**\r
+   *\r
+   * @param bitOffset first bit to start writing\r
+   * @param array array to write into. Bytes are written most-significant byte first. This is the opposite\r
+   *  of the internal representation, which is exposed by {@link #getBitArray()}\r
+   * @param offset position in array to start writing\r
+   * @param numBytes how many bytes to write\r
+   */\r
+  public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) {\r
+    for (int i = 0; i < numBytes; i++) {\r
+      int theByte = 0;\r
+      for (int j = 0; j < 8; j++) {\r
+        if (get(bitOffset)) {\r
+          theByte |= 1 << (7 - j);\r
+        }\r
+        bitOffset++;\r
+      }\r
+      array[offset + i] = (byte) theByte;\r
+    }\r
+  }\r
+\r
   /**\r
    * @return underlying array of ints. The first element holds the first 32 bits, and the least\r
    *         significant bit is bit 0.\r
@@ -153,11 +230,7 @@ public final class BitArray {
   }\r
 \r
   private static int[] makeArray(int size) {\r
-    int arraySize = size >> 5;\r
-    if ((size & 0x1F) != 0) {\r
-      arraySize++;\r
-    }\r
-    return new int[arraySize];\r
+    return new int[(size + 31) >> 5];\r
   }\r
   \r
   public String toString() {\r