Commit aee9d1bf authored by Andrzej Hunt's avatar Andrzej Hunt
Browse files

Bug 1261983 - infer: close file input stream in case of GZIP failure r=nechen

This commit also cleans up extraneous stream closes: these streams
are closed in finally, so we don't need to also clean them up in
the main try block body.

MozReview-Commit-ID: EXxlmmtqvyq

--HG--
extra : amend_source : 72ad8cb816202e8e3f234157bae092cea004d34a
parent a77567be
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -215,6 +215,7 @@ public class DownloadAction extends BaseAction {
    protected void extract(File sourceFile, File destinationFile, String checksum)
            throws UnrecoverableDownloadContentException, RecoverableDownloadContentException {
        InputStream inputStream = null;
        InputStream gzInputStream = null;
        OutputStream outputStream = null;
        File temporaryFile = null;

@@ -226,13 +227,15 @@ public class DownloadAction extends BaseAction {

            temporaryFile = new File(destinationDirectory, destinationFile.getName() + ".tmp");

            inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(sourceFile)));
            // We have to have keep a handle to the BufferedInputStream: the GZIPInputStream
            // constructor can fail e.g. if the stream isn't a GZIP stream. If we didn't keep
            // a reference to that stream we wouldn't be able to close it if GZInputStream throws.
            // (The BufferedInputStream constructor doesn't throw, so we don't need to care about it.)
            inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
            gzInputStream = new GZIPInputStream(inputStream);
            outputStream = new BufferedOutputStream(new FileOutputStream(temporaryFile));

            IOUtils.copy(inputStream, outputStream);

            inputStream.close();
            outputStream.close();
            IOUtils.copy(gzInputStream, outputStream);

            if (!verify(temporaryFile, checksum)) {
                Log.w(LOGTAG, "Checksum of extracted file does not match.");
@@ -244,6 +247,7 @@ public class DownloadAction extends BaseAction {
            // We could not extract to the destination: Keep temporary file and try again next time we run.
            throw new RecoverableDownloadContentException(RecoverableDownloadContentException.DISK_IO, e);
        } finally {
            IOUtils.safeStreamClose(gzInputStream);
            IOUtils.safeStreamClose(inputStream);
            IOUtils.safeStreamClose(outputStream);