Updated Java BitBuffer code to add methods and make the class public.

pull/16/head
Project Nayuki 7 years ago
parent 493d3c31f2
commit 7a2555816b

@ -30,7 +30,7 @@ import java.util.Objects;
/**
* An appendable sequence of bits. Bits are packed in big endian within a byte.
*/
final class BitBuffer {
public final class BitBuffer implements Cloneable {
/*---- Fields ----*/
@ -57,6 +57,14 @@ final class BitBuffer {
}
// Returns the bit at the given index, yielding 0 or 1, or throwing IndexOutOfBoundsException.
public int getBit(int index) {
if (index < 0 || index > bitLength)
throw new IndexOutOfBoundsException();
return data.get(index) ? 1 : 0;
}
// Returns a copy of all bytes, padding up to the nearest byte. Bits are packed in big endian within a byte.
public byte[] getBytes() {
byte[] result = new byte[(bitLength + 7) / 8];
@ -85,4 +93,16 @@ final class BitBuffer {
}
}
// Returns a copy of this bit buffer object.
public BitBuffer clone() {
try {
BitBuffer result = (BitBuffer)super.clone();
result.data = (BitSet)result.data.clone();
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
}

Loading…
Cancel
Save