Debian Codegraph @

src/uk/me/parabola/imgfmt/app/BitReader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 29-Aug-2008
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 /**
20 * Read an array as a bit stream.
21 *
22 * @author Steve Ratcliffe
23 */
24 public class BitReader {
25 private final byte[] buf;
26 private int bitPosition;
27
28 public BitReader(byte[] buf) {
29 this.buf = buf;
30 }
31
32 public boolean get1() {
33 int off = bitPosition % 8;
34 byte b = buf[bitPosition / 8];
35
36 bitPosition++;
37 return ((b >> off) & 1) == 1;
38 }
39
40 public int get(int n) {
41 int res = 0;
42
43 int pos = 0;
44 while (pos < n) {
45 int index = bitPosition / 8;
46 int off = bitPosition % 8;
47
48 byte b = buf[index];
49 b >>= off;
50 int nbits = n - pos;
51 if (nbits > (8-off))
52 nbits = 8 - off;
53
54 int mask = ((1 << nbits) - 1);
55 res |= ((b & mask) << pos);
56 pos += nbits;
57 bitPosition += nbits;
58 }
59
60 return res;
61 }
62
63 /**
64 * Get a signed quantity.
65 *
66 * The sign bit is the last bit in the field.
67 *
68 * @param n The field width, including the sign bit.
69 * @return A signed number.
70 */
71 public int sget(int n) {
72 int res = get(n);
73 int top = 1 << (n - 1);
74
75 if ((res & top) != 0) {
76 int mask = top - 1;
77 res = ~mask | res;
78 }
79 return res;
80 }
81
82 /**
83 * Get a signed n-bit value, treating 1 << (n-1) as a flag to read another signed n-bit value
84 * for extended range.
85 */
86 public int sget2(int n) {
87 int top = 1 << (n - 1);
88 int mask = top - 1;
89 int base = 0;
90
91 int res = get(n);
92 while (res == top) {
93 // Add to the base value, and read another
94 base += mask;
95 res = get(n);
96 }
97
98 // The final byte determines the sign of the result. Add or subtract the base as
99 // appropriate.
100 if ((res & top) == 0)
101 res += base;
102 else
103 res = (res | ~mask) - base; // Make negative and subtract the base
104
105 return res;
106 }
107
108 public int getBitPosition() {
109 return bitPosition;
110 }
111
112 public int getNumberOfBits() {
113 return buf.length * 8;
114 }
115
116 /**
117 * Debugging routine that returns the remainder of the stream as a string.
118 * The bits are in little endian order, so that numbers can be read from left
119 * to right, although the whole string has to read from right to left.
120 *
121 * @return A string in binary.
122 */
123 public String remainder() {
124 int save = bitPosition;
125 StringBuilder sb = new StringBuilder();
126 while (bitPosition < buf.length * 8) {
127 sb.insert(0, get1() ? "1" : "0");
128 }
129 bitPosition = save;
130 return sb.toString();
131 }
132 }