* cache MediaInfo instances

This commit is contained in:
Reinhard Pointner 2015-11-14 17:25:35 +00:00
parent 6c1109ced1
commit 7103d4216e
1 changed files with 28 additions and 9 deletions

View File

@ -29,6 +29,7 @@ import java.util.Scanner;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -64,6 +65,7 @@ public class MediaBindingBean {
private final File mediaFile; private final File mediaFile;
private final Map<File, Object> context; private final Map<File, Object> context;
private String mediaInfoKey;
private MediaInfo mediaInfo; private MediaInfo mediaInfo;
private Object metaInfo; private Object metaInfo;
@ -876,19 +878,36 @@ public class MediaBindingBean {
} }
} }
private static final Map<String, MediaInfo> sharedMediaInfoObjects = new WeakHashMap<String, MediaInfo>(64);
private synchronized MediaInfo getMediaInfo() { private synchronized MediaInfo getMediaInfo() {
if (mediaInfo == null) {
// make sure media file is defined // make sure media file is defined
checkMediaFile(); checkMediaFile();
MediaInfo newMediaInfo = new MediaInfo(); // lazy initialize
if (mediaInfo == null) {
// lazy initialize
if (mediaInfoKey == null) {
// 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())) { try {
throw new RuntimeException("Cannot open media file: " + mediaFile); // make sure to create a new String object which can be garbage collected as soon the binding object not used anymore
mediaInfoKey = new String(getInferredMediaFile().getCanonicalPath());
} catch (IOException e) {
throw new IllegalStateException(e);
}
} }
mediaInfo = newMediaInfo; synchronized (sharedMediaInfoObjects) {
mediaInfo = sharedMediaInfoObjects.get(mediaInfoKey);
if (mediaInfo == null) {
MediaInfo mi = new MediaInfo();
if (!mi.open(new File(mediaInfoKey))) {
throw new RuntimeException("Cannot open media file: " + mediaInfoKey);
}
sharedMediaInfoObjects.put(mediaInfoKey, mi);
mediaInfo = mi;
}
}
} }
return mediaInfo; return mediaInfo;