From 56602e0a2cee94a82f280fc2b4443b6f5c00c15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Tue, 20 Aug 2024 12:12:31 +0200 Subject: [PATCH] fix(abg): fix file handle leak in jar creation --- .../workflows/mavenbinding/ZipUtils.kt | 31 +++---------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/ZipUtils.kt b/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/ZipUtils.kt index 5c8e8fa60..5e3df8c6c 100644 --- a/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/ZipUtils.kt +++ b/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/ZipUtils.kt @@ -29,7 +29,6 @@ internal fun OutputStream.createZipFile(contents: Path) = zipFile(file.toFile(), zipOutputStream) } } - zipOutputStream.flush() } /** @@ -60,17 +59,8 @@ private fun zipDirectory( lastAccessTime = FileTime.fromMillis(0) } zos.putNextEntry(zipEntry) - val bis = - BufferedInputStream( - FileInputStream(file), - ) - var bytesRead: Long = 0 - val bytesIn = ByteArray(BUFFER_SIZE) - var read: Int - while ((bis.read(bytesIn).also { read = it }) != -1) { - zos.write(bytesIn, 0, read) - bytesRead += read.toLong() - } + BufferedInputStream(FileInputStream(file)) + .use { it.copyTo(zos) } zos.closeEntry() } } @@ -96,20 +86,7 @@ private fun zipFile( lastAccessTime = FileTime.fromMillis(0) } zos.putNextEntry(zipEntry) - val bis = - BufferedInputStream( - FileInputStream( - file, - ), - ) - var bytesRead: Long = 0 - val bytesIn = ByteArray(BUFFER_SIZE) - var read: Int - while ((bis.read(bytesIn).also { read = it }) != -1) { - zos.write(bytesIn, 0, read) - bytesRead += read.toLong() - } + BufferedInputStream(FileInputStream(file)) + .use { it.copyTo(zos) } zos.closeEntry() } - -private const val BUFFER_SIZE = 8192