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

52
LINES

< > BotCompany Repo | #1035731 // HuffmanByteArray - Huffman codec [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (15231L/90K).

1  
persistable sclass HuffmanByteArray is BitIO {
2  
  settable byte[] data;
3  
  gettable int[] histogram;
4  
  gettable HuffmanTreeMaker tm;
5  
  gettable new HuffmanTree tree;
6  
  
7  
  // options
8  
  settable bool startWithMagicCode;
9  
  settable S magicCode = "HUF1";
10  
  
11  
  *(byte[] *data) {}
12  
13  
  public void readWrite(BitHead head) {
14  
    if (head.writeMode() && histogram == null) {
15  
      histogram = byteHistogramArray(data);
16  
      tm = new HuffmanTreeMaker;
17  
      tm.importByteHistogram(histogram);
18  
      tree = tm!;
19  
    }
20  
21  
    // Part 1 (magic code)
22  
    if (startWithMagicCode)
23  
      head.exchangeASCII(magicCode);
24  
  
25  
    // Part 2 (Huffman character tree)
26  
    tree.readWrite(head);
27  
28  
    // Part 3a (size)
29  
30  
    new VariableSizeUIntForBitHead().chunkSize(3).readWrite(head, -> data.length, size -> data = new byte[size]);
31  
    //print("length " + data.length);
32  
    
33  
    // Part 3 (text)
34  
    if (head.writeMode()) {
35  
      fOr (b : data)
36  
        tree.writeCharacter(b, head);
37  
    }
38  
    
39  
    if (head.readMode()) {
40  
      for i over data: {
41  
        if (head.isEOF()) break;
42  
        data[i] = toUByte(tree.readSymbol(head));
43  
      }
44  
    }
45  
  }
46  
  
47  
  byte[] get() { ret data; }
48  
  
49  
  // update selfType
50  
  public selfType fromByteArray(byte[] data) { BitIO.super.fromByteArray(data); this; }
51  
  public selfType load(byte[] data) { BitIO.super.load(data); this; }
52  
}

Author comment

Began life as a copy of #1035674

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, onxytkatvevr

No comments. add comment

Snippet ID: #1035731
Snippet name: HuffmanByteArray - Huffman codec [OK]
Eternal ID of this version: #1035731/11
Text MD5: 53e855445a153fb8f77787f862df7555
Transpilation MD5: e9e4978b3f68996a4e9f87e3509db72d
Author: stefan
Category: javax / compression
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-15 20:56:00
Source code size: 1385 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 96 / 198
Version history: 10 change(s)
Referenced in: [show references]