fix log to reflect reality
[zxing.git] / csharp / common / BitSource.cs
index 91df66e..983caa7 100755 (executable)
@@ -1,5 +1,5 @@
-/*\r
-* Copyright 2008 ZXing authors\r
+/*\r
+* Copyright 2007 ZXing authors\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
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 */\r
+using System;\r
 namespace com.google.zxing.common\r
 {\r
-    using System;\r
-    using System.Text;\r
-\r
-    /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
-    /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
-    /// *\r
-    /// JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have\r
-    /// the horizontal component first, but this is for compatibility with the C++ code. The original\r
-    /// code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use\r
-    /// less memory and go with bytes.\r
-    /// *\r
-    /// </summary>\r
-    /// <author>  dswitkin@google.com (Daniel Switkin)\r
-    /// \r
-    /// </author>\r
-    public sealed class BitSource\r
-    { \r
-          private sbyte[] 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(sbyte[] 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 Exception();\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
+       /// <summary> <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 thread-safe but not reentrant. Unless the caller modifies the bytes array\r
+       /// it passed in, in which case all bets are off.</p>\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class BitSource\r
+       {\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'bytes '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private sbyte[] bytes;\r
+               private int byteOffset;\r
+               private int bitOffset;\r
+               \r
+               /// <param name="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
+               /// </param>\r
+               public BitSource(sbyte[] bytes)\r
+               {\r
+                       this.bytes = bytes;\r
+               }\r
+               \r
+               /// <param name="numBits">number of bits to read\r
+               /// </param>\r
+               /// <returns> int representing the bits read. The bits will appear as the least-significant\r
+               /// bits of the int\r
+               /// </returns>\r
+               /// <throws>  IllegalArgumentException if numBits isn't in [1,32] </throws>\r
+               public int readBits(int numBits)\r
+               {\r
+                       if (numBits < 1 || numBits > 32)\r
+                       {\r
+                               throw new System.ArgumentException();\r
+                       }\r
+                       \r
+                       int result = 0;\r
+                       \r
+                       // First, read remainder from current byte\r
+                       if (bitOffset > 0)\r
+                       {\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
+                               {\r
+                                       bitOffset = 0;\r
+                                       byteOffset++;\r
+                               }\r
+                       }\r
+                       \r
+                       // Next read whole bytes\r
+                       if (numBits > 0)\r
+                       {\r
+                               while (numBits >= 8)\r
+                               {\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
+                               {\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
+               /// <returns> number of bits that can be read successfully\r
+               /// </returns>\r
+               public int available()\r
+               {\r
+                       return 8 * (bytes.Length - byteOffset) - bitOffset;\r
+               }\r
+       }\r
 }
\ No newline at end of file