Be robust against unexpected EOS in WebvttCueParser

Issue: #3228

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167504122
This commit is contained in:
olly 2017-09-04 09:26:59 -07:00 committed by Oliver Woodman
parent 472df08f08
commit a0df5bb50a

View File

@ -21,6 +21,7 @@ import android.text.Layout.Alignment;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan; import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan; import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan; import android.text.style.BackgroundColorSpan;
@ -92,20 +93,25 @@ import java.util.regex.Pattern;
/* package */ boolean parseCue(ParsableByteArray webvttData, WebvttCue.Builder builder, /* package */ boolean parseCue(ParsableByteArray webvttData, WebvttCue.Builder builder,
List<WebvttCssStyle> styles) { List<WebvttCssStyle> styles) {
String firstLine = webvttData.readLine(); String firstLine = webvttData.readLine();
if (firstLine == null) {
return false;
}
Matcher cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(firstLine); Matcher cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(firstLine);
if (cueHeaderMatcher.matches()) { if (cueHeaderMatcher.matches()) {
// We have found the timestamps in the first line. No id present. // We have found the timestamps in the first line. No id present.
return parseCue(null, cueHeaderMatcher, webvttData, builder, textBuilder, styles); return parseCue(null, cueHeaderMatcher, webvttData, builder, textBuilder, styles);
} else { }
// The first line is not the timestamps, but could be the cue id. // The first line is not the timestamps, but could be the cue id.
String secondLine = webvttData.readLine(); String secondLine = webvttData.readLine();
if (secondLine == null) {
return false;
}
cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(secondLine); cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(secondLine);
if (cueHeaderMatcher.matches()) { if (cueHeaderMatcher.matches()) {
// We can do the rest of the parsing, including the id. // We can do the rest of the parsing, including the id.
return parseCue(firstLine.trim(), cueHeaderMatcher, webvttData, builder, textBuilder, return parseCue(firstLine.trim(), cueHeaderMatcher, webvttData, builder, textBuilder,
styles); styles);
} }
}
return false; return false;
} }
@ -233,7 +239,7 @@ import java.util.regex.Pattern;
// Parse the cue text. // Parse the cue text.
textBuilder.setLength(0); textBuilder.setLength(0);
String line; String line;
while ((line = webvttData.readLine()) != null && !line.isEmpty()) { while (!TextUtils.isEmpty(line = webvttData.readLine())) {
if (textBuilder.length() > 0) { if (textBuilder.length() > 0) {
textBuilder.append("\n"); textBuilder.append("\n");
} }