Bit reading helpers.
| 18 | * Bit reading helpers. |
| 19 | */ |
| 20 | final class BitReader { |
| 21 | |
| 22 | // Possible values: {5, 6}. 5 corresponds to 32-bit build, 6 to 64-bit. This value is used for |
| 23 | // JIT conditional compilation. |
| 24 | private static final int LOG_BITNESS = Utils.getLogBintness(); |
| 25 | |
| 26 | // Not only Java compiler prunes "if (const false)" code, but JVM as well. |
| 27 | // Code under "if (BIT_READER_DEBUG != 0)" have zero performance impact (outside unit tests). |
| 28 | private static final int BIT_READER_DEBUG = Utils.isDebugMode(); |
| 29 | |
| 30 | static final int BITNESS = 1 << LOG_BITNESS; |
| 31 | |
| 32 | private static final int BYTENESS = BITNESS / 8; |
| 33 | private static final int CAPACITY = 4096; |
| 34 | // After encountering the end of the input stream, this amount of zero bytes will be appended. |
| 35 | private static final int SLACK = 64; |
| 36 | private static final int BUFFER_SIZE = CAPACITY + SLACK; |
| 37 | // Don't bother to replenish the buffer while this number of bytes is available. |
| 38 | private static final int SAFEGUARD = 36; |
| 39 | private static final int WATERLINE = CAPACITY - SAFEGUARD; |
| 40 | |
| 41 | // "Half" refers to "half of native integer type", i.e. on 64-bit machines it is 32-bit type, |
| 42 | // on 32-bit machines it is 16-bit. |
| 43 | private static final int HALF_BITNESS = BITNESS / 2; |
| 44 | private static final int HALF_SIZE = BYTENESS / 2; |
| 45 | private static final int HALVES_CAPACITY = CAPACITY / HALF_SIZE; |
| 46 | private static final int HALF_BUFFER_SIZE = BUFFER_SIZE / HALF_SIZE; |
| 47 | |
| 48 | private static final int LOG_HALF_SIZE = LOG_BITNESS - 4; |
| 49 | |
| 50 | static final int HALF_WATERLINE = WATERLINE / HALF_SIZE; |
| 51 | |
| 52 | /** |
| 53 | * Fills up the input buffer. |
| 54 | * |
| 55 | * <p> Should not be called if there are at least 36 bytes present after current position. |
| 56 | * |
| 57 | * <p> After encountering the end of the input stream, 64 additional zero bytes are copied to the |
| 58 | * buffer. |
| 59 | */ |
| 60 | static int readMoreInput(State s) { |
| 61 | if (s.endOfStreamReached != 0) { |
| 62 | if (halfAvailable(s) >= -2) { |
| 63 | return BROTLI_OK; |
| 64 | } |
| 65 | return Utils.makeError(s, BROTLI_ERROR_TRUNCATED_INPUT); |
| 66 | } |
| 67 | final int readOffset = s.halfOffset << LOG_HALF_SIZE; |
| 68 | int bytesInBuffer = CAPACITY - readOffset; |
| 69 | // Move unused bytes to the head of the buffer. |
| 70 | Utils.copyBytesWithin(s.byteBuffer, 0, readOffset, CAPACITY); |
| 71 | s.halfOffset = 0; |
| 72 | while (bytesInBuffer < CAPACITY) { |
| 73 | final int spaceLeft = CAPACITY - bytesInBuffer; |
| 74 | final int len = Utils.readInput(s, s.byteBuffer, bytesInBuffer, spaceLeft); |
| 75 | if (len < BROTLI_ERROR) { |
| 76 | return len; |
| 77 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…