@@ -34,11 +34,11 @@ public class JBBPBitOutputStream extends FilterOutputStream implements JBBPCount
34
34
*/
35
35
private final JBBPBitOrder bitOrderMode ;
36
36
/**
37
- * Inside bit buffer.
37
+ * Internal bit buffer.
38
38
*/
39
39
private int bitBuffer ;
40
40
/**
41
- * Number of bits inside the bit buffer.
41
+ * Number of bits buffered by the bit buffer.
42
42
*/
43
43
private int bitBufferCount ;
44
44
/**
@@ -58,7 +58,7 @@ public JBBPBitOutputStream(final OutputStream out) {
58
58
/**
59
59
* A Constructor.
60
60
*
61
- * @param out an output stream to be filtered.
61
+ * @param out an output stream to be filtered.
62
62
* @param bitOrderMode a bit writing mode to used for writing operations.
63
63
* @see JBBPBitOrder#LSB0
64
64
* @see JBBPBitOrder#MSB0
@@ -211,17 +211,17 @@ public long getCounter() {
211
211
}
212
212
213
213
/**
214
- * Get the inside bit buffer value.
214
+ * Get the internal bit buffer value.
215
215
*
216
- * @return the inside bit buffer value
216
+ * @return the internal bit buffer value
217
217
*/
218
218
@ Override
219
219
public int getBitBuffer () {
220
220
return this .bitBuffer ;
221
221
}
222
222
223
223
/**
224
- * Get the number of bits cached in the inside bit buffer.
224
+ * Get the number of bits cached in the internal bit buffer.
225
225
*
226
226
* @return the number of cached bits in the bit buffer
227
227
*/
@@ -278,29 +278,47 @@ public void write(final byte[] b) throws IOException {
278
278
*/
279
279
public void writeBits (final int value , final JBBPBitNumber bitNumber ) throws IOException {
280
280
if (this .bitBufferCount == 0 && bitNumber == JBBPBitNumber .BITS_8 ) {
281
- write (value );
281
+ this . write (value );
282
282
} else {
283
- final int initialMask ;
284
283
int mask ;
285
- initialMask = 1 ;
286
- mask = initialMask << this .bitBufferCount ;
287
-
288
284
int accumulator = value ;
289
285
int i = bitNumber .getBitNumber ();
290
286
291
- while (i > 0 ) {
292
- this .bitBuffer = this .bitBuffer | ((accumulator & 1 ) == 0 ? 0 : mask );
293
- accumulator >>= 1 ;
294
-
295
- mask = mask << 1 ;
296
-
297
- i --;
298
- this .bitBufferCount ++;
299
- if (this .bitBufferCount == 8 ) {
300
- this .bitBufferCount = 0 ;
301
- writeByte (this .bitBuffer );
302
- mask = initialMask ;
303
- this .bitBuffer = 0 ;
287
+ if (this .bitOrderMode == JBBPBitOrder .MSB0_DIRECT ) {
288
+ final int initialMask = 0x80 ;
289
+ mask = initialMask >> this .bitBufferCount ;
290
+ final int accumulatorMask = 1 << (bitNumber .getBitNumber () - 1 );
291
+ while (i > 0 ) {
292
+ this .bitBuffer = this .bitBuffer | ((accumulator & accumulatorMask ) == 0 ? 0 : mask );
293
+ accumulator <<= 1 ;
294
+ mask >>= 1 ;
295
+
296
+ i --;
297
+ this .bitBufferCount ++;
298
+ if (this .bitBufferCount == 8 ) {
299
+ this .bitBufferCount = 0 ;
300
+ writeByte (this .bitBuffer );
301
+ mask = initialMask ;
302
+ this .bitBuffer = 0 ;
303
+ }
304
+ }
305
+ } else {
306
+ final int initialMask = 1 ;
307
+ mask = initialMask << this .bitBufferCount ;
308
+ while (i > 0 ) {
309
+ this .bitBuffer = this .bitBuffer | ((accumulator & 1 ) == 0 ? 0 : mask );
310
+
311
+ accumulator >>= 1 ;
312
+ mask = mask << 1 ;
313
+
314
+ i --;
315
+ this .bitBufferCount ++;
316
+ if (this .bitBufferCount == 8 ) {
317
+ this .bitBufferCount = 0 ;
318
+ writeByte (this .bitBuffer );
319
+ mask = initialMask ;
320
+ this .bitBuffer = 0 ;
321
+ }
304
322
}
305
323
}
306
324
}
@@ -328,7 +346,7 @@ public void align(final long alignByteNumber) throws IOException {
328
346
}
329
347
330
348
/**
331
- * Inside method to write a byte into wrapped stream.
349
+ * Internal method to write a byte into wrapped stream.
332
350
*
333
351
* @param value a byte value to be written
334
352
* @throws IOException it will be thrown for transport problems
@@ -350,9 +368,9 @@ public void close() throws IOException {
350
368
@ Override
351
369
public void write (final int value ) throws IOException {
352
370
if (this .bitBufferCount == 0 ) {
353
- writeByte (value );
371
+ this . writeByte (value );
354
372
} else {
355
- writeBits (value , JBBPBitNumber .BITS_8 );
373
+ this . writeBits (value , JBBPBitNumber .BITS_8 );
356
374
}
357
375
}
358
376
@@ -379,7 +397,7 @@ public void writeBytes(final byte[] array, final int length, final JBBPByteOrder
379
397
}
380
398
381
399
/**
382
- * Reset the byte counter for the stream. The Inside bit buffer will be reset also.
400
+ * Reset the byte counter for the stream. The internal bit buffer will be reset also.
383
401
*/
384
402
@ Override
385
403
public void resetCounter () {
0 commit comments