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

42
LINES

< > BotCompany Repo | #1019811 // decompressMozillaJSONLZ4File

JavaX fragment (include)

1  
sS decompressMozillaJSONLZ4File(File f) {
2  
  byte[] data = loadBinaryFile(f);
3  
  if (empty(data)) null;
4  
  
5  
  int lDecompressed = intFromBytes_littleEndian(data, 8);
6  
7  
  // our very own LZ4 decompressor!
8  
  int i = 12, n = l(data), iOut = 0;
9  
  byte[] out = new byte[lDecompressed];
10  
  while (i < n) {
11  
    int lLiteral = (data[i] >> 4) & 0x0F;
12  
    int lMatches = (data[i] & 0x0F)+4;
13  
    i++;
14  
    if (lLiteral == 15) {
15  
      int more;
16  
      do {
17  
        more = data[i++] & 0xFF;
18  
        lLiteral += more;
19  
      } while (more == 255);
20  
    }
21  
    arraycopy(data, i, out, iOut, lLiteral);
22  
    iOut += lLiteral;
23  
    i += lLiteral;
24  
    if (i >= n) break;
25  
    int offset = data[i] & 0xFF | (data[i+1] & 0xFF) << 8;
26  
    i += 2;
27  
    if (offset == 0) fail("bad offset");
28  
    if (lMatches == 19) {
29  
      int more;
30  
      do {
31  
        more = data[i++] & 0xFF;
32  
        lMatches += more;
33  
      } while (more == 255);
34  
    }
35  
    int j = lMatches;
36  
    while (j-- > 0) {
37  
      out[iOut] = out[iOut-offset];
38  
      iOut++;
39  
    }
40  
  }
41  
  ret fromUtf8(out);
42  
}

Author comment

Began life as a copy of #1019807

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1019811
Snippet name: decompressMozillaJSONLZ4File
Eternal ID of this version: #1019811/1
Text MD5: 3add850dfee3b23296469bf93db66ba6
Author: stefan
Category: javax / lz4
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-26 23:50:00
Source code size: 1067 bytes / 42 lines
Pitched / IR pitched: No / No
Views / Downloads: 227 / 252
Referenced in: [show references]