Consistent UTF-8 where possible (#9927)

* Consistent UTF-8 where possible

* Remove one import
This commit is contained in:
SomeTroglodyte
2023-08-21 17:17:21 +02:00
committed by GitHub
parent 4851865bce
commit cc43a41ec7
13 changed files with 31 additions and 31 deletions

View File

@ -320,7 +320,7 @@ class GameInfo : IsPartOfGameInfoSerialization, HasGameInfoSerializationVersion
checksum = "" // Checksum calculation cannot include old checksum, obvs checksum = "" // Checksum calculation cannot include old checksum, obvs
val bytes = MessageDigest val bytes = MessageDigest
.getInstance("SHA-1") .getInstance("SHA-1")
.digest(json().toJson(this).toByteArray()) .digest(json().toJson(this).toByteArray(Charsets.UTF_8))
checksum = oldChecksum checksum = oldChecksum
return Gzip.encode(bytes) return Gzip.encode(bytes)
} }

View File

@ -17,7 +17,7 @@ class LinuxX11SaverLoader : PlatformSaverLoader {
success, file -> success, file ->
if (!success) return@createSaveDialog if (!success) return@createSaveDialog
try { try {
file.writeString(data, false, "UTF-8") file.writeString(data, false, Charsets.UTF_8.name())
onSaved(file.path()) onSaved(file.path())
} catch (ex: Exception) { } catch (ex: Exception) {
onError(ex) onError(ex)
@ -34,7 +34,7 @@ class LinuxX11SaverLoader : PlatformSaverLoader {
FileChooser.createLoadDialog(stage, "Load game") { success, file -> FileChooser.createLoadDialog(stage, "Load game") { success, file ->
if (!success) return@createLoadDialog if (!success) return@createLoadDialog
try { try {
val data = file.readString("UTF-8") val data = file.readString(Charsets.UTF_8.name())
onLoaded(data, file.path()) onLoaded(data, file.path())
} catch (ex: Exception) { } catch (ex: Exception) {
onError(ex) onError(ex)

View File

@ -6,7 +6,6 @@ import com.unciv.json.json
import com.unciv.logic.map.MapParameters import com.unciv.logic.map.MapParameters
import com.unciv.logic.map.TileMap import com.unciv.logic.map.TileMap
import com.unciv.ui.screens.savescreens.Gzip import com.unciv.ui.screens.savescreens.Gzip
import com.unciv.utils.Log
object MapSaver { object MapSaver {
@ -30,11 +29,11 @@ object MapSaver {
} }
fun saveMap(mapName: String, tileMap: TileMap) { fun saveMap(mapName: String, tileMap: TileMap) {
getMap(mapName).writeString(mapToSavedString(tileMap), false) getMap(mapName).writeString(mapToSavedString(tileMap), false, Charsets.UTF_8.name())
} }
fun loadMap(mapFile: FileHandle): TileMap { fun loadMap(mapFile: FileHandle): TileMap {
return mapFromSavedString(mapFile.readString()) return mapFromSavedString(mapFile.readString(Charsets.UTF_8.name()))
} }
fun getMaps(): Array<FileHandle> = Gdx.files.local(mapsFolder).list() fun getMaps(): Array<FileHandle> = Gdx.files.local(mapsFolder).list()

View File

@ -4,6 +4,7 @@ import com.badlogic.gdx.Files
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.utils.JsonReader import com.badlogic.gdx.utils.JsonReader
import com.badlogic.gdx.utils.GdxRuntimeException // Kdoc
import com.badlogic.gdx.utils.SerializationException import com.badlogic.gdx.utils.SerializationException
import com.unciv.UncivGame import com.unciv.UncivGame
import com.unciv.json.fromJsonFile import com.unciv.json.fromJsonFile
@ -84,7 +85,7 @@ class UncivFiles(
} else { } else {
files.local(path) files.local(path)
} }
return file.writer(append) return file.writer(append, Charsets.UTF_8.name())
} }
fun getMultiplayerSaves(): Sequence<FileHandle> { fun getMultiplayerSaves(): Sequence<FileHandle> {
@ -226,7 +227,7 @@ class UncivFiles(
loadGameFromFile(getSave(gameName)) loadGameFromFile(getSave(gameName))
fun loadGameFromFile(gameFile: FileHandle): GameInfo { fun loadGameFromFile(gameFile: FileHandle): GameInfo {
val gameData = gameFile.readString() val gameData = gameFile.readString(Charsets.UTF_8.name())
if (gameData.isNullOrBlank()) { if (gameData.isNullOrBlank()) {
throw emptyFile(gameFile) throw emptyFile(gameFile)
} }
@ -304,7 +305,7 @@ class UncivFiles(
} }
fun setGeneralSettings(gameSettings: GameSettings) { fun setGeneralSettings(gameSettings: GameSettings) {
getGeneralSettingsFile().writeString(json().toJson(gameSettings), false) getGeneralSettingsFile().writeString(json().toJson(gameSettings), false, Charsets.UTF_8.name())
} }
companion object { companion object {

View File

@ -38,8 +38,7 @@ object DropBox: FileStorage {
try { try {
if (data != "") { if (data != "") {
// StandardCharsets.UTF_8 requires API 19 val postData: ByteArray = data.toByteArray(Charsets.UTF_8)
val postData: ByteArray = data.toByteArray(Charset.forName("UTF-8"))
val outputStream = DataOutputStream(outputStream) val outputStream = DataOutputStream(outputStream)
outputStream.write(postData) outputStream.write(postData)
outputStream.flush() outputStream.flush()
@ -48,7 +47,7 @@ object DropBox: FileStorage {
return inputStream return inputStream
} catch (ex: Exception) { } catch (ex: Exception) {
debug("Dropbox exception", ex) debug("Dropbox exception", ex)
val reader = BufferedReader(InputStreamReader(errorStream)) val reader = BufferedReader(InputStreamReader(errorStream, Charsets.UTF_8))
val responseString = reader.readText() val responseString = reader.readText()
debug("Response: %s", responseString) debug("Response: %s", responseString)
@ -63,7 +62,7 @@ object DropBox: FileStorage {
return null return null
} catch (error: Error) { } catch (error: Error) {
Log.error("Dropbox error", error) Log.error("Dropbox error", error)
debug("Error stream: %s", { BufferedReader(InputStreamReader(errorStream)).readText() }) debug("Error stream: %s", { BufferedReader(InputStreamReader(errorStream, Charsets.UTF_8)).readText() })
return null return null
} }
} }
@ -86,7 +85,7 @@ object DropBox: FileStorage {
data="{\"path\":\"${getLocalGameLocation(fileName)}\"}", data="{\"path\":\"${getLocalGameLocation(fileName)}\"}",
contentType="application/json" contentType="application/json"
)!! )!!
val reader = BufferedReader(InputStreamReader(stream)) val reader = BufferedReader(InputStreamReader(stream, Charsets.UTF_8))
return json().fromJson(MetaData::class.java, reader.readText()) return json().fromJson(MetaData::class.java, reader.readText())
} }
@ -101,7 +100,7 @@ object DropBox: FileStorage {
override fun loadFileData(fileName: String): String { override fun loadFileData(fileName: String): String {
val inputStream = downloadFile(getLocalGameLocation(fileName)) val inputStream = downloadFile(getLocalGameLocation(fileName))
return BufferedReader(InputStreamReader(inputStream)).readText() return BufferedReader(InputStreamReader(inputStream, Charsets.UTF_8)).readText()
} }
override fun authenticate(userId: String, password: String): Boolean { override fun authenticate(userId: String, password: String): Boolean {
@ -148,7 +147,7 @@ object DropBox: FileStorage {
// val result = dropboxApi("https://api.dropboxapi.com/2/file_properties/templates/add_for_user", // val result = dropboxApi("https://api.dropboxapi.com/2/file_properties/templates/add_for_user",
// "{\"name\": \"Security\",\"description\": \"These properties describe how confidential this file or folder is.\",\"fields\": [{\"name\": \"Security Policy\",\"description\": \"This is the security policy of the file or folder described.\nPolicies can be Confidential, Public or Internal.\",\"type\": \"string\"}]}" // "{\"name\": \"Security\",\"description\": \"These properties describe how confidential this file or folder is.\",\"fields\": [{\"name\": \"Security Policy\",\"description\": \"This is the security policy of the file or folder described.\nPolicies can be Confidential, Public or Internal.\",\"type\": \"string\"}]}"
// ,"application/json") // ,"application/json")
// return BufferedReader(InputStreamReader(result)).readText() // return BufferedReader(InputStreamReader(result, Charsets.UTF_8)).readText()
// } // }
// private class FolderList{ // private class FolderList{

View File

@ -51,19 +51,18 @@ object SimpleHttp {
try { try {
if (content.isNotEmpty()) { if (content.isNotEmpty()) {
doOutput = true doOutput = true
// StandardCharsets.UTF_8 requires API 19 val postData: ByteArray = content.toByteArray(Charsets.UTF_8)
val postData: ByteArray = content.toByteArray(Charset.forName("UTF-8"))
val outputStream = DataOutputStream(outputStream) val outputStream = DataOutputStream(outputStream)
outputStream.write(postData) outputStream.write(postData)
outputStream.flush() outputStream.flush()
} }
val text = BufferedReader(InputStreamReader(inputStream)).readText() val text = BufferedReader(InputStreamReader(inputStream, Charsets.UTF_8)).readText()
action(true, text, responseCode) action(true, text, responseCode)
} catch (t: Throwable) { } catch (t: Throwable) {
debug("Error during HTTP request", t) debug("Error during HTTP request", t)
val errorMessageToReturn = val errorMessageToReturn =
if (errorStream != null) BufferedReader(InputStreamReader(errorStream)).readText() if (errorStream != null) BufferedReader(InputStreamReader(errorStream, Charsets.UTF_8)).readText()
else t.message!! else t.message!!
debug("Returning error message [%s]", errorMessageToReturn) debug("Returning error message [%s]", errorMessageToReturn)
action(false, errorMessageToReturn, responseCode) action(false, errorMessageToReturn, responseCode)

View File

@ -59,7 +59,7 @@ class ModCategories : ArrayList<ModCategories.Category>() {
val json = json() val json = json()
val compact = json.toJson(this, ModCategories::class.java, Category::class.java) val compact = json.toJson(this, ModCategories::class.java, Category::class.java)
val verbose = json.prettyPrint(compact) val verbose = json.prettyPrint(compact)
Gdx.files.local(fileLocation).writeString(verbose, false, "UTF-8") Gdx.files.local(fileLocation).writeString(verbose, false, Charsets.UTF_8.name())
} }
fun fromSelectBox(selectBox: TranslatedSelectBox): Category { fun fromSelectBox(selectBox: TranslatedSelectBox): Category {

View File

@ -2,13 +2,12 @@ package com.unciv.models.translations
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.files.FileHandle
import java.nio.charset.Charset
import kotlin.collections.set import kotlin.collections.set
object TranslationFileReader { object TranslationFileReader {
const val percentagesFileLocation = "jsons/translations/completionPercentages.properties" const val percentagesFileLocation = "jsons/translations/completionPercentages.properties"
val charset: String = Charset.forName("UTF-8").name() val charset: String = Charsets.UTF_8.name()
fun read(file: FileHandle): LinkedHashMap<String, String> { fun read(file: FileHandle): LinkedHashMap<String, String> {
val translations = LinkedHashMap<String, String>() val translations = LinkedHashMap<String, String>()

View File

@ -57,7 +57,7 @@ object Github {
} catch (ex: Exception) { } catch (ex: Exception) {
// No error handling, just log the message. // No error handling, just log the message.
// NOTE that this 'read error stream' CAN ALSO fail, but will be caught by the big try/catch // NOTE that this 'read error stream' CAN ALSO fail, but will be caught by the big try/catch
val reader = BufferedReader(InputStreamReader(errorStream)) val reader = BufferedReader(InputStreamReader(errorStream, Charsets.UTF_8))
Log.error("Message from GitHub: %s", reader.readText()) Log.error("Message from GitHub: %s", reader.readText())
throw ex throw ex
} }

View File

@ -26,7 +26,7 @@ object Gzip {
private fun decompress(compressed: ByteArray): String { private fun decompress(compressed: ByteArray): String {
val bis = ByteArrayInputStream(compressed) val bis = ByteArrayInputStream(compressed)
val gis = GZIPInputStream(bis) val gis = GZIPInputStream(bis)
val br = BufferedReader(InputStreamReader(gis, "UTF-8")) val br = BufferedReader(InputStreamReader(gis, Charsets.UTF_8))
val sb = StringBuilder() val sb = StringBuilder()
var line: String? = br.readLine() var line: String? = br.readLine()
while (line != null) { while (line != null) {

View File

@ -65,7 +65,7 @@ internal object DesktopLauncher {
width = maximumWindowBounds.width, width = maximumWindowBounds.width,
height = maximumWindowBounds.height height = maximumWindowBounds.height
) )
FileHandle(SETTINGS_FILE_NAME).writeString(json().toJson(settings), false) // so when we later open the game we get fullscreen FileHandle(SETTINGS_FILE_NAME).writeString(json().toJson(settings), false, Charsets.UTF_8.name()) // so when we later open the game we get fullscreen
} }
// Kludge! This is a workaround - the matching call in DesktopDisplay doesn't "take" quite permanently, // Kludge! This is a workaround - the matching call in DesktopDisplay doesn't "take" quite permanently,
// the window might revert to the "config" values when the user moves the window - worse if they // the window might revert to the "config" values when the user moves the window - worse if they

View File

@ -121,7 +121,7 @@ internal object ImagePacker {
// An image folder can optionally have a TexturePacker settings file // An image folder can optionally have a TexturePacker settings file
val settingsFile = File("$input${File.separator}TexturePacker.settings") val settingsFile = File("$input${File.separator}TexturePacker.settings")
val settings = if (settingsFile.exists()) val settings = if (settingsFile.exists())
Json().fromJson(TexturePacker.Settings::class.java, settingsFile.reader()) Json().fromJson(TexturePacker.Settings::class.java, settingsFile.reader(Charsets.UTF_8))
else defaultSettings else defaultSettings
TexturePacker.process(settings, input, output, packFileName) TexturePacker.process(settings, input, output, packFileName)

View File

@ -39,6 +39,9 @@ object SystemUtils {
builder.appendLine("Max Memory: $maxMemory MB") builder.appendLine("Max Memory: $maxMemory MB")
} }
// Encoding used by Java when not explicitly specified/-able (such as atlas loader)
builder.appendLine("System default encoding: " + Charset.defaultCharset().name())
return builder.toString() return builder.toString()
} }