Example #1
0
  /** Convert a MD5MD5CRC32FileChecksum to a Json string. */
  public static String toJsonString(final MD5MD5CRC32FileChecksum checksum) {
    if (checksum == null) {
      return null;
    }

    final Map<String, Object> m = new TreeMap<String, Object>();
    m.put("algorithm", checksum.getAlgorithmName());
    m.put("length", checksum.getLength());
    m.put("bytes", StringUtils.byteToHexString(checksum.getBytes()));
    return toJsonString(MD5MD5CRC32FileChecksum.class, m);
  }
Example #2
0
  /** Convert a Json map to a MD5MD5CRC32FileChecksum. */
  public static MD5MD5CRC32FileChecksum toMD5MD5CRC32FileChecksum(final Map<?, ?> json)
      throws IOException {
    if (json == null) {
      return null;
    }

    final Map<?, ?> m = (Map<?, ?>) json.get(MD5MD5CRC32FileChecksum.class.getSimpleName());
    final String algorithm = (String) m.get("algorithm");
    final int length = (int) (long) (Long) m.get("length");
    final byte[] bytes = StringUtils.hexStringToByte((String) m.get("bytes"));

    final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    final int bytesPerCRC = in.readInt();
    final long crcPerBlock = in.readLong();
    final MD5Hash md5 = MD5Hash.read(in);
    final MD5MD5CRC32FileChecksum checksum =
        new MD5MD5CRC32FileChecksum(bytesPerCRC, crcPerBlock, md5);

    // check algorithm name
    final String alg = "MD5-of-" + crcPerBlock + "MD5-of-" + bytesPerCRC + "CRC32";
    if (!alg.equals(algorithm)) {
      throw new IOException(
          "Algorithm not matched: algorithm="
              + algorithm
              + ", crcPerBlock="
              + crcPerBlock
              + ", bytesPerCRC="
              + bytesPerCRC);
    }
    // check length
    if (length != checksum.getLength()) {
      throw new IOException(
          "Length not matched: length="
              + length
              + ", checksum.getLength()="
              + checksum.getLength());
    }

    return checksum;
  }
Example #3
0
  /** Convert a Json map to a MD5MD5CRC32FileChecksum. */
  public static MD5MD5CRC32FileChecksum toMD5MD5CRC32FileChecksum(final Map<?, ?> json)
      throws IOException {
    if (json == null) {
      return null;
    }

    final Map<?, ?> m = (Map<?, ?>) json.get(FileChecksum.class.getSimpleName());
    final String algorithm = (String) m.get("algorithm");
    final int length = (int) (long) (Long) m.get("length");
    final byte[] bytes = StringUtils.hexStringToByte((String) m.get("bytes"));

    final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    final DataChecksum.Type crcType =
        MD5MD5CRC32FileChecksum.getCrcTypeFromAlgorithmName(algorithm);
    final MD5MD5CRC32FileChecksum checksum;

    // Recreate what DFSClient would have returned.
    switch (crcType) {
      case CRC32:
        checksum = new MD5MD5CRC32GzipFileChecksum();
        break;
      case CRC32C:
        checksum = new MD5MD5CRC32CastagnoliFileChecksum();
        break;
      default:
        throw new IOException("Unknown algorithm: " + algorithm);
    }
    checksum.readFields(in);

    // check algorithm name
    if (!checksum.getAlgorithmName().equals(algorithm)) {
      throw new IOException(
          "Algorithm not matched. Expected "
              + algorithm
              + ", Received "
              + checksum.getAlgorithmName());
    }
    // check length
    if (length != checksum.getLength()) {
      throw new IOException(
          "Length not matched: length="
              + length
              + ", checksum.getLength()="
              + checksum.getLength());
    }

    return checksum;
  }