If a parser really wants to throw a runtime exception, don't crash.

This commit is contained in:
Oliver Woodman 2015-09-28 12:16:26 +01:00
parent ee80be9217
commit 71f542f7c2

View File

@ -45,6 +45,7 @@ import java.io.InputStream;
private boolean parsing; private boolean parsing;
private PlayableSubtitle result; private PlayableSubtitle result;
private IOException error; private IOException error;
private RuntimeException runtimeError;
private boolean subtitlesAreRelative; private boolean subtitlesAreRelative;
private long subtitleOffsetUs; private long subtitleOffsetUs;
@ -67,6 +68,7 @@ import java.io.InputStream;
parsing = false; parsing = false;
result = null; result = null;
error = null; error = null;
runtimeError = null;
} }
/** /**
@ -111,6 +113,7 @@ import java.io.InputStream;
parsing = true; parsing = true;
result = null; result = null;
error = null; error = null;
runtimeError = null;
handler.obtainMessage(MSG_SAMPLE, Util.getTopInt(sampleHolder.timeUs), handler.obtainMessage(MSG_SAMPLE, Util.getTopInt(sampleHolder.timeUs),
Util.getBottomInt(sampleHolder.timeUs), sampleHolder).sendToTarget(); Util.getBottomInt(sampleHolder.timeUs), sampleHolder).sendToTarget();
} }
@ -128,11 +131,15 @@ import java.io.InputStream;
try { try {
if (error != null) { if (error != null) {
throw error; throw error;
} } else if (runtimeError != null) {
throw runtimeError;
} else {
return result; return result;
}
} finally { } finally {
error = null;
result = null; result = null;
error = null;
runtimeError = null;
} }
} }
@ -159,11 +166,14 @@ import java.io.InputStream;
private void handleSample(long sampleTimeUs, SampleHolder holder) { private void handleSample(long sampleTimeUs, SampleHolder holder) {
Subtitle parsedSubtitle = null; Subtitle parsedSubtitle = null;
IOException error = null; IOException error = null;
RuntimeException runtimeError = null;
try { try {
InputStream inputStream = new ByteArrayInputStream(holder.data.array(), 0, holder.size); InputStream inputStream = new ByteArrayInputStream(holder.data.array(), 0, holder.size);
parsedSubtitle = parser.parse(inputStream); parsedSubtitle = parser.parse(inputStream);
} catch (IOException e) { } catch (IOException e) {
error = e; error = e;
} catch (RuntimeException e) {
runtimeError = e;
} }
synchronized (this) { synchronized (this) {
if (sampleHolder != holder) { if (sampleHolder != holder) {
@ -172,6 +182,7 @@ import java.io.InputStream;
this.result = new PlayableSubtitle(parsedSubtitle, subtitlesAreRelative, sampleTimeUs, this.result = new PlayableSubtitle(parsedSubtitle, subtitlesAreRelative, sampleTimeUs,
subtitleOffsetUs); subtitleOffsetUs);
this.error = error; this.error = error;
this.runtimeError = runtimeError;
this.parsing = false; this.parsing = false;
} }
} }