From 83187c9edd7b088bc18960c5eb9a2042ca536b5f Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 3 Jul 2022 00:19:39 +0200 Subject: [PATCH] fix: DomFileEditor opening in- and output streams on the same file --- .../app/revanced/patcher/data/impl/ResourceData.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt b/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt index fddabde..573cd0a 100644 --- a/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt +++ b/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt @@ -20,19 +20,23 @@ class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable) : Closeable { + + // lazily open an output stream + // this is required because when constructing a DomFileEditor the output stream is created along with the input stream, which is not allowed + // the workaround is to lazily create the output stream. This way it would be used after the input stream is closed, which happens in the constructor + constructor(file: File) : this(file.inputStream(), lazy { file.outputStream() }) val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream).also(Document::normalize) override fun close() = - TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), StreamResult(outputStream)) + TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), StreamResult(outputStream.value)) }