Fix Base64OutputStream swallowing/throwing the wrong exception.
Base64OutputStream.close() first write()s (possibly empty) data
and then close()s the underlying stream. Because of an inverted
condition, exceptions thrown by the underlying stream were handled
incorrectly:
- if both the underlying write() and close()/flush() throw
then it would throw the latter exception when it should
have thrown the former, suppressing the latter.
- if only the underlying close() throws then that exception
was swallowed when it should have been closed.
This bug existed ever since this file was first added to
Android in commit 9df2ffd4205115a68c4fe0651b2072e3e4573dd2
in Feb 2010 (before the concept of suppressed exceptions
existed/was supported). The buggy code was proposed in a
review comment on that CL, which was then accepted/integrated
into the CL.
This CL fixes the inverted condition and adds test coverage.
It also adds code to add the suppressed exception (if any).
Bug: 111697617
Test: Confirmed that Base64Test fails (as described above) before
this CL but passes after this CL, when running these commands:
make FrameworksCoreTests && \
adb install -r ${ANDROID_PRODUCT_OUT}/data/app/FrameworksCoreTests/FrameworksCoreTests.apk && \
adb shell am instrument -w -e class android.util.Base64Test \
com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner
Specifically, before this CL, the following test failures occur:
1.) junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:48)
at junit.framework.Assert.fail(Assert.java:56)
at android.util.Base64Test.testOutputStream_ioExceptionDuringClose(Base64Test.java:546)
2.) junit.framework.ComparisonFailure: expected:<[writ]e()> but was:<[clos]e()>
at junit.framework.Assert.assertEquals(Assert.java:85)
at junit.framework.Assert.assertEquals(Assert.java:91)
at android.util.Base64Test.testOutputStream_ioExceptionDuringCloseAndWrite(Base64Test.java:574)
Change-Id: If7fd7c4615ca004638d7c5d8f1869e7eddb16f33
2 files changed