| 5 | |
| 6 | // RFC2118 |
| 7 | public class Compress implements Codec { |
| 8 | private final @NotNull Codec sink; |
| 9 | private int pos; |
| 10 | private int rem; |
| 11 | private final byte[] dict = new byte[8192]; |
| 12 | private final short[] hash = new short[65536]; |
| 13 | private int idx; |
| 14 | private int match_idx; |
| 15 | private int match_off = -1; |
| 16 | private int match_len; |
| 17 | private boolean flushed = true; |
| 18 | |
| 19 | public Compress(@NotNull Codec sink) { |
| 20 | this.sink = sink; |
| 21 | Arrays.fill(hash, (short)-1); |
| 22 | } |
| 23 | |
| 24 | protected int getPos() { |
| 25 | return pos; |
| 26 | } |
| 27 | |
| 28 | protected void putBits(int val, int nbits) throws CodecException { |
| 29 | pos += nbits; |
| 30 | rem |= val << (32 - pos); |
| 31 | while (pos > 7) { |
| 32 | sink.update((byte)(rem >> 24)); |
| 33 | pos -= 8; |
| 34 | rem <<= 8; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private void putLiteral(byte c) throws CodecException { |
| 39 | if (c >= 0) |
| 40 | putBits(c, 8); // 0xxx xxxx |
| 41 | else |
| 42 | putBits(c & 0x7f | 0x100, 9); // 1 0xxx xxxx |
| 43 | } |
| 44 | |
| 45 | private void putTuple(int off, int len) throws CodecException { |
| 46 | if (off < 64) |
| 47 | putBits(0x3c0 | off, 10); // 11 11xx xxxx |
| 48 | else if (off < 320) |
| 49 | putBits(0xe00 | (off - 64), 12); // 1110 xxxx xxxx |
| 50 | else |
| 51 | putBits(0xc000 | (off - 320), 16); // 110x xxxx xxxx xxxx |
| 52 | if (len < 4) // len == 3 |
| 53 | putBits(0, 1); // 0 |
| 54 | else if (len < 8) |
| 55 | putBits(0x08 | (len & 0x03), 4); // 10xx |
| 56 | else if (len < 16) |
| 57 | putBits(0x30 | (len & 0x07), 6); // 11 0xxx |
| 58 | else if (len < 32) |
| 59 | putBits(0xe0 | (len & 0x0f), 8); // 1110 xxxx |
| 60 | else if (len < 64) |
| 61 | putBits(0x3c0 | (len & 0x1f), 10); // 11 110x xxxx |
| 62 | else if (len < 128) |
| 63 | putBits(0xf80 | (len & 0x3f), 12); // 1111 10xx xxxx |
| 64 | else if (len < 256) |
nothing calls this directly
no outgoing calls
no test coverage detected