Skip to content

Commit

Permalink
Merge pull request #420 from alvasw/electrum_downloader_close_streams
Browse files Browse the repository at this point in the history
ElectrumBinariesDownloader: Close all streams after download
  • Loading branch information
chimp1984 authored Jul 5, 2022
2 parents 3ee06c2 + 3ea2c15 commit 9b768bf
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import java.io.File
import java.io.FileOutputStream
import java.net.URL
import java.nio.channels.Channels
import java.nio.channels.ReadableByteChannel

abstract class DownloadElectrumBinariesTask : DefaultTask() {

Expand Down Expand Up @@ -49,17 +48,22 @@ abstract class DownloadElectrumBinariesTask : DefaultTask() {
}

private fun downloadFile(url: String) {
val inputStream = URL(url).openStream()
val readableByteChannel: ReadableByteChannel = Channels.newChannel(inputStream)
URL(url).openStream().use { inputStream ->
Channels.newChannel(inputStream).use { readableByteChannel ->
println("Downloading: $url")

val outputFilePath: File = getOutputFileForUrl(url)
FileOutputStream(outputFilePath).use { fileOutputStream ->
fileOutputStream.channel
.transferFrom(readableByteChannel, 0, Long.MAX_VALUE)
}
}
}
}

private fun getOutputFileForUrl(url: String): File {
val outputDir: File = outputDir.get().asFile
val outputFileName = url.split("/").last()
val outputFilePath: File = outputDir.resolve(outputFileName)

println("Downloading: $url")
val fileOutputStream = FileOutputStream(outputFilePath)
fileOutputStream.channel
.transferFrom(readableByteChannel, 0, Long.MAX_VALUE)
return outputDir.resolve(outputFileName)
}

}

0 comments on commit 9b768bf

Please sign in to comment.