Skip to content

Instantly share code, notes, and snippets.

@zwxbest
Created May 28, 2019 11:50
Show Gist options
  • Save zwxbest/edc4d1035a8aff7e075f87b0441ed943 to your computer and use it in GitHub Desktop.
Save zwxbest/edc4d1035a8aff7e075f87b0441ed943 to your computer and use it in GitHub Desktop.
checkSum
package util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.CRC32;
import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.internal.util.scanner.Resource;
import org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource;
/**
* @author zhangweixiao - 19-2-22
*/
public class CheckSum {
/**
* Calculates the checksum of this string.
*
* @param filePath 脚本文件的绝对路径
* @return The crc-32 checksum of the bytes.
* @throws Exception
*/
/* private -> for testing */
public static int calculateChecksum(String filePath) throws Exception {
Resource resource = new FileSystemResource(filePath);
final CRC32 crc32 = new CRC32();
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
try {
String line;
while ((line = bufferedReader.readLine()) != null) {
crc32.update(line.getBytes("UTF-8"));
}
} catch (IOException e) {
String message = "Unable to calculate checksum";
if (resource != null) {
message += " for " + resource.getLocation() + " (" + resource.getLocationOnDisk() + ")";
}
throw new FlywayException(message, e);
}
return (int) crc32.getValue();
}
public static void main(String[] args) throws Exception {
System.out.println(calculateChecksum("/home/zhenyu/work/rmb/rmb-uc2b/src/main/resources/db/migration/V2_23_20190411170201__dml.sql"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment