Skip to content

Commit f6dc28e

Browse files
committed
tmp srt file is now explicitly written in utf8
1 parent a4d29f3 commit f6dc28e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/SubtitleUtilities.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.io.BufferedWriter;
2+
import java.io.EOFException;
13
import java.io.File;
24
import java.io.IOException;
35
import java.nio.charset.Charset;
@@ -13,26 +15,37 @@ class SubtitleUtilities {
1315
* That break iOS native player and thus should be replaced
1416
*/
1517
public static Map.Entry<Integer, String> replaceAllWeirdCharsFromFile(String f) throws IOException {
18+
Charset charset = Charset.forName("UTF-8");
19+
1620
int weirdCharsCount = 0;
1721
Set<String> weirdCharsList = new HashSet<>();
1822

19-
String text = new String(Files.readAllBytes(Paths.get(f)), Charset.forName("UTF-8"));
23+
String text = new String(Files.readAllBytes(Paths.get(f)), charset);
2024
char[] textArray = text.toCharArray();
2125

2226
for(int i=0; i<textArray.length; i++) {
2327
if(text.codePointAt(i)>500&&i!=0) {
2428
textArray[i] = '-';
2529
weirdCharsList.add(String.valueOf(text.charAt(i)));
2630

27-
2831
//System.out.println(textArray[i]);
2932
System.out.println(i + " " + text.charAt(i) + " " + text.codePointAt(i) );
3033
weirdCharsCount++;
3134
}
3235
}
3336

3437
text = String.valueOf(textArray);
35-
Files.write(Paths.get(f.substring(0, f.length() - 4) + "_clean.srt"), text.getBytes());
38+
39+
// we have to use this construct as Java8 write does not allow Charset encoding
40+
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(f.substring(0, f.length() - 4) + "_clean.srt"), charset) ) {
41+
//Files.write(Paths.get(f.substring(0, f.length() - 4) + "_clean.srt"), text.getBytes());
42+
writer.write(text);
43+
writer.flush();
44+
} catch (EOFException e) {
45+
Log.appendToInfoArea(e.toString());
46+
e.printStackTrace();
47+
}
48+
3649
return new AbstractMap.SimpleEntry<>(weirdCharsCount, String.join(", ", weirdCharsList));
3750
}
3851

0 commit comments

Comments
 (0)