Bitwise IO: Difference between revisions
m
→{{header|Ecstasy}}
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36, explicit :prototype) |
|||
Line 1,423:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BitwiseIO {
class BitReader {
construct(Byte[] bytes) {▼
{▼
▲ construct(Byte[] bytes)
this.bits = bytes.toBitArray();
private Bit[] bits;
private Int index;
Int offset
@Override
Int get() {
return index;
@Override
void set(Int offset) {
assert 0 <= offset < size;
index = offset;
}
Int size.get()
return bits.size;
Boolean eof.get()
return index >= size;
Bit readBit() {
return eof ? assert:bounds : bits[index++];
Byte readByte() {
assert:bounds index + 8 <= size as $"eof (offset={index}, size={size}";
Int start = index;
index += 8;
return bits[start ..< index].toByte();
}
class BitWriter {
private Bit[] bits = new Bit[];
BitWriter writeBit(Bit bit) {
bits.add(bit);
return this;
BitWriter writeByte(Byte byte) {
bits.addAll(byte.toBitArray());
return this;
Byte[] bytes.get() {
// "zero fill" the bits to the next byte boundary: if the bits don't currently stop at
// a byte boundary, then calc the number of "extra" bits (bits.size & 0x7) and append
Line 1,498 ⟶ 1,484:
bits += bits.size & 0x7 == 0 ? [] : Byte:0.toBitArray() [bits.size & 0x7 ..< 8];
return bits.toByteArray();
}
@Inject Console console;
void run() {
▲ Bit[] orig = [0,1,0,1,0,1,1,1,0,1,0,1,0]; // hexadecimal 57 50 (with LSB padding)
val out = new BitWriter();
Line 1,515 ⟶ 1,500:
val test = new Bit[orig.size]((Int i) -> in.readBit());
assert test == orig;
▲ }
}
}
</syntaxhighlight>
|