Esempio n. 1
0
  /**
   * Fill the sliding dictionary with more data.
   *
   * @throws IOException
   */
  private void fillBuffer() throws IOException {
    init();

    int bit = bits.nextBit();
    if (bit == 1) {
      // literal value
      int literal;
      if (literalTree != null) {
        literal = literalTree.read(bits);
      } else {
        literal = bits.nextBits(8);
      }

      if (literal == -1) {
        // end of stream reached, nothing left to decode
        return;
      }

      buffer.put(literal);

    } else if (bit == 0) {
      // back reference
      int distanceLowSize = dictionarySize == 4096 ? 6 : 7;
      int distanceLow = bits.nextBits(distanceLowSize);
      int distanceHigh = distanceTree.read(bits);
      if (distanceHigh == -1 && distanceLow <= 0) {
        // end of stream reached, nothing left to decode
        return;
      }
      int distance = distanceHigh << distanceLowSize | distanceLow;

      int length = lengthTree.read(bits);
      if (length == 63) {
        length += bits.nextBits(8);
      }
      length += minimumMatchLength;

      buffer.copy(distance + 1, length);
    }
  }