1
mirror of https://github.com/revanced/revanced-patcher synced 2025-02-25 00:15:24 +01:00

fix: DomFileEditor opening in- and output streams on the same file

This commit is contained in:
oSumAtrIX 2022-07-03 00:19:39 +02:00
parent 79d70cff4b
commit 83187c9edd
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -20,19 +20,23 @@ class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable<Fi
inner class XmlFileHolder {
operator fun get(inputStream: InputStream, outputStream: OutputStream) =
DomFileEditor(inputStream, outputStream)
DomFileEditor(inputStream, lazyOf(outputStream))
operator fun get(path: String) = DomFileEditor(this@ResourceData[path])
}
}
class DomFileEditor internal constructor(inputStream: InputStream, private val outputStream: OutputStream) : Closeable {
constructor(file: File) : this(file.inputStream(), file.outputStream())
class DomFileEditor internal constructor(inputStream: InputStream, private val outputStream: Lazy<OutputStream>) : 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))
}