Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

128
LINES

< > BotCompany Repo | #1035675 // BitHead - ByteHead extended with bit operations (extension with trailing bit count handling, dev.)

JavaX fragment (include)

sclass BitHead > ByteHead {
  gettable int align = 8;
  gettable int currentByte;
  int eofBits;
  int nextByte;
  
  replace SomethingIO with BitIO.
  
  *() {}
  *(InputStream inputStream) { super(inputStream); }
  *(OutputStream outputStream) { super(outputStream); }

  // choose fast or slow version depending on alignment
  void write(byte[] data) {
    if (align == 0)
      super.write(data);
    else
      for (b : data) write(b);
  }
  
  void writeByte(int i) {
    if (align == 0)
      super.writeByte(i);
    else {
      currentByte |= i << align;
      super.writeByte(currentByte);
      currentByte = i >> (8-align);
    }
  }
  
  void writeBit(int i) {
    writeBit((i & 1) != 0);
  }
  
  void writeBit(bool b) {
    if (b) currentByte |= 1 << align;
    if (align == 7) {
      super.writeByte(currentByte);
      currentByte = align = 0;
    } else
      ++align;
  }
  
  bool readBit() {
    bool bitSet = peekBit();
    advanceBit();
    ret bitSet;
  }
  
  bool peekBit() {
    if (currentByte < 0)
      fail("eof");
      
    if (align == 8)
      bufferNextByte();
    ret (currentByte & (1 << align)) != 0;
  }
  
  void bufferNextByte {
    if (align == 8) {
      // first read ever
      currentByte = read();
      align = 0;
      if (currentByte < 0) ret; // EOF
      nextByte = read();
    } else {
      currentByte = nextByte;
      if (currentByte >= 0)
        nextByte = read();
    }
  }
  
  bool isEOF() {
  }
  
  void advanceBit {
    if (currentByte < 0) ret;
    if (align == 7)
      bufferNextByte();
    else
      ++align;
  }
  
  bool byteAligned() {
    ret align == 0;
  }
  
  void completeByte aka flushBits aka finish(bool padWithOnes default false) {
    if (byteAligned()) ret;
    if (padWithOnes) currentByte |= 0xFF << align;
    super.writeByte(currentByte);
    currentByte = align = 0;
  }
  
  // TODO: switch to more compact version saving 5 bits on average
  void writeTrailingBitCount aka trailingBitCount(bool padWithOnes default false) {
    if (!writeMode()) ret;
    
    int bitCount = modRange_incl(align(), 1, 8);
    completeByte(padWithOnes);
    writeByte(bitCount);
  }
  
  void exchange(SomethingIO writable) {
    if (writable != null) writable.readWrite(this);
  }
  
  void exchangeBit(IF0<Bool> getter, IVF1<Bool> setter) {
    if (writeMode())
      writeBit(getter!);

    if (readMode())
      setter.get(readBit());
  }
  
  void exchangeBit(int i) { exchangeBit(odd(i)); }
  void exchangeBit(bool i) {
    exchangeBit(-> i, j -> assertEquals(i, j));
  }
  
  void exchange(BitIO getter, Runnable setter) {
    if (writeMode())
      setter.run();
    
    if (readMode())
      getter.readWrite(this);
  }
}

Author comment

Began life as a copy of #1035655

download  show line numbers  debug dex  old transpilations   

Travelled to 1 computer(s): mqqgnosmbjvj

No comments. add comment

-
Snippet ID: #1035675
Snippet name: BitHead - ByteHead extended with bit operations (extension with trailing bit count handling, dev.)
Eternal ID of this version: #1035675/1
Text MD5: 181c7e9c784294d7adbc369e458b91ba
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-03 02:42:47
Source code size: 2833 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 119 / 126
Referenced in: