From c3b46341d7af1ffed6e8df7df8cf87bf7001b540 Mon Sep 17 00:00:00 2001 From: Zemian Deng Date: Mon, 22 Apr 2013 00:26:21 -0400 Subject: [PATCH] #KT-3533 Fixed - not to null in encoding name parameter in String, Bytes, and File. --- libraries/stdlib/src/kotlin/ArraysJVM.kt | 9 +---- libraries/stdlib/src/kotlin/StringsJVM.kt | 9 +---- libraries/stdlib/src/kotlin/io/Files.kt | 45 +++++++++++++++-------- libraries/stdlib/src/kotlin/io/JIO.kt | 11 +++++- libraries/stdlib/test/StringJVMTest.kt | 7 ++++ 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/libraries/stdlib/src/kotlin/ArraysJVM.kt b/libraries/stdlib/src/kotlin/ArraysJVM.kt index 0b068cfe98c..0bcb7ada0f9 100644 --- a/libraries/stdlib/src/kotlin/ArraysJVM.kt +++ b/libraries/stdlib/src/kotlin/ArraysJVM.kt @@ -104,12 +104,7 @@ public inline val ByteArray.inputStream : ByteArrayInputStream public inline fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length) -public inline fun ByteArray.toString(encoding: String?): String { - if (encoding != null) { - return String(this, encoding) - } else { - return String(this) - } -} +public inline fun ByteArray.toString(encoding: String): String = String(this, encoding) +public inline fun ByteArray.toString(): String = String(this) public inline fun ByteArray.toString(encoding: Charset) : String = String(this, encoding) \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/StringsJVM.kt b/libraries/stdlib/src/kotlin/StringsJVM.kt index c0d9dc8d1b3..982a9653989 100644 --- a/libraries/stdlib/src/kotlin/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/StringsJVM.kt @@ -146,13 +146,8 @@ 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(encoding: String?=null):ByteArray { - if(encoding==null) { - return (this as java.lang.String).getBytes() - } else { - return (this as java.lang.String).getBytes(encoding) - } -} +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: 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 0f48ec9bff7..186b033f6df 100644 --- a/libraries/stdlib/src/kotlin/io/Files.kt +++ b/libraries/stdlib/src/kotlin/io/Files.kt @@ -110,33 +110,41 @@ public fun File.appendBytes(data: ByteArray): Unit { } /** - * Reads the entire content of the file as a String using the optional - * character encoding. The default platform encoding is used if the character - * encoding is not specified or null. + * Reads the entire content of the file as a String using the a character encoding. * * This method is not recommended on huge files. */ -public fun File.readText(encoding:String? = null) : String = readBytes().toString(encoding) +public fun File.readText(encoding:String) : String = readBytes().toString(encoding) /** - * Reads the entire content of the file as a String using the + * 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() + +/** + * Reads the entire content of the file as a String using a character encoding. + * + * This method is not recommended on huge files. + */ public fun File.readText(encoding:Charset) : String = readBytes().toString(encoding) /** - * Writes the text as the contents of the file using the optional - * character encoding. The default platform encoding is used if the character - * encoding is not specified or null. + * Writes the text as the contents of the file using the a + * character encoding. */ -public fun File.writeText(text: String, encoding:String?=null): Unit { writeBytes(text.toByteArray(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 optional - * character encoding. The default platform encoding is used if the character - * encoding is not specified or null. + * 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()) } + +/** + * Writes the text as the contents of the file using a character encoding. */ public fun File.writeText(text: String, encoding:Charset): Unit { writeBytes(text.toByteArray(encoding)) } @@ -148,14 +156,19 @@ public fun File.appendText(text: String, encoding: Charset): Unit { } /** - * Appends text to the contents of the file using optional character encoding. - * The default platform encoding is used if the character encoding is - * not specified or null. + * Appends text to the contents of the file using a character encoding. */ -public fun File.appendText(text: String, encoding: String? = null): Unit { +public fun File.appendText(text: String, encoding: String): 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 b3bf5b261eb..bd3440f72ba 100644 --- a/libraries/stdlib/src/kotlin/io/JIO.kt +++ b/libraries/stdlib/src/kotlin/io/JIO.kt @@ -315,11 +315,18 @@ public fun Reader.copyTo(out: Writer, bufferSize: Int = defaultBufferSize): Long } /** - * Reads the entire content of the URL as a String with an optional character set name + * Reads the entire content of the URL as a String with a character set name * * This method is not recommended on huge files. */ -public fun URL.readText(encoding: String? = null): String = readBytes().toString(encoding) +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() /** * Reads the entire content of the URL as a String with the specified character encoding. diff --git a/libraries/stdlib/test/StringJVMTest.kt b/libraries/stdlib/test/StringJVMTest.kt index 8e884e66fd3..c607b40e91c 100644 --- a/libraries/stdlib/test/StringJVMTest.kt +++ b/libraries/stdlib/test/StringJVMTest.kt @@ -288,4 +288,11 @@ class StringJVMTest { assertEquals(example.trim(), example.trimLeading().trimTrailing()) } } + + test fun toByteArrayEncodings() { + val s = "hello" + val defaultCharset = java.nio.charset.Charset.defaultCharset()!! + assertEquals(s.toByteArray().toString(), s.toByteArray(defaultCharset).toString()) + assertEquals(s.toByteArray().toString(), s.toByteArray(defaultCharset.name()).toString()) + } }