* code cleanup

This commit is contained in:
Reinhard Pointner 2011-09-13 01:44:54 +00:00
parent af60f6b6f1
commit 17af89b0bf
3 changed files with 39 additions and 41 deletions

View File

@ -7,11 +7,8 @@ import static net.sourceforge.filebot.format.Define.*;
import static net.sourceforge.filebot.hash.VerificationUtilities.*; import static net.sourceforge.filebot.hash.VerificationUtilities.*;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner; import java.util.Scanner;
import java.util.zip.CRC32;
import net.sf.ehcache.Cache; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
@ -245,7 +242,7 @@ public class EpisodeBindingBean {
private void checkMediaFile() throws RuntimeException { private void checkMediaFile() throws RuntimeException {
// make sure file is not null, and that it is an existing file // make sure file is not null, and that it is an existing file
if (mediaFile == null || !mediaFile.isFile()) if (mediaFile == null || !mediaFile.isFile())
throw new RuntimeException(String.format("Invalid media file: %s", mediaFile)); throw new RuntimeException("Invalid media file: " + mediaFile);
} }
@ -258,7 +255,7 @@ public class EpisodeBindingBean {
// use inferred media file (e.g. actual movie file instead of subtitle file) // use inferred media file (e.g. actual movie file instead of subtitle file)
if (!newMediaInfo.open(getInferredMediaFile())) { if (!newMediaInfo.open(getInferredMediaFile())) {
throw new RuntimeException(String.format("Cannot open media file: %s", mediaFile)); throw new RuntimeException("Cannot open media file: " + mediaFile);
} }
mediaInfo = newMediaInfo; mediaInfo = newMediaInfo;
@ -272,9 +269,8 @@ public class EpisodeBindingBean {
for (String key : keys) { for (String key : keys) {
String value = getMediaInfo().get(streamKind, streamNumber, key); String value = getMediaInfo().get(streamKind, streamNumber, key);
if (value.length() > 0) { if (value.length() > 0)
return value; return value;
}
} }
return null; return null;
@ -284,36 +280,13 @@ public class EpisodeBindingBean {
private String crc32(File file) throws IOException, InterruptedException { private String crc32(File file) throws IOException, InterruptedException {
// try to get checksum from cache // try to get checksum from cache
Cache cache = CacheManager.getInstance().getCache("checksum"); Cache cache = CacheManager.getInstance().getCache("checksum");
Element element = cache.get(file);
if (element != null)
return (String) element.getValue();
try { // compute and cache checksum
return String.format("%08X", cache.get(file).getValue()); String hash = computeHash(file, HashType.SFV);
} catch (Exception e) { cache.put(new Element(file, hash));
// checksum is not cached return hash;
}
// calculate checksum
InputStream in = new FileInputStream(file);
CRC32 crc = new CRC32();
try {
byte[] buffer = new byte[32 * 1024];
int len = 0;
while ((len = in.read(buffer)) >= 0) {
crc.update(buffer, 0, len);
// make this long-running operation interruptible
if (Thread.interrupted())
throw new InterruptedException();
}
} finally {
in.close();
}
// cache calculated checksum
cache.put(new Element(file, crc.getValue()));
return String.format("%08X", crc.getValue());
} }
} }

View File

@ -5,7 +5,9 @@ package net.sourceforge.filebot.hash;
import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.FileUtilities.*;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -84,6 +86,31 @@ public final class VerificationUtilities {
} }
public static String computeHash(File file, HashType type) throws IOException, InterruptedException {
Hash hash = type.newHash();
// calculate checksum
InputStream in = new FileInputStream(file);
try {
byte[] buffer = new byte[32 * 1024];
int len = 0;
while ((len = in.read(buffer)) >= 0) {
hash.update(buffer, 0, len);
// make this long-running operation interruptible
if (Thread.interrupted())
throw new InterruptedException();
}
} finally {
in.close();
}
return hash.digest();
}
/** /**
* Dummy constructor to prevent instantiation. * Dummy constructor to prevent instantiation.
*/ */

View File

@ -138,11 +138,9 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
try { try {
MessageDigest hash = MessageDigest.getInstance("MD5"); MessageDigest hash = MessageDigest.getInstance("MD5");
hash.update(data); hash.update(data);
return String.format("%032x", new BigInteger(1, hash.digest())); // as hex string
// return hex string
return String.format("%032x", new BigInteger(1, hash.digest()));
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); throw new RuntimeException(e); // won't happen
} }
} }