#KT-3533 Fixed - not to null in encoding name parameter in String, Bytes, and File.

This commit is contained in:
Zemian Deng
2013-04-22 00:26:21 -04:00
committed by Evgeny Gerashchenko
parent b6fc0c5c13
commit c3b46341d7
5 changed files with 49 additions and 32 deletions
+2 -7
View File
@@ -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.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length)
public inline fun ByteArray.toString(encoding: String?): String { public inline fun ByteArray.toString(encoding: String): String = String(this, encoding)
if (encoding != null) { public inline fun ByteArray.toString(): String = String(this)
return String(this, encoding)
} else {
return String(this)
}
}
public inline fun ByteArray.toString(encoding: Charset) : String = String(this, encoding) public inline fun ByteArray.toString(encoding: Charset) : String = String(this, encoding)
+2 -7
View File
@@ -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 CharSequence.length() : Int = (this as java.lang.CharSequence).length()
public inline fun String.toByteArray(encoding: String?=null):ByteArray { public inline fun String.toByteArray():ByteArray = (this as java.lang.String).getBytes()
if(encoding==null) { public inline fun String.toByteArray(encoding: String):ByteArray = (this as java.lang.String).getBytes(encoding)
return (this as java.lang.String).getBytes()
} else {
return (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.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) public inline fun String.toBoolean() : Boolean = java.lang.Boolean.parseBoolean(this)
+29 -16
View File
@@ -110,33 +110,41 @@ public fun File.appendBytes(data: ByteArray): Unit {
} }
/** /**
* Reads the entire content of the file as a String using the optional * Reads the entire content of the file as a String using the a character encoding.
* character encoding. The default platform encoding is used if the character
* encoding is not specified or null.
* *
* This method is not recommended on huge files. * 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. * character encoding.
* *
* This method is not recommended on huge files. * 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) public fun File.readText(encoding:Charset) : String = readBytes().toString(encoding)
/** /**
* Writes the text as the contents of the file using the optional * Writes the text as the contents of the file using the a
* character encoding. The default platform encoding is used if the character * character encoding.
* encoding is not specified or null.
*/ */
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 * Writes the text as the contents of the file using the default platform
* character encoding. The default platform encoding is used if the character * character encoding.
* encoding is not specified or null. */
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)) } 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. * Appends text to the contents of the file using a character encoding.
* The default platform encoding is used if the character encoding is
* not specified or null.
*/ */
public fun File.appendText(text: String, encoding: String? = null): Unit { public fun File.appendText(text: String, encoding: String): Unit {
appendBytes(text.toByteArray(encoding)) 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 * Copies this file to the given output file, returning the number of bytes copied
*/ */
+9 -2
View File
@@ -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. * 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. * Reads the entire content of the URL as a String with the specified character encoding.
+7
View File
@@ -288,4 +288,11 @@ class StringJVMTest {
assertEquals(example.trim(), example.trimLeading().trimTrailing()) 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())
}
} }