From a2b41a717e079e30cebe129526c63bcce501baf9 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 7 Mar 2012 20:08:23 +0000 Subject: [PATCH] switched to UTF-8 by default for string encoding --- libraries/stdlib/src/JavaIo.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/stdlib/src/JavaIo.kt b/libraries/stdlib/src/JavaIo.kt index dd3a982f984..bcce2e368f8 100644 --- a/libraries/stdlib/src/JavaIo.kt +++ b/libraries/stdlib/src/JavaIo.kt @@ -6,6 +6,7 @@ import java.util.NoSuchElementException import java.net.URL protected val defaultBufferSize: Int = 64 * 1024 +protected val defaultCharset: String = "UTF-8" inline fun print(message : Any?) { System.out?.print(message) @@ -293,8 +294,8 @@ fun File.relativePath(descendant: File): String { * * This method is not recommended on huge files. */ -fun File.readText(): String { - return FileReader(this).use{ it.readText() } +fun File.readText(encoding: String = defaultCharset): String { + return readBytes().toString(encoding) } /** @@ -397,9 +398,16 @@ fun Reader.copyTo(out: Writer, bufferSize: Int = defaultBufferSize): Long { * * This method is not recommended on huge files. */ -fun URL.readText(encoding: String? = null): String { +fun URL.readText(encoding: String = defaultCharset): String { val bytes = readBytes() - return if (encoding != null) String(bytes, encoding) else String(bytes) + return bytes.toString(encoding) +} + +/** + * Converts the bytes to a [[String]] using the given encoding or uses the [[defaultCharset]] (UTF-8) + */ +fun ByteArray.toString(encoding: String = defaultCharset): String { + return if (encoding != null) String(this, encoding) else String(this) } /**