Skip to content

Commit 46944a0

Browse files
committed
initial commit
0 parents  commit 46944a0

6 files changed

Lines changed: 406 additions & 0 deletions

File tree

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.iusenko</groupId>
6+
<artifactId>SubRipTools</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>SubRipTools</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.10</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iusenko.subrip;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.iusenko.subrip;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
/**
11+
*
12+
* @author iusenko
13+
*/
14+
public class BufferManager {
15+
16+
private static final int BUFFER_SIZE = 1024;
17+
private RandomAccessFile file;
18+
private String path;
19+
private long position = 0;
20+
private byte[] buffer = new byte[BUFFER_SIZE];
21+
22+
BufferManager(String path, long position) throws FileNotFoundException, IOException {
23+
if (position < 0) {
24+
throw new IllegalArgumentException("File: " + path + ", initial position is negative");
25+
}
26+
this.path = path;
27+
this.file = new RandomAccessFile(path, "r");
28+
29+
this.position = position;
30+
if (position < this.file.length()) {
31+
this.file.seek(this.position);
32+
} else {
33+
34+
}
35+
}
36+
37+
BufferManager(String path) throws FileNotFoundException, IOException {
38+
this(path, 0);
39+
}
40+
41+
List<Phrase> getNext() throws Exception {
42+
if (position < this.file.length()) {
43+
44+
file.seek(position);
45+
int read = readIntoBuffer();
46+
if (read == 0) {
47+
48+
} else {
49+
position += read;
50+
51+
return parse(buffer, read);
52+
}
53+
}
54+
return Collections.EMPTY_LIST;
55+
}
56+
57+
List<Phrase> getPrev() throws Exception {
58+
if (position > 0) {
59+
position = (position - BUFFER_SIZE) > 0 ? (position - BUFFER_SIZE) : 0;
60+
if (position == 0) {
61+
// throw event : start reached
62+
}
63+
64+
file.seek(position);
65+
int read = readIntoBuffer();
66+
if (read == 0) {
67+
} else {
68+
position += read;
69+
return parse(buffer, read);
70+
}
71+
} else {
72+
}
73+
return Collections.EMPTY_LIST;
74+
}
75+
76+
protected int readIntoBuffer() throws IOException {
77+
int read = file.read(buffer);
78+
if (read == -1) {
79+
// throw event
80+
81+
return 0;
82+
}
83+
return read;
84+
}
85+
86+
protected List<Phrase> parse(byte[] buffer, int length) throws Exception {
87+
ByteArrayInputStream in = new ByteArrayInputStream(buffer, 0, length);
88+
SubRipParser subParser = new SubRipParser(in);
89+
List<Phrase> phrases;
90+
try {
91+
phrases = subParser.getPhrases();
92+
} catch (Exception ex) {
93+
throw ex;
94+
}
95+
return phrases;
96+
}
97+
98+
public void close() {
99+
try {
100+
file.close();
101+
} catch (IOException ex) {
102+
// ignore it
103+
}
104+
}
105+
106+
public String getPath() {
107+
return path;
108+
}
109+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.iusenko.subrip;
2+
3+
import java.io.Serializable;
4+
5+
public class Phrase implements Serializable {
6+
7+
private int number;
8+
private String text;
9+
private String fromTime;
10+
private String toTime;
11+
12+
public Phrase(String text) {
13+
this.text = text;
14+
this.fromTime = "00:00:00.00";
15+
this.toTime = "00:00:00.00";
16+
}
17+
18+
public Phrase() {
19+
}
20+
21+
public String getText() {
22+
return text;
23+
}
24+
25+
public void setText(String text) {
26+
this.text = text;
27+
}
28+
29+
public String getFromTime() {
30+
return fromTime;
31+
}
32+
33+
public void setFromTime(String fromTime) {
34+
this.fromTime = fromTime;
35+
}
36+
37+
public String getToTime() {
38+
return toTime;
39+
}
40+
41+
public void setToTime(String toTime) {
42+
this.toTime = toTime;
43+
}
44+
45+
public int getNumber() {
46+
return number;
47+
}
48+
49+
public void setNumber(int number) {
50+
this.number = number;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
StringBuilder sb = new StringBuilder();
56+
sb.append("number=").append(number);
57+
sb.append(", time=").append(fromTime).append(":").append(toTime);
58+
sb.append(", text=").append(text);
59+
return sb.toString();
60+
}
61+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.iusenko.subrip;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.regex.Pattern;
9+
10+
class SubRipParser {
11+
12+
private static final Pattern phraseNumberPattern = Pattern.compile("^\\d+");
13+
private static final Pattern timePattern = Pattern.compile("\\d+(:\\d+)+,\\d+\\s+-->\\s+\\d+(:\\d+)+,\\d+");
14+
private BufferedReader in;
15+
16+
protected SubRipParser() {
17+
// for testing purpose
18+
}
19+
20+
public SubRipParser(InputStream in) {
21+
if (in == null) {
22+
throw new IllegalArgumentException();
23+
}
24+
this.in = new BufferedReader(new InputStreamReader(in));
25+
}
26+
27+
public List<Phrase> getPhrases() throws Exception {
28+
List<Phrase> phrases = new ArrayList<Phrase>();
29+
String line;
30+
StringBuilder buffer = new StringBuilder();
31+
Phrase phrase = new Phrase();
32+
33+
while ((line = in.readLine()) != null) {
34+
line = line.trim();
35+
36+
if (isPhraseNumber(line)) {
37+
phrase = new Phrase();
38+
phrase.setNumber(Integer.parseInt(line));
39+
buffer = new StringBuilder();
40+
continue;
41+
}
42+
43+
if (isBlank(line)) {
44+
phrase.setText(buffer.toString().trim());
45+
boolean fromBlank = isBlank(phrase.getFromTime());
46+
boolean toBlank = isBlank(phrase.getToTime());
47+
boolean textBlank = isBlank(phrase.getText());
48+
if (!fromBlank && !toBlank && !textBlank) {
49+
phrases.add(phrase);
50+
}
51+
continue;
52+
}
53+
54+
if (isTime(line)) {
55+
phrase.setFromTime(getStartTime(line));
56+
phrase.setToTime(getEndTime(line));
57+
continue;
58+
}
59+
60+
if (isSentence(line)) {
61+
buffer.append(" ").append(line).append("\n");
62+
}
63+
}
64+
return phrases;
65+
}
66+
67+
protected final boolean isSentence(String line) {
68+
if (isBlank(line) || isTime(line) || isPhraseNumber(line)) {
69+
return false;
70+
}
71+
return true;
72+
}
73+
74+
protected final boolean isTime(String line) {
75+
return timePattern.matcher(line).matches();
76+
}
77+
78+
protected final boolean isPhraseNumber(String line) {
79+
return phraseNumberPattern.matcher(line).matches();
80+
}
81+
82+
protected final String getStartTime(String rawString) {
83+
return getTime(rawString, true);
84+
}
85+
86+
protected final String getEndTime(String rawString) {
87+
return getTime(rawString, false);
88+
}
89+
90+
protected final String getTime(String rawString, boolean getStartTime) {
91+
int index = rawString.indexOf("-->");
92+
if (index == -1) {
93+
return "";
94+
}
95+
96+
rawString = rawString.replaceAll(",", ".");
97+
if (getStartTime) {
98+
return rawString.substring(0, index).trim();
99+
} else {
100+
return rawString.substring(index + 3).trim();
101+
}
102+
}
103+
104+
private boolean isBlank(String string) {
105+
return string == null || "".equals(string.trim());
106+
}
107+
}

0 commit comments

Comments
 (0)