Move BitSource to common package so that it can be reused by Data Matrix decoder
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 10 Mar 2008 20:16:57 +0000 (20:16 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 10 Mar 2008 20:16:57 +0000 (20:16 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@259 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/common/BitSource.java [new file with mode: 0755]
core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java
core/test/src/com/google/zxing/common/BitSourceTestCase.java [new file with mode: 0644]

diff --git a/core/src/com/google/zxing/common/BitSource.java b/core/src/com/google/zxing/common/BitSource.java
new file mode 100755 (executable)
index 0000000..8d592ff
--- /dev/null
@@ -0,0 +1,96 @@
+/*\r
+ * Copyright 2007 Google Inc.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.google.zxing.common;\r
+\r
+/**\r
+ * <p>This provides an easy abstraction to read bits at a time from a sequence of bytes, where the\r
+ * number of bits read is not often a multiple of 8.</p>\r
+ *\r
+ * <p>This class is not thread-safe.</p>\r
+ *\r
+ * @author srowen@google.com (Sean Owen)\r
+ */\r
+public final class BitSource {\r
+\r
+  private final byte[] bytes;\r
+  private int byteOffset;\r
+  private int bitOffset;\r
+\r
+  /**\r
+   * @param bytes bytes from which this will read bits. Bits will be read from the first byte first.\r
+   * Bits are read within a byte from most-significant to least-significant bit.\r
+   */\r
+  public BitSource(byte[] bytes) {\r
+    this.bytes = bytes;\r
+  }\r
+\r
+  /**\r
+   * @param numBits number of bits to read\r
+   * @return int representing the bits read. The bits will appear as the least-significant\r
+   *         bits of the int\r
+   * @throws IllegalArgumentException if numBits isn't in [1,32]\r
+   */\r
+  public int readBits(int numBits) {\r
+    if (numBits < 1 || numBits > 32) {\r
+      throw new IllegalArgumentException();\r
+    }\r
+\r
+    int result = 0;\r
+\r
+    // First, read remainder from current byte\r
+    if (bitOffset > 0) {\r
+      int bitsLeft = 8 - bitOffset;\r
+      int toRead = numBits < bitsLeft ? numBits : bitsLeft;\r
+      int bitsToNotRead = bitsLeft - toRead;\r
+      int mask = (0xFF >> (8 - toRead)) << bitsToNotRead;\r
+      result = (bytes[byteOffset] & mask) >> bitsToNotRead;\r
+      numBits -= toRead;\r
+      bitOffset += toRead;\r
+      if (bitOffset == 8) {\r
+        bitOffset = 0;\r
+        byteOffset++;\r
+      }\r
+    }\r
+\r
+    // Next read whole bytes\r
+    if (numBits > 0) {\r
+      while (numBits >= 8) {\r
+        result = (result << 8) | (bytes[byteOffset] & 0xFF);\r
+        byteOffset++;\r
+        numBits -= 8;\r
+      }\r
+\r
+      // Finally read a partial byte\r
+      if (numBits > 0) {\r
+        int bitsToNotRead = 8 - numBits;\r
+        int mask = (0xFF >> bitsToNotRead) << bitsToNotRead;\r
+        result = (result << numBits) | ((bytes[byteOffset] & mask) >> bitsToNotRead);\r
+        bitOffset += numBits;\r
+      }\r
+    }\r
+\r
+    return result;\r
+  }\r
+\r
+  /**\r
+   * @return number of bits that can be read successfully\r
+   */\r
+  public int available() {\r
+    return 8 * (bytes.length - byteOffset) - bitOffset;\r
+  }\r
+\r
+}\r
index 059007d..712a0cb 100644 (file)
@@ -17,6 +17,7 @@
 package com.google.zxing.qrcode.decoder;
 
 import com.google.zxing.ReaderException;
+import com.google.zxing.common.BitSource;
 
 import java.io.UnsupportedEncodingException;
 
diff --git a/core/test/src/com/google/zxing/common/BitSourceTestCase.java b/core/test/src/com/google/zxing/common/BitSourceTestCase.java
new file mode 100644 (file)
index 0000000..3f61831
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2007 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common;
+
+import junit.framework.TestCase;
+
+/**
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class BitSourceTestCase extends TestCase {
+
+  public void testSource() {
+    byte[] bytes = new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5};
+    BitSource source = new BitSource(bytes);
+    assertEquals(40, source.available());
+    assertEquals(0, source.readBits(1));
+    assertEquals(39, source.available());
+    assertEquals(0, source.readBits(6));
+    assertEquals(33, source.available());
+    assertEquals(1, source.readBits(1));
+    assertEquals(32, source.available());
+    assertEquals(2, source.readBits(8));
+    assertEquals(24, source.available());
+    assertEquals(12, source.readBits(10));
+    assertEquals(14, source.available());
+    assertEquals(16, source.readBits(8));
+    assertEquals(6, source.available());
+    assertEquals(5, source.readBits(6));
+    assertEquals(0, source.available());
+  }
+
+}
\ No newline at end of file