From f7099a5b6accf8d9eea8d35bc62534d191e1a1f1 Mon Sep 17 00:00:00 2001 From: Zemian Deng Date: Tue, 23 Apr 2013 21:26:19 -0400 Subject: [PATCH] Use default param to String encoding name instead of using extra overloaded method. --- libraries/stdlib/src/kotlin/StringsJVM.kt | 3 +-- libraries/stdlib/src/kotlin/io/Files.kt | 27 +++-------------------- libraries/stdlib/src/kotlin/io/JIO.kt | 9 +------- 3 files changed, 5 insertions(+), 34 deletions(-) diff --git a/libraries/stdlib/src/kotlin/StringsJVM.kt b/libraries/stdlib/src/kotlin/StringsJVM.kt index 982a9653989..0c28d711abb 100644 --- a/libraries/stdlib/src/kotlin/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/StringsJVM.kt @@ -146,8 +146,7 @@ public inline fun CharSequence.toString() : String? = (this as java.lang.CharSeq public inline fun CharSequence.length() : Int = (this as java.lang.CharSequence).length() -public inline fun String.toByteArray():ByteArray = (this as java.lang.String).getBytes() -public inline fun String.toByteArray(encoding: String):ByteArray = (this as java.lang.String).getBytes(encoding) +public inline fun String.toByteArray(encoding: String = java.nio.charset.Charset.defaultCharset()!!.name()):ByteArray = (this as java.lang.String).getBytes(encoding) public inline fun String.toByteArray(encoding: java.nio.charset.Charset):ByteArray = (this as java.lang.String).getBytes(encoding) public inline fun String.toBoolean() : Boolean = java.lang.Boolean.parseBoolean(this) diff --git a/libraries/stdlib/src/kotlin/io/Files.kt b/libraries/stdlib/src/kotlin/io/Files.kt index 186b033f6df..3468de7a7a0 100644 --- a/libraries/stdlib/src/kotlin/io/Files.kt +++ b/libraries/stdlib/src/kotlin/io/Files.kt @@ -114,15 +114,7 @@ public fun File.appendBytes(data: ByteArray): Unit { * * This method is not recommended on huge files. */ -public fun File.readText(encoding:String) : String = readBytes().toString(encoding) - -/** - * Reads the entire content of the file as a String using the default platform - * character encoding. - * - * This method is not recommended on huge files. - */ -public fun File.readText() : String = readBytes().toString() +public fun File.readText(encoding:String = java.nio.charset.Charset.defaultCharset()!!.name()) : String = readBytes().toString(encoding) /** * Reads the entire content of the file as a String using a character encoding. @@ -135,13 +127,7 @@ public fun File.readText(encoding:Charset) : String = readBytes().toString(encod * Writes the text as the contents of the file using the a * character encoding. */ -public fun File.writeText(text: String, encoding:String): Unit { writeBytes(text.toByteArray(encoding)) } - -/** - * Writes the text as the contents of the file using the default platform - * character encoding. - */ -public fun File.writeText(text: String): Unit { writeBytes(text.toByteArray()) } +public fun File.writeText(text: String, encoding:String = java.nio.charset.Charset.defaultCharset()!!.name()): Unit { writeBytes(text.toByteArray(encoding)) } /** * Writes the text as the contents of the file using a character encoding. @@ -158,17 +144,10 @@ public fun File.appendText(text: String, encoding: Charset): Unit { /** * Appends text to the contents of the file using a character encoding. */ -public fun File.appendText(text: String, encoding: String): Unit { +public fun File.appendText(text: String, encoding: String = java.nio.charset.Charset.defaultCharset()!!.name()): Unit { appendBytes(text.toByteArray(encoding)) } -/** - * Appends text to the contents of the file using default platform character encoding. - */ -public fun File.appendText(text: String): Unit { - appendBytes(text.toByteArray()) -} - /** * Copies this file to the given output file, returning the number of bytes copied */ diff --git a/libraries/stdlib/src/kotlin/io/JIO.kt b/libraries/stdlib/src/kotlin/io/JIO.kt index bd3440f72ba..91eb5c1fc53 100644 --- a/libraries/stdlib/src/kotlin/io/JIO.kt +++ b/libraries/stdlib/src/kotlin/io/JIO.kt @@ -319,14 +319,7 @@ public fun Reader.copyTo(out: Writer, bufferSize: Int = defaultBufferSize): Long * * This method is not recommended on huge files. */ -public fun URL.readText(encoding: String): String = readBytes().toString(encoding) - -/** - * Reads the entire content of the URL as a String with default character set name - * - * This method is not recommended on huge files. - */ -public fun URL.readText(): String = readBytes().toString() +public fun URL.readText(encoding: String = java.nio.charset.Charset.defaultCharset()!!.name()): String = readBytes().toString(encoding) /** * Reads the entire content of the URL as a String with the specified character encoding.