From ef42ddee3746aebd87fe49011ce3b7ae98b283fe Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 26 Nov 2024 15:55:14 +0100 Subject: [PATCH] Closing streams --- .../net/codinux/invoicing/converter/EInvoiceConverter.kt | 6 ++++-- .../net/codinux/invoicing/creation/EInvoiceCreator.kt | 6 ++++-- .../kotlin/net/codinux/invoicing/email/EmailsFetcher.kt | 6 +++++- .../invoicing/filesystem/FilesystemInvoiceReader.kt | 8 ++++++-- .../kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt | 6 +++--- .../net/codinux/invoicing/creation/EInvoiceCreatorTest.kt | 2 +- .../net/codinux/invoicing/email/EmailsFetcherTest.kt | 2 ++ 7 files changed, 25 insertions(+), 11 deletions(-) diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt index 6425f6e..22b566c 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt @@ -65,8 +65,10 @@ open class EInvoiceConverter { protected open fun createXRechnungXml(invoice: Invoice): String = EInvoiceCreator().createXRechnungXml(invoice) protected open fun copyResource(resourceName: String, outputFile: File, outputFileExtension: String) { - javaClass.classLoader.getResourceAsStream(resourceName).use { - it?.copyTo(File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream()) + javaClass.classLoader.getResourceAsStream(resourceName).use { inputStream -> + File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream().use { outputStream -> + inputStream?.copyTo(outputStream) + } } } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt index e1b2e66..c6a7cc5 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt @@ -61,10 +61,12 @@ open class EInvoiceCreator( .setProducer("danki die geile Sau") .setCreator(System.getProperty("user.name")) - exporter.load(pdfFile.inputStream()) + pdfFile.inputStream().use { exporter.load(it) } exporter.setXML(invoiceXml.toByteArray()) - exporter.export(outputFile.outputStream()) + outputFile.outputStream().use { outputStream -> + exporter.export(outputStream) + } } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index eb34515..7072fa1 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -197,7 +197,11 @@ open class EmailsFetcher( private fun downloadAttachment(part: Part, status: FetchEmailsStatus) = File(status.userAttachmentsDownloadDirectory, FileUtil.removeIllegalFileCharacters(part.fileName)).also { file -> - part.inputStream.use { it.copyTo(file.outputStream()) } + part.inputStream.use { inputStream -> + file.outputStream().use { outputStream -> + inputStream.copyTo(outputStream) + } + } } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt index 7c86c74..804acb7 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt @@ -35,9 +35,13 @@ open class FilesystemInvoiceReader( val extension = file.extension.lowercase() if (extension == "pdf") { - eInvoiceReader.extractFromPdf(file.inputStream()) + file.inputStream().use { inputStream -> + eInvoiceReader.extractFromPdf(inputStream) + } } else if (extension == "xml") { - eInvoiceReader.extractFromXml(file.inputStream()) + file.inputStream().use { inputStream -> + eInvoiceReader.extractFromXml(inputStream) + } } else { null } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt index 4fadd38..1380e0a 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt @@ -17,7 +17,7 @@ open class EInvoiceReader( } - open fun extractFromXml(xmlFile: File) = extractFromXml(xmlFile.inputStream()) + open fun extractFromXml(xmlFile: File) = xmlFile.inputStream().use { extractFromXml(it) } open fun extractFromXml(stream: InputStream) = extractFromXml(stream.reader().readText()) @@ -28,7 +28,7 @@ open class EInvoiceReader( return extractInvoice(importer) } - open fun extractFromPdf(pdfFile: File) = extractFromPdf(pdfFile.inputStream()) + open fun extractFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractFromPdf(it) } open fun extractFromPdf(stream: InputStream): Invoice { val importer = ZUGFeRDInvoiceImporter(stream) @@ -36,7 +36,7 @@ open class EInvoiceReader( return extractInvoice(importer) } - open fun extractXmlFromPdf(pdfFile: File) = extractXmlFromPdf(pdfFile.inputStream()) + open fun extractXmlFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractXmlFromPdf(it) } open fun extractXmlFromPdf(stream: InputStream): String { val importer = ZUGFeRDInvoiceImporter(stream) diff --git a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt index c0ca1df..b5f44ef 100644 --- a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt +++ b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt @@ -36,7 +36,7 @@ class EInvoiceCreatorTest { underTest.createFacturXPdf(invoice, testFile) - val importer = ZUGFeRDInvoiceImporter(testFile.inputStream()) + val importer = testFile.inputStream().use { ZUGFeRDInvoiceImporter(it) } val xml = String(importer.rawXML, Charsets.UTF_8) assertInvoiceXml(xml) diff --git a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt index 9bb1414..c2df90d 100644 --- a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt +++ b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt @@ -3,6 +3,7 @@ package net.codinux.invoicing.email import assertk.assertThat import assertk.assertions.isEmpty import assertk.assertions.isNotEmpty +import assertk.assertions.isNull import net.codinux.invoicing.email.model.EmailAccount import org.junit.jupiter.api.Test import kotlin.test.Ignore @@ -28,6 +29,7 @@ class EmailsFetcherTest { fun fetchAllEmails() { val result = underTest.fetchAllEmails(emailAccount, FetchEmailsOptions(true)) + assertThat(result.overallError).isNull() assertThat(result.emails).isNotEmpty() val emailsWithoutBody = result.emails.filter { it.plainTextOrHtmlBody == null }