Closing streams

This commit is contained in:
dankito 2024-11-26 15:55:14 +01:00
parent af03c0d5d3
commit ef42ddee37
7 changed files with 25 additions and 11 deletions

View File

@ -65,8 +65,10 @@ open class EInvoiceConverter {
protected open fun createXRechnungXml(invoice: Invoice): String = EInvoiceCreator().createXRechnungXml(invoice) protected open fun createXRechnungXml(invoice: Invoice): String = EInvoiceCreator().createXRechnungXml(invoice)
protected open fun copyResource(resourceName: String, outputFile: File, outputFileExtension: String) { protected open fun copyResource(resourceName: String, outputFile: File, outputFileExtension: String) {
javaClass.classLoader.getResourceAsStream(resourceName).use { javaClass.classLoader.getResourceAsStream(resourceName).use { inputStream ->
it?.copyTo(File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream()) File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream().use { outputStream ->
inputStream?.copyTo(outputStream)
}
} }
} }

View File

@ -61,10 +61,12 @@ open class EInvoiceCreator(
.setProducer("danki die geile Sau") .setProducer("danki die geile Sau")
.setCreator(System.getProperty("user.name")) .setCreator(System.getProperty("user.name"))
exporter.load(pdfFile.inputStream()) pdfFile.inputStream().use { exporter.load(it) }
exporter.setXML(invoiceXml.toByteArray()) exporter.setXML(invoiceXml.toByteArray())
exporter.export(outputFile.outputStream()) outputFile.outputStream().use { outputStream ->
exporter.export(outputStream)
}
} }

View File

@ -197,7 +197,11 @@ open class EmailsFetcher(
private fun downloadAttachment(part: Part, status: FetchEmailsStatus) = private fun downloadAttachment(part: Part, status: FetchEmailsStatus) =
File(status.userAttachmentsDownloadDirectory, FileUtil.removeIllegalFileCharacters(part.fileName)).also { file -> 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)
}
}
} }

View File

@ -35,9 +35,13 @@ open class FilesystemInvoiceReader(
val extension = file.extension.lowercase() val extension = file.extension.lowercase()
if (extension == "pdf") { if (extension == "pdf") {
eInvoiceReader.extractFromPdf(file.inputStream()) file.inputStream().use { inputStream ->
eInvoiceReader.extractFromPdf(inputStream)
}
} else if (extension == "xml") { } else if (extension == "xml") {
eInvoiceReader.extractFromXml(file.inputStream()) file.inputStream().use { inputStream ->
eInvoiceReader.extractFromXml(inputStream)
}
} else { } else {
null null
} }

View File

@ -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()) open fun extractFromXml(stream: InputStream) = extractFromXml(stream.reader().readText())
@ -28,7 +28,7 @@ open class EInvoiceReader(
return extractInvoice(importer) 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 { open fun extractFromPdf(stream: InputStream): Invoice {
val importer = ZUGFeRDInvoiceImporter(stream) val importer = ZUGFeRDInvoiceImporter(stream)
@ -36,7 +36,7 @@ open class EInvoiceReader(
return extractInvoice(importer) 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 { open fun extractXmlFromPdf(stream: InputStream): String {
val importer = ZUGFeRDInvoiceImporter(stream) val importer = ZUGFeRDInvoiceImporter(stream)

View File

@ -36,7 +36,7 @@ class EInvoiceCreatorTest {
underTest.createFacturXPdf(invoice, testFile) underTest.createFacturXPdf(invoice, testFile)
val importer = ZUGFeRDInvoiceImporter(testFile.inputStream()) val importer = testFile.inputStream().use { ZUGFeRDInvoiceImporter(it) }
val xml = String(importer.rawXML, Charsets.UTF_8) val xml = String(importer.rawXML, Charsets.UTF_8)
assertInvoiceXml(xml) assertInvoiceXml(xml)

View File

@ -3,6 +3,7 @@ package net.codinux.invoicing.email
import assertk.assertThat import assertk.assertThat
import assertk.assertions.isEmpty import assertk.assertions.isEmpty
import assertk.assertions.isNotEmpty import assertk.assertions.isNotEmpty
import assertk.assertions.isNull
import net.codinux.invoicing.email.model.EmailAccount import net.codinux.invoicing.email.model.EmailAccount
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import kotlin.test.Ignore import kotlin.test.Ignore
@ -28,6 +29,7 @@ class EmailsFetcherTest {
fun fetchAllEmails() { fun fetchAllEmails() {
val result = underTest.fetchAllEmails(emailAccount, FetchEmailsOptions(true)) val result = underTest.fetchAllEmails(emailAccount, FetchEmailsOptions(true))
assertThat(result.overallError).isNull()
assertThat(result.emails).isNotEmpty() assertThat(result.emails).isNotEmpty()
val emailsWithoutBody = result.emails.filter { it.plainTextOrHtmlBody == null } val emailsWithoutBody = result.emails.filter { it.plainTextOrHtmlBody == null }