From f0d4c83e13740e127deeedf496afaee4cdd6cd5b Mon Sep 17 00:00:00 2001 From: Max Bendick Date: Wed, 17 Oct 2018 13:08:01 -0700 Subject: [PATCH] Close all streams --- src/main/scala/s3/website/model/push.scala | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/scala/s3/website/model/push.scala b/src/main/scala/s3/website/model/push.scala index cc26714f..392c27c5 100644 --- a/src/main/scala/s3/website/model/push.scala +++ b/src/main/scala/s3/website/model/push.scala @@ -62,8 +62,10 @@ case class Upload(originalFile: File, uploadType: UploadType)(implicit site: Sit if (fileIsGzippedByExternalBuildTool) { val unzippedFile = createTempFile("unzipped", originalFile.getName) unzippedFile.deleteOnExit() - using(new GZIPInputStream(fis(originalFile))) { stream => - IOUtils.copy(stream, new FileOutputStream(unzippedFile)) + using(new GZIPInputStream(fis(originalFile))) { inputStream => + using(new FileOutputStream(unzippedFile)) { outputStream => + IOUtils.copy(inputStream, outputStream) + } } unzippedFile } else { @@ -113,8 +115,12 @@ case class Upload(originalFile: File, uploadType: UploadType)(implicit site: Sit logger.debug(s"Gzipping file ${originalFile.getName}") val tempFile = createTempFile(originalFile.getName, "gzip") tempFile.deleteOnExit() - using(new GZIPOutputStream(new FileOutputStream(tempFile))) { stream => - IOUtils.copy(fis(originalFile), stream) + using(new FileOutputStream(tempFile)) { fileOutputStream => + using(new GZIPOutputStream(fileOutputStream)) { gzipOutputStream => + using(fis(originalFile)) { originalFileInputStream => + IOUtils.copy(originalFileInputStream, gzipOutputStream) + } + } } tempFile } @@ -128,11 +134,12 @@ case class Upload(originalFile: File, uploadType: UploadType)(implicit site: Sit if (originalFile.length() < amountOfMagicGzipBytes) { false } else { - val fis = new FileInputStream(originalFile) - val firstTwoBytes = Array.fill[Byte](amountOfMagicGzipBytes)(0) - fis.read(firstTwoBytes, 0, amountOfMagicGzipBytes) - val head = firstTwoBytes(0) & 0xff | (firstTwoBytes(1) << 8) & 0xff00 - head == GZIPInputStream.GZIP_MAGIC + using(fis(originalFile)) { stream => + val firstTwoBytes = Array.fill[Byte](amountOfMagicGzipBytes)(0) + stream.read(firstTwoBytes, 0, amountOfMagicGzipBytes) + val head = firstTwoBytes(0) & 0xff | (firstTwoBytes(1) << 8) & 0xff00 + head == GZIPInputStream.GZIP_MAGIC + } } private[this] def fis(file: File): InputStream = new FileInputStream(file)