Skip to content

Commit

Permalink
Be robust against unexpected EOS in WebvttCueParser
Browse files Browse the repository at this point in the history
Issue: #3228

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167504122
  • Loading branch information
ojw28 committed Sep 5, 2017
1 parent 472df08 commit a0df5bb
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
Expand Down Expand Up @@ -92,19 +93,24 @@ public WebvttCueParser() {
/* package */ boolean parseCue(ParsableByteArray webvttData, WebvttCue.Builder builder,
List<WebvttCssStyle> styles) {
String firstLine = webvttData.readLine();
if (firstLine == null) {
return false;
}
Matcher cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(firstLine);
if (cueHeaderMatcher.matches()) {
// We have found the timestamps in the first line. No id present.
return parseCue(null, cueHeaderMatcher, webvttData, builder, textBuilder, styles);
} else {
// The first line is not the timestamps, but could be the cue id.
String secondLine = webvttData.readLine();
cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(secondLine);
if (cueHeaderMatcher.matches()) {
// We can do the rest of the parsing, including the id.
return parseCue(firstLine.trim(), cueHeaderMatcher, webvttData, builder, textBuilder,
styles);
}
}
// The first line is not the timestamps, but could be the cue id.
String secondLine = webvttData.readLine();
if (secondLine == null) {
return false;
}
cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(secondLine);
if (cueHeaderMatcher.matches()) {
// We can do the rest of the parsing, including the id.
return parseCue(firstLine.trim(), cueHeaderMatcher, webvttData, builder, textBuilder,
styles);
}
return false;
}
Expand Down Expand Up @@ -233,7 +239,7 @@ private static boolean parseCue(String id, Matcher cueHeaderMatcher, ParsableByt
// Parse the cue text.
textBuilder.setLength(0);
String line;
while ((line = webvttData.readLine()) != null && !line.isEmpty()) {
while (!TextUtils.isEmpty(line = webvttData.readLine())) {
if (textBuilder.length() > 0) {
textBuilder.append("\n");
}
Expand Down

0 comments on commit a0df5bb

Please sign in to comment.