mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2026-01-09 12:56:17 +00:00
fix: Correctly save XML files in UTF-8 by using a bufferedWriter (#356)
This commit is contained in:
@@ -4,7 +4,6 @@ import org.w3c.dom.Document
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.StringWriter
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
import javax.xml.transform.OutputKeys
|
||||
import javax.xml.transform.TransformerFactory
|
||||
@@ -36,20 +35,22 @@ class Document internal constructor(
|
||||
readerCount.remove(it)
|
||||
}
|
||||
|
||||
it.outputStream().buffered().use { stream ->
|
||||
val transformer = TransformerFactory.newInstance().newTransformer()
|
||||
// Set to UTF-16 but encode as UTF-8 to prevent surrogate pairs from being escaped to broken numeric character references.
|
||||
if (isAndroid) {
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16")
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
|
||||
val transformer = TransformerFactory.newInstance().newTransformer()
|
||||
// Set to UTF-16 to prevent surrogate pairs from being escaped to invalid numeric character references, but save as UTF-8.
|
||||
if (isAndroid) {
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16")
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
|
||||
it.bufferedWriter(charset = Charsets.UTF_8).use { writer ->
|
||||
transformer.transform(DOMSource(this), StreamResult(writer))
|
||||
}
|
||||
transformer.transform(DOMSource(this), StreamResult(stream))
|
||||
} else {
|
||||
transformer.transform(DOMSource(this), StreamResult(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
private val readerCount = mutableMapOf<File, Int>()
|
||||
private val isAndroid = System.getProperty("java.runtime.name").equals("Android Runtime")
|
||||
private val isAndroid = System.getProperty("java.runtime.name") == "Android Runtime"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user