Пример #1
0
  /**
   * Parsing method for metafile headers, saving each value into separate variable.
   *
   * @param s String containing metafile
   * @return Boolean value notifying whether header ended or not (true = end of header)
   */
  private boolean parseHeader(String s) {
    String subs;
    int colonIndex;
    if (s.equals("")) {
      // timto prazdnym radkem skoncil header, muzeme prestat cist
      return true;
    }
    colonIndex = s.indexOf(":");
    subs = s.substring(0, colonIndex);
    if (subs.equalsIgnoreCase("zsync")) {
      headers.version = s.substring(colonIndex + 2);
      // zkontrolujeme kompatibilitu
      if (headers.version.equals("0.0.4") || headers.version.equals("0.0.2")) {
        throw new RuntimeException(
            "This version is not compatible with zsync streams in versions up to 0.0.4");
      }
    } else if (subs.equalsIgnoreCase("Blocksize")) {
      headers.blocksize = Integer.parseInt(s.substring(colonIndex + 2));
    } else if (subs.equalsIgnoreCase("Length")) {
      headers.length = Long.parseLong(s.substring(colonIndex + 2));
    } else if (subs.equalsIgnoreCase("Hash-Lengths")) {
      int comma = s.indexOf(",");
      int seqNum = Integer.parseInt(s.substring((colonIndex + 2), comma));
      headers.setSeqNum(seqNum);
      int nextComma = s.indexOf(",", comma + 1);
      headers.setRsumBytes(Integer.parseInt(s.substring(comma + 1, nextComma)));
      headers.setChecksumBytes(Integer.parseInt(s.substring(nextComma + 1)));
      if ((headers.getSeqNum() < 1 || headers.getSeqNum() > 2)
          || (headers.getRsumButes() < 1 || headers.getRsumButes() > 4)
          || (headers.getChecksumBytes() < 3 || headers.getChecksumBytes() > 16)) {
        throw new RuntimeException("Nonsensical hash lengths line " + s.substring(colonIndex + 2));
      }

    } else if (subs.equalsIgnoreCase("URL")) {
      headers.url = s.substring(colonIndex + 2);
    } else if (subs.equalsIgnoreCase("Z-URL")) {
      // not implemented yet
    } else if (subs.equalsIgnoreCase("SHA-1")) {
      headers.sha1 = s.substring(colonIndex + 2);
    } else if (subs.equalsIgnoreCase("Z-Map2")) {
      // not implemented yet
    }
    return false;
  }
Пример #2
0
  /**
   * Fills a chaining hash table with ChecksumPairs
   *
   * @param checksums Byte array with bytes of whole metafile
   */
  private void fillHashTable(byte[] checksums) {
    int i = 16;
    // spocteme velikost hashtable podle poctu bloku dat
    while ((2 << (i - 1)) > blockNum && i > 4) {
      i--;
    }
    // vytvorime hashtable o velikosti 2^i (max. 2^16, min. 2^4)
    hashtable = new ChainingHash(2 << (i - 1));
    ChecksumPair p = null;
    // Link item;
    int offset = 0;
    int weakSum = 0;
    int seq = 0;
    int off = fileOffset;

    byte[] weak = new byte[4];
    byte[] strongSum = new byte[headers.getChecksumBytes()];

    while (seq < blockNum) {

      for (int w = 0; w < headers.getRsumButes(); w++) {
        weak[w] = checksums[off];
        off++;
      }

      for (int s = 0; s < strongSum.length; s++) {
        strongSum[s] = checksums[off];
        off++;
      }

      weakSum = 0;
      weakSum += (weak[2] & 0x000000FF) << 24;
      weakSum += (weak[3] & 0x000000FF) << 16;
      weakSum += (weak[0] & 0x000000FF) << 8;
      weakSum += (weak[1] & 0x000000FF);

      p = new ChecksumPair(weakSum, strongSum.clone(), offset, headers.blocksize, seq);
      offset += headers.blocksize;
      seq++;
      // item = new Link(p);
      hashtable.insert(p);
    }
  }
Пример #3
0
 /**
  * Length of used strong sum
  *
  * @return Length of strong sum
  */
 public int getChecksumBytes() {
   return headers.getChecksumBytes();
 }