001package org.avaje.dbmigration.runner;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.StringReader;
006import java.util.zip.CRC32;
007
008/**
009 * Calculates the checksum for the given string content.
010 */
011public class Checksum {
012
013  /**
014   * Returns the checksum of this string.
015   */
016  public static int calculate(String str) {
017
018    final CRC32 crc32 = new CRC32();
019
020    BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
021    try {
022      String line;
023      while ((line = bufferedReader.readLine()) != null) {
024        crc32.update(line.getBytes("UTF-8"));
025      }
026    } catch (IOException e) {
027      throw new RuntimeException("Failed to calculate checksum", e);
028    }
029
030    return (int) crc32.getValue();
031  }
032}