/** * Causes the currently handled word to be written to the stream */ @Override public void flush() { flipWord(); }
/** * Causes the currently handled word to be written to the stream */ @Override public void flush() { flipWord(); }
private void checkAndFlipByte() { // Wish I could avoid this check in most cases... if(bitsLeft == 0) { flipWord(); } }
private void checkAndFlipByte() { // Wish I could avoid this check in most cases... if(bitsLeft == 0) { flipWord(); } }
/** * Writes the given long to the stream using bits amount of meaningful bits. This command does not * check input values, so if they're larger than what can fit the bits (you should check this before writing), * expect some weird results. * * @param value Value to be written to the stream * @param bits How many bits are stored to the stream */ public void writeBits(long value, int bits) { if(bits <= bitsLeft) { int lastBitPosition = bitsLeft - bits; lB |= (value << lastBitPosition) & MASK_ARRAY[bitsLeft - 1]; bitsLeft -= bits; checkAndFlipByte(); // We could be at 0 bits left because of the <= condition .. would it be faster with // the other one? } else { value &= MASK_ARRAY[bits - 1]; int firstBitPosition = bits - bitsLeft; lB |= value >>> firstBitPosition; bits -= bitsLeft; flipWord(); lB |= value << (64 - bits); bitsLeft -= bits; } }
/** * Writes the given long to the stream using bits amount of meaningful bits. This command does not * check input values, so if they're larger than what can fit the bits (you should check this before writing), * expect some weird results. * * @param value Value to be written to the stream * @param bits How many bits are stored to the stream */ public void writeBits(long value, int bits) { if(bits <= bitsLeft) { int lastBitPosition = bitsLeft - bits; lB |= (value << lastBitPosition) & MASK_ARRAY[bitsLeft - 1]; bitsLeft -= bits; checkAndFlipByte(); // We could be at 0 bits left because of the <= condition .. would it be faster with // the other one? } else { value &= MASK_ARRAY[bits - 1]; int firstBitPosition = bits - bitsLeft; lB |= value >>> firstBitPosition; bits -= bitsLeft; flipWord(); lB |= value << (64 - bits); bitsLeft -= bits; } }