-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM3UParser.java
More file actions
91 lines (72 loc) · 2.15 KB
/
M3UParser.java
File metadata and controls
91 lines (72 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.ArrayList;
public class M3UParser {
public M3UParser() throws Exception {
}
@SuppressWarnings("resource")
public String convertStreamToString(java.io.InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
}
public ArrayList<M3UHolder> parseFile(File f) throws FileNotFoundException {
ArrayList<M3UHolder> myArray = new ArrayList<M3UHolder>();
if (f.exists()) {
String stream = convertStreamToString(new FileInputStream(f));
stream = stream.replaceAll("#EXTM3U", "").trim();
String[] arr = stream.split("#EXTINF.*,");
String lines[] = null;
String name = "";
String url = "";
String code = "";
{
for (int n = 0; n < arr.length; n++) {
lines = arr[n].split("\\r?\\n");
if(lines!=null && lines.length==2) {
if (endsWithDigits(lines[1])) {
for (int i = 0; i<lines.length;i++) {
if (i==0) {
name = lines[i];
}else {
url = lines[i];
code = lastBigInteger(lines[i]).toString();
M3UHolder m3uHold = new M3UHolder();
m3uHold.setName(name);
m3uHold.setUrl(url);
m3uHold.setCode(code);
myArray.add(m3uHold);
}
}
}
}
}
}
}
return myArray;
}
private static boolean endsWithDigits(String s) {
String shortString = s.substring(s.length() -3, s.length());
if (shortString == null) {
return false;
}
try {
@SuppressWarnings("unused")
double d = Double.parseDouble(shortString);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private static BigInteger lastBigInteger(String s) {
int i = s.length();
while (i > 0 && Character.isDigit(s.charAt(i - 1))) {
i--;
}
return new BigInteger(s.substring(i));
}
}