Inlined BitBuffer.getBytes() into its single usage (with simplification because the bit length is a multiple of 8) in most language versions, synchronized comment and variable name in Rust version.

This commit is contained in:
Project Nayuki
2018-10-06 04:44:03 +00:00
parent 8c262c00dd
commit c7bc281e18
9 changed files with 40 additions and 69 deletions
-15
View File
@@ -77,21 +77,6 @@ public final class BitBuffer implements Cloneable {
}
/**
* Returns an array representing this buffer's bits packed into bytes in big endian. If the
* bit length isn't a multiple of 8, then the remaining bits of the final byte are all '0'.
* @return a new byte array (not {@code null}) representing this bit sequence
*/
public byte[] getBytes() {
byte[] result = new byte[(bitLength + 7) >>> 3]; // Round up to whole byte, won't overflow
for (int i = 0; i < bitLength; i++) {
if (data.get(i))
result[i >>> 3] |= 1 << (7 - (i & 7));
}
return result;
}
/**
* Appends the specified number of low-order bits of the specified value to this
* buffer. Requires 0 &#x2264; len &#x2264; 31 and 0 &#x2264; val &lt; 2<sup>len</sup>.
+6 -1
View File
@@ -184,8 +184,13 @@ public final class QrCode {
for (int padByte = 0xEC; bb.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
bb.appendBits(padByte, 8);
// Pack bits into bytes in big endian
byte[] dataCodewords = new byte[bb.bitLength() / 8];
for (int i = 0; i < bb.bitLength(); i++)
dataCodewords[i >>> 3] |= bb.getBit(i) << (7 - (i & 7));
// Create the QR Code object
return new QrCode(version, ecl, bb.getBytes(), mask);
return new QrCode(version, ecl, dataCodewords, mask);
}