--- /dev/null
+/*\r
+ * 11/19/04 1.0 moved to LGPL.\r
+ * \r
+ * 12/12/99 0.0.7 Implementation stores single bits \r
+ * as ints for better performance. mdm@techie.com.\r
+ *\r
+ * 02/28/99 0.0 Java Conversion by E.B, javalayer@javazoom.net\r
+ *\r
+ * Adapted from the public c code by Jeff Tsay.\r
+ *\r
+ *-----------------------------------------------------------------------\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU Library General Public License as published\r
+ * by the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU Library General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Library General Public\r
+ * License along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
+ *----------------------------------------------------------------------\r
+ */\r
+\r
+/**\r
+ * Implementation of Bit Reservoir for Layer III.\r
+ * <p>\r
+ * The implementation stores single bits as a word in the buffer. If a bit is\r
+ * set, the corresponding word in the buffer will be non-zero. If a bit is\r
+ * clear, the corresponding word is zero. Although this may seem waseful, this\r
+ * can be a factor of two quicker than packing 8 bits to a byte and extracting.\r
+ * <p>\r
+ */\r
+\r
+// REVIEW: there is no range checking, so buffer underflow or overflow\r
+// can silently occur.\r
+\r
+\r
+final class BitReserve {\r
+ /**\r
+ * Size of the internal buffer to store the reserved bits. Must be a power of\r
+ * 2. And x8, as each bit is stored as a single entry.\r
+ */\r
+ private static final int BUFSIZE = 4096 * 8;\r
+\r
+ /**\r
+ * Mask that can be used to quickly implement the modulus operation on\r
+ * BUFSIZE.\r
+ */\r
+ private static final int BUFSIZE_MASK = BUFSIZE - 1;\r
+\r
+ \r
+ private int offset;\r
+\r
+ \r
+ public int totbit;\r
+\r
+ \r
+ public int buf_byte_idx;\r
+\r
+ \r
+ private final int[] buf;\r
+\r
+ BitReserve() {\r
+ offset = 0;\r
+ totbit = 0;\r
+ buf_byte_idx = 0;\r
+ buf = new int[BUFSIZE];\r
+ }\r
+\r
+ /**\r
+ * Return totbit Field.\r
+ */\r
+ \r
+ public int hsstell() {\r
+ return (totbit);\r
+ }\r
+\r
+ /**\r
+ * Read a number bits from the bit stream.\r
+ * \r
+ * @param N\r
+ * the number of\r
+ */\r
+ public int hgetbits( int N) {\r
+\r
+ totbit += N;\r
+\r
+ int val = 0;\r
+\r
+ int pos = buf_byte_idx;\r
+ if (pos + N < BUFSIZE) {\r
+ TERMINATE: while (N-- > 0) {\r
+ val <<= 1;\r
+ val |= ((buf[pos++] != 0) ? 1 : 0);\r
+ }\r
+ } else {\r
+ TERMINATE: while (N-- > 0) {\r
+ val <<= 1;\r
+ val |= ((buf[pos] != 0) ? 1 : 0);\r
+ pos = (pos + 1) & BUFSIZE_MASK;\r
+ }\r
+ }\r
+\r
+ buf_byte_idx = pos;\r
+\r
+ return val;\r
+\r
+ }\r
+\r
+ /**\r
+ * Returns next bit from reserve.\r
+ * \r
+ * @returns 0 if next bit is reset, or 1 if next bit is set.\r
+ */\r
+ \r
+ public int hget1bit() {\r
+ totbit++;\r
+ int val = buf[buf_byte_idx];\r
+ buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;\r
+ return val;\r
+ }\r
+\r
+ /**\r
+ * Write 8 bits into the bit stream.\r
+ */\r
+ \r
+ public void hputbuf( int val) {\r
+ int ofs = offset;\r
+ buf[ofs++] = val & 0x80;\r
+ buf[ofs++] = val & 0x40;\r
+ buf[ofs++] = val & 0x20;\r
+ buf[ofs++] = val & 0x10;\r
+ buf[ofs++] = val & 0x08;\r
+ buf[ofs++] = val & 0x04;\r
+ buf[ofs++] = val & 0x02;\r
+ buf[ofs++] = val & 0x01;\r
+\r
+ if (ofs == BUFSIZE)\r
+ offset = 0;\r
+ else\r
+ offset = ofs;\r
+\r
+ }\r
+\r
+ /**\r
+ * Rewind N bits in Stream.\r
+ */\r
+ public void rewindNbits( int N) {\r
+ totbit -= N;\r
+ buf_byte_idx -= N;\r
+ if (buf_byte_idx < 0)\r
+ buf_byte_idx += BUFSIZE;\r
+ }\r
+\r
+ /**\r
+ * Rewind N bytes in Stream.\r
+ */\r
+ \r
+ public void rewindNbytes( int N) {\r
+ int bits = (N << 3);\r
+ totbit -= bits;\r
+ buf_byte_idx -= bits;\r
+ if (buf_byte_idx < 0)\r
+ buf_byte_idx += BUFSIZE;\r
+ }\r
+}\r