Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 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: 14-Dec-2006
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import uk.me.parabola.log.Logger;
20
21 /**
22 * A class to write the bitstream.
23 *
24 * @author Steve Ratcliffe
25 */
26 public class BitWriter {
27 private static final Logger log = Logger.getLogger(BitWriter.class);
28
29 // Choose so that most roads will not fill it.
30 private static final int INITIAL_BUF_SIZE = 20;
31
32 // The byte buffer and its current length (allocated length)
33 private byte[] buf; // The buffer
34 private int bufsize; // The allocated size
35 private int buflen; // The actual used length
36
37 // The bit offset into the byte array.
38 private int bitoff;
39 private static final int BUFSIZE_INC = 50;
40
41 public BitWriter() {
42 bufsize = INITIAL_BUF_SIZE;
43 buf = new byte[bufsize];
44 }
45
46 /**
47 * Put exactly one bit into the buffer.
48 *
49 * @param b The bottom bit of the integer is set at the current bit position.
50 */
51 private void put1(int b) {
52 ensureSize(bitoff + 1);
53
54 int off = getByteOffset(bitoff);
55
56 // Get the remaining bits into the byte.
57 int rem = bitoff - 8 * off;
58
59 // Or it in, we are assuming that the position is never turned back.
60 buf[off] |= (b & 0x1) << rem;
61
62 // Increment position
63 bitoff++;
64
65 // If we are in a new byte, increase the byte length.
66 if ((bitoff & 0x7) == 1)
67 buflen++;
68
69 debugPrint(b, 1);
70 }
71
72 public void put1(boolean b) {
73 put1(b ? 1 : 0);
74 }
75
76 /**
77 * Put a number of bits into the buffer, growing it if necessary.
78 *
79 * @param bval The bits to add, the lowest <b>n</b> bits will be added to
80 * the buffer.
81 * @param nb The number of bits.
82 */
83 public void putn(int bval, int nb) {
84 int val = bval & ((1<<nb) - 1);
85 int n = nb;
86
87 // We need to be able to deal with more than 24 bits, but now we can't yet
88 if (n >= 24)
89 throw new IllegalArgumentException();
90
91 ensureSize(bitoff + n);
92
93 // Get each affected byte and set bits into it until we are done.
94 while (n > 0) {
95 int ind = getByteOffset(bitoff);
96 int rem = bitoff - 8*ind;
97
98 buf[ind] |= ((val << rem) & 0xff);
99
100 // Shift down in preparation for next byte.
101 val >>>= 8-rem;
102
103 // Account for change so far
104 int nput = 8 - rem;
105 if (nput > n)
106 nput = n;
107 bitoff += nput;
108 n -= nput;
109 }
110
111 buflen = (bitoff+7)/8;
112 }
113
114 /**
115 * Write a signed value. If the value doesn't fit into nb bits, write one or more 1 << (nb-1)
116 * as a flag for extended range.
117 */
118
119 public void sputn(int bval, int nb) {
120 int top = 1 << (nb - 1);
121 int mask = top - 1;
122 int val = Math.abs(bval);
123 while (val > mask) {
124 putn(top, nb);
125 val -= mask;
126 }
127 if (bval < 0) {
128 putn((top - val) | top, nb);
129 } else {
130 putn(val, nb);
131 }
132 }
133
134 public byte[] getBytes() {
135 return buf;
136 }
137
138 public int getBitPosition() {
139 return bitoff;
140 }
141 /**
142 * Get the number of bytes actually used to hold the bit stream. This therefore can be and usually
143 * is less than the length of the buffer returned by {@link #getBytes()}.
144 * @return Number of bytes required to hold the output.
145 */
146 public int getLength() {
147 return buflen;
148 }
149
150 /**
151 * Get the byte offset for the given bit number.
152 *
153 * @param boff The number of the bit in question.
154 * @return The index into the byte array where the bit resides.
155 */
156 private int getByteOffset(int boff) {
157 return boff/8;
158 }
159
160 /**
161 * Set everything up so that the given size can be accommodated.
162 * The buffer is re-sized if necessary.
163 *
164 * @param newlen The new length of the bit buffer in bits.
165 */
166 private void ensureSize(int newlen) {
167 if (newlen/8 >= bufsize)
168 reallocBuffer();
169 }
170
171 /**
172 * Reallocate the byte buffer.
173 */
174 private void reallocBuffer() {
175 log.debug("reallocating buffer");
176 bufsize += BUFSIZE_INC;
177 byte[] newbuf = new byte[bufsize];
178
179 System.arraycopy(this.buf, 0, newbuf, 0, this.buf.length);
180 this.buf = newbuf;
181 }
182
183 private void debugPrint(int b, int i) {
184 if (log.isDebugEnabled())
185 log.debug("after put", i, "of", b, "bufsize=", bufsize, ", len=",
186 buflen, ", pos=", bitoff);
187 }
188
189
190 }