diff --git a/CHANGELOG.md b/CHANGELOG.md index 0160a90..f216604 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,24 @@ +# [2.2.0-dev.3](https://github.com/revanced/revanced-patcher/compare/v2.2.0-dev.2...v2.2.0-dev.3) (2022-07-02) + + +### Bug Fixes + +* DomFileEditor opening in- and output streams on the same file ([83187c9](https://github.com/revanced/revanced-patcher/commit/83187c9edd7b088bc18960c5eb9a2042ca536b5f)) + +# [2.2.0-dev.2](https://github.com/revanced/revanced-patcher/compare/v2.2.0-dev.1...v2.2.0-dev.2) (2022-07-02) + + +### Features + +* streams overload for `XmlFileHolder` ([6f72c4c](https://github.com/revanced/revanced-patcher/commit/6f72c4c4c051e48c8d03d2a7b2cfc1c53028ed86)) + +# [2.2.0-dev.1](https://github.com/revanced/revanced-patcher/compare/v2.1.2...v2.2.0-dev.1) (2022-07-02) + + +### Features + +* remove deprecated functions ([ada5a03](https://github.com/revanced/revanced-patcher/commit/ada5a033de3cf94e7255ec2d522520f86431f001)) + ## [2.1.2](https://github.com/revanced/revanced-patcher/compare/v2.1.1...v2.1.2) (2022-06-29) diff --git a/gradle.properties b/gradle.properties index 1e97a0d..2a13e8c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ kotlin.code.style = official -version = 2.1.2 +version = 2.2.0-dev.3 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 3b9c52f..573cd0a 100644 --- a/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt +++ b/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt @@ -4,31 +4,39 @@ import app.revanced.patcher.data.Data import org.w3c.dom.Document import java.io.Closeable import java.io.File +import java.io.InputStream +import java.io.OutputStream import javax.xml.parsers.DocumentBuilderFactory import javax.xml.transform.TransformerFactory import javax.xml.transform.dom.DOMSource import javax.xml.transform.stream.StreamResult class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable { - operator fun get(path: String) = resourceCacheDirectory.resolve(path) val xmlEditor = XmlFileHolder() + + operator fun get(path: String) = resourceCacheDirectory.resolve(path) + override fun iterator() = resourceCacheDirectory.walkTopDown().iterator() inner class XmlFileHolder { + operator fun get(inputStream: InputStream, outputStream: OutputStream) = + DomFileEditor(inputStream, lazyOf(outputStream)) + operator fun get(path: String) = DomFileEditor(this@ResourceData[path]) } - - @Deprecated("Use operator getter instead of resolve function", ReplaceWith("get(path)")) - fun resolve(path: String) = get(path) - - @Deprecated("Use operator getter on xmlEditor instead of getXmlEditor function", ReplaceWith("xmlEditor[path]")) - fun getXmlEditor(path: String) = xmlEditor[path] } -class DomFileEditor internal constructor(private val domFile: File) : Closeable { - val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder() - .parse(domFile).also(Document::normalize) +class DomFileEditor internal constructor(inputStream: InputStream, private val outputStream: Lazy) : 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.value)) - override fun close() = TransformerFactory.newInstance().newTransformer() - .transform(DOMSource(file), StreamResult(domFile.outputStream())) }