switched to UTF-8 by default for string encoding
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.NoSuchElementException
|
|||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
protected val defaultBufferSize: Int = 64 * 1024
|
protected val defaultBufferSize: Int = 64 * 1024
|
||||||
|
protected val defaultCharset: String = "UTF-8"
|
||||||
|
|
||||||
inline fun print(message : Any?) {
|
inline fun print(message : Any?) {
|
||||||
System.out?.print(message)
|
System.out?.print(message)
|
||||||
@@ -293,8 +294,8 @@ fun File.relativePath(descendant: File): String {
|
|||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
fun File.readText(): String {
|
fun File.readText(encoding: String = defaultCharset): String {
|
||||||
return FileReader(this).use<FileReader,String>{ it.readText() }
|
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.
|
* 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()
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user