Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close all streams #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/main/scala/s3/website/model/push.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down