private static synchronized boolean browseFolderForSubtitles( File subFolder, File file, DLNAMediaInfo media, boolean usecache) { boolean found = false; if (!usecache) { cache = null; } if (cache == null) { cache = new HashMap<>(); } final Set<String> supported = SubtitleType.getSupportedFileExtensions(); File[] allSubs = cache.get(subFolder); if (allSubs == null) { allSubs = subFolder.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { String ext = FilenameUtils.getExtension(name).toLowerCase(); if ("sub".equals(ext)) { // Avoid microdvd/vobsub confusion by ignoring sub+idx pairs here since // they'll come in unambiguously as vobsub via the idx file anyway return isFileExists(new File(dir, name), "idx") == null; } return supported.contains(ext); } }); if (allSubs != null) { cache.put(subFolder, allSubs); } } String fileName = getFileNameWithoutExtension(file.getName()).toLowerCase(); if (allSubs != null) { for (File f : allSubs) { if (f.isFile() && !f.isHidden()) { String fName = f.getName().toLowerCase(); for (String ext : supported) { if (fName.length() > ext.length() && fName.startsWith(fileName) && endsWithIgnoreCase(fName, "." + ext)) { int a = fileName.length(); int b = fName.length() - ext.length() - 1; String code = ""; if (a <= b) { // handling case with several dots: <video>..<extension> code = fName.substring(a, b); } if (code.startsWith(".")) { code = code.substring(1); } boolean exists = false; if (media != null) { for (DLNAMediaSubtitle sub : media.getSubtitleTracksList()) { if (f.equals(sub.getExternalFile())) { exists = true; } else if (equalsIgnoreCase(ext, "idx") && sub.getType() == SubtitleType.MICRODVD) { // sub+idx => VOBSUB sub.setType(SubtitleType.VOBSUB); exists = true; } else if (equalsIgnoreCase(ext, "sub") && sub.getType() == SubtitleType.VOBSUB) { // VOBSUB try { sub.setExternalFile(f); } catch (FileNotFoundException ex) { LOGGER.warn("Exception during external subtitles scan.", ex); } exists = true; } } } if (!exists) { DLNAMediaSubtitle sub = new DLNAMediaSubtitle(); sub.setId( 100 + (media == null ? 0 : media.getSubtitleTracksList().size())); // fake id, not used if (code.length() == 0 || !Iso639.getCodeList().contains(code)) { sub.setLang(DLNAMediaSubtitle.UND); sub.setType(SubtitleType.valueOfFileExtension(ext)); if (code.length() > 0) { sub.setFlavor(code); if (sub.getFlavor().contains("-")) { String flavorLang = sub.getFlavor().substring(0, sub.getFlavor().indexOf('-')); String flavorTitle = sub.getFlavor().substring(sub.getFlavor().indexOf('-') + 1); if (Iso639.getCodeList().contains(flavorLang)) { sub.setLang(flavorLang); sub.setFlavor(flavorTitle); } } } } else { sub.setLang(code); sub.setType(SubtitleType.valueOfFileExtension(ext)); } try { sub.setExternalFile(f); } catch (FileNotFoundException ex) { LOGGER.warn("Exception during external subtitles scan.", ex); } found = true; if (media != null) { media.getSubtitleTracksList().add(sub); } } } } } } } return found; }