| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app; |
| 18 |
|
| 19 |
import uk.me.parabola.log.Logger; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class BitWriter { |
| 27 |
private static final Logger log = Logger.getLogger(BitWriter.class); |
| 28 |
|
| 29 |
|
| 30 |
private static final int INITIAL_BUF_SIZE = 20; |
| 31 |
|
| 32 |
|
| 33 |
private byte[] buf; |
| 34 |
private int bufsize; |
| 35 |
private int buflen; |
| 36 |
|
| 37 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private void put1(int b) { |
| 52 |
ensureSize(bitoff + 1); |
| 53 |
|
| 54 |
int off = getByteOffset(bitoff); |
| 55 |
|
| 56 |
|
| 57 |
int rem = bitoff - 8 * off; |
| 58 |
|
| 59 |
|
| 60 |
buf[off] |= (b & 0x1) << rem; |
| 61 |
|
| 62 |
|
| 63 |
bitoff++; |
| 64 |
|
| 65 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void putn(int bval, int nb) { |
| 84 |
int val = bval & ((1<<nb) - 1); |
| 85 |
int n = nb; |
| 86 |
|
| 87 |
|
| 88 |
if (n >= 24) |
| 89 |
throw new IllegalArgumentException(); |
| 90 |
|
| 91 |
ensureSize(bitoff + n); |
| 92 |
|
| 93 |
|
| 94 |
while (n > 0) { |
| 95 |
int ind = getByteOffset(bitoff); |
| 96 |
int rem = bitoff - 8*ind; |
| 97 |
|
| 98 |
buf[ind] |= ((val << rem) & 0xff); |
| 99 |
|
| 100 |
|
| 101 |
val >>>= 8-rem; |
| 102 |
|
| 103 |
|
| 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 |
|
| 116 |
|
| 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 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public int getLength() { |
| 147 |
return buflen; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private int getByteOffset(int boff) { |
| 157 |
return boff/8; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private void ensureSize(int newlen) { |
| 167 |
if (newlen/8 >= bufsize) |
| 168 |
reallocBuffer(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 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 |
} |