mirror of
https://github.com/revanced/revanced-patcher.git
synced 2025-06-13 05:37:38 +02:00
fix: close stream when closing DomFileEditor
This commit is contained in:
@ -26,17 +26,24 @@ class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable<Fi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DomFileEditor internal constructor(inputStream: InputStream, private val outputStream: Lazy<OutputStream>) : Closeable {
|
class DomFileEditor internal constructor(
|
||||||
|
private val inputStream: InputStream,
|
||||||
|
private val outputStream: Lazy<OutputStream>,
|
||||||
|
) : Closeable {
|
||||||
|
val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream)
|
||||||
|
.also(Document::normalize)
|
||||||
|
|
||||||
// lazily open an output stream
|
// 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
|
// 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
|
// 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() })
|
constructor(file: File) : this(file.inputStream(), lazy { file.outputStream() })
|
||||||
|
|
||||||
val file: Document =
|
override fun close() {
|
||||||
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream).also(Document::normalize)
|
val result = StreamResult(outputStream.value)
|
||||||
|
TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), result)
|
||||||
|
|
||||||
override fun close() =
|
inputStream.close()
|
||||||
TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), StreamResult(outputStream.value))
|
outputStream.value.close()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user