Unify charset-related APIs to use Charsets.UTF_8 as default. (Manual merge of PR#491)
Minor cleanup.
This commit is contained in:
@@ -4,13 +4,12 @@ import java.io.*
|
|||||||
import java.nio.charset.*
|
import java.nio.charset.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively process this file and all children with the given block
|
* Recursively process this file and all children with the given block
|
||||||
*/
|
*/
|
||||||
public fun File.recurse(block: (File) -> Unit): Unit {
|
public fun File.recurse(block: (File) -> Unit): Unit {
|
||||||
block(this)
|
block(this)
|
||||||
val children = this.listFiles()
|
val children = listFiles()
|
||||||
if (children != null) {
|
if (children != null) {
|
||||||
for (child in children) {
|
for (child in children) {
|
||||||
child.recurse(block)
|
child.recurse(block)
|
||||||
@@ -22,7 +21,7 @@ public fun File.recurse(block: (File) -> Unit): Unit {
|
|||||||
* Returns this if the file is a directory or the parent if its a file inside a directory
|
* Returns this if the file is a directory or the parent if its a file inside a directory
|
||||||
*/
|
*/
|
||||||
public val File.directory: File
|
public val File.directory: File
|
||||||
get() = if (this.isDirectory()) this else this.getParentFile()!!
|
get() = if (isDirectory()) this else getParentFile()!!
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the canonical path of the file
|
* Returns the canonical path of the file
|
||||||
@@ -47,7 +46,7 @@ public val File.path: String
|
|||||||
*/
|
*/
|
||||||
public val File.extension: String
|
public val File.extension: String
|
||||||
get() {
|
get() {
|
||||||
val text = this.name
|
val text = name
|
||||||
val idx = text.lastIndexOf('.')
|
val idx = text.lastIndexOf('.')
|
||||||
return if (idx >= 0) {
|
return if (idx >= 0) {
|
||||||
text.substring(idx + 1)
|
text.substring(idx + 1)
|
||||||
@@ -60,14 +59,14 @@ public val File.extension: String
|
|||||||
* Returns true if the given file is in the same directory or a descendant directory
|
* Returns true if the given file is in the same directory or a descendant directory
|
||||||
*/
|
*/
|
||||||
public fun File.isDescendant(file: File): Boolean {
|
public fun File.isDescendant(file: File): Boolean {
|
||||||
return file.directory.canonicalPath.startsWith(this.directory.canonicalPath)
|
return file.directory.canonicalPath.startsWith(directory.canonicalPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the relative path of the given descendant of this file if its a descendant
|
* Returns the relative path of the given descendant of this file if its a descendant
|
||||||
*/
|
*/
|
||||||
public fun File.relativePath(descendant: File): String {
|
public fun File.relativePath(descendant: File): String {
|
||||||
val prefix = this.directory.canonicalPath
|
val prefix = directory.canonicalPath
|
||||||
val answer = descendant.canonicalPath
|
val answer = descendant.canonicalPath
|
||||||
return if (answer.startsWith(prefix)) {
|
return if (answer.startsWith(prefix)) {
|
||||||
val prefixSize = prefix.size
|
val prefixSize = prefix.size
|
||||||
@@ -90,7 +89,7 @@ public fun File.reader(): FileReader = FileReader(this)
|
|||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
public fun File.readBytes(): ByteArray {
|
public fun File.readBytes(): ByteArray {
|
||||||
return FileInputStream(this).use { it.readBytes(this.length().toInt()) }
|
return FileInputStream(this).use { it.readBytes(length().toInt()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,46 +107,45 @@ public fun File.appendBytes(data: ByteArray): Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the entire content of the file as a String using the a character encoding.
|
* Reads the entire content of the file as a String using specified charset.
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
public fun File.readText(encoding: String = Charset.defaultCharset().name()): String = readBytes().toString(encoding)
|
public fun File.readText(charset: String): String = readBytes().toString(charset)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the entire content of the file as a String using a character encoding.
|
* Reads the entire content of the file as a String using UTF-8 or specified charset.
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
public fun File.readText(encoding: Charset): String = readBytes().toString(encoding)
|
public fun File.readText(charset: Charset = Charsets.UTF_8): String = readBytes().toString(charset)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the text as the contents of the file using the a
|
* Writes the text as the contents of the file using specified charset.
|
||||||
* character encoding.
|
|
||||||
*/
|
*/
|
||||||
public fun File.writeText(text: String, encoding: String = Charset.defaultCharset().name()): Unit {
|
public fun File.writeText(text: String, charset: String): Unit {
|
||||||
writeBytes(text.toByteArray(encoding))
|
writeBytes(text.toByteArray(charset))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the text as the contents of the file using a character encoding.
|
* Writes the text as the contents of the file using UTF-8 or specified charset.
|
||||||
*/
|
*/
|
||||||
public fun File.writeText(text: String, encoding: Charset): Unit {
|
public fun File.writeText(text: String, charset: Charset = Charsets.UTF_8): Unit {
|
||||||
writeBytes(text.toByteArray(encoding))
|
writeBytes(text.toByteArray(charset))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends text to the contents of the file using a given character encoding.
|
* Appends text to the contents of the file using UTF-8 or specified charset.
|
||||||
*/
|
*/
|
||||||
public fun File.appendText(text: String, encoding: Charset): Unit {
|
public fun File.appendText(text: String, charset: Charset = Charsets.UTF_8): Unit {
|
||||||
appendBytes(text.toByteArray(encoding))
|
appendBytes(text.toByteArray(charset))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends text to the contents of the file using a character encoding.
|
* Appends text to the contents of the file using specified charset.
|
||||||
*/
|
*/
|
||||||
public fun File.appendText(text: String, encoding: String = Charset.defaultCharset().name()): Unit {
|
public fun File.appendText(text: String, charset: String): Unit {
|
||||||
appendBytes(text.toByteArray(encoding))
|
appendBytes(text.toByteArray(charset))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,11 +187,11 @@ public fun File.forEachBlock(closure: (ByteArray, Int) -> Unit): Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads file line by line. Default charset is UTF-8.
|
* Reads file line by line using specified [charset]. Default charset is UTF-8.
|
||||||
*
|
*
|
||||||
* You may use this function on huge files
|
* You may use this function on huge files
|
||||||
*/
|
*/
|
||||||
public fun File.forEachLine(charset: String = "UTF-8", closure: (line: String) -> Unit): Unit {
|
public fun File.forEachLine(charset: Charset = Charsets.UTF_8, closure: (line: String) -> Unit): Unit {
|
||||||
val reader = BufferedReader(InputStreamReader(FileInputStream(this), charset))
|
val reader = BufferedReader(InputStreamReader(FileInputStream(this), charset))
|
||||||
try {
|
try {
|
||||||
reader.forEachLine(closure)
|
reader.forEachLine(closure)
|
||||||
@@ -202,19 +200,29 @@ public fun File.forEachLine(charset: String = "UTF-8", closure: (line: String) -
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads file line by line using the specified [charset].
|
||||||
|
*
|
||||||
|
* You may use this function on huge files
|
||||||
|
*/
|
||||||
|
public fun File.forEachLine(charset: String, closure: (line: String) -> Unit): Unit = forEachLine(Charset.forName(charset), closure)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads file content into list of lines using specified [charset]
|
||||||
|
*
|
||||||
|
* Do not use this function for huge files.
|
||||||
|
*/
|
||||||
|
public fun File.readLines(charset: String): List<String> = readLines(Charset.forName(charset))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads file content as strings list. By default uses UTF-8 charset.
|
* Reads file content as strings list. By default uses UTF-8 charset.
|
||||||
*
|
*
|
||||||
* Do not use this function for huge files.
|
* Do not use this function for huge files.
|
||||||
*/
|
*/
|
||||||
public fun File.readLines(charset: String = "UTF-8"): List<String> {
|
public fun File.readLines(charset: Charset = Charsets.UTF_8): List<String> {
|
||||||
val rs = ArrayList<String>()
|
val result = ArrayList<String>()
|
||||||
|
forEachLine(charset) { result.add(it); }
|
||||||
this.forEachLine(charset) {(line: String): Unit ->
|
return result
|
||||||
rs.add(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11,12 +11,6 @@ import kotlin.InlineOption.ONLY_LOCAL_RETURN
|
|||||||
*/
|
*/
|
||||||
public val defaultBufferSize: Int = 64 * 1024
|
public val defaultBufferSize: Int = 64 * 1024
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the default [[Charset]] which defaults to UTF-8
|
|
||||||
*/
|
|
||||||
public val defaultCharset: Charset = Charset.forName("UTF-8")!!
|
|
||||||
|
|
||||||
|
|
||||||
/** Prints the given message to [[System.out]] */
|
/** Prints the given message to [[System.out]] */
|
||||||
public fun print(message: Any?) {
|
public fun print(message: Any?) {
|
||||||
System.out.print(message)
|
System.out.print(message)
|
||||||
@@ -162,28 +156,28 @@ public fun InputStream.buffered(bufferSize: Int = defaultBufferSize): InputStrea
|
|||||||
else
|
else
|
||||||
BufferedInputStream(this, bufferSize)
|
BufferedInputStream(this, bufferSize)
|
||||||
|
|
||||||
/** Creates a reader on an input stream with specified *encoding* */
|
/** Creates a reader on an input stream using UTF-8 or specified charset. */
|
||||||
public fun InputStream.reader(encoding: Charset = defaultCharset): InputStreamReader = InputStreamReader(this, encoding)
|
public fun InputStream.reader(charset: Charset = Charsets.UTF_8): InputStreamReader = InputStreamReader(this, charset)
|
||||||
|
|
||||||
/** Creates a reader on an input stream with specified *encoding* */
|
/** Creates a reader on an input stream using specified *charset* */
|
||||||
public fun InputStream.reader(encoding: String): InputStreamReader = InputStreamReader(this, encoding)
|
public fun InputStream.reader(charset: String): InputStreamReader = InputStreamReader(this, charset)
|
||||||
|
|
||||||
/** Creates a reader on an input stream with specified *encoding* */
|
/** Creates a reader on an input stream using specified *decoder* */
|
||||||
public fun InputStream.reader(encoding: CharsetDecoder): InputStreamReader = InputStreamReader(this, encoding)
|
public fun InputStream.reader(decoder: CharsetDecoder): InputStreamReader = InputStreamReader(this, decoder)
|
||||||
|
|
||||||
|
|
||||||
/** Creates a buffered output stream */
|
/** Creates a buffered output stream */
|
||||||
public fun OutputStream.buffered(bufferSize: Int = defaultBufferSize): BufferedOutputStream
|
public fun OutputStream.buffered(bufferSize: Int = defaultBufferSize): BufferedOutputStream
|
||||||
= if (this is BufferedOutputStream) this else BufferedOutputStream(this, bufferSize)
|
= if (this is BufferedOutputStream) this else BufferedOutputStream(this, bufferSize)
|
||||||
|
|
||||||
/** Creates a writer on an output stream with specified *encoding* */
|
/** Creates a writer on an output stream using UTF-8 or specified charset. */
|
||||||
public fun OutputStream.writer(encoding: Charset = defaultCharset): OutputStreamWriter = OutputStreamWriter(this, encoding)
|
public fun OutputStream.writer(charset: Charset = Charsets.UTF_8): OutputStreamWriter = OutputStreamWriter(this, charset)
|
||||||
|
|
||||||
/** Creates a writer on an output stream with specified *encoding* */
|
/** Creates a writer on an output stream using specified *charset* */
|
||||||
public fun OutputStream.writer(encoding: String): OutputStreamWriter = OutputStreamWriter(this, encoding)
|
public fun OutputStream.writer(charset: String): OutputStreamWriter = OutputStreamWriter(this, charset)
|
||||||
|
|
||||||
/** Creates a writer on an output stream with specified *encoding* */
|
/** Creates a writer on an output stream using specified *encoder* */
|
||||||
public fun OutputStream.writer(encoding: CharsetEncoder): OutputStreamWriter = OutputStreamWriter(this, encoding)
|
public fun OutputStream.writer(encoder: CharsetEncoder): OutputStreamWriter = OutputStreamWriter(this, encoder)
|
||||||
|
|
||||||
|
|
||||||
/** Creates a buffered reader, or returns self if Reader is already buffered */
|
/** Creates a buffered reader, or returns self if Reader is already buffered */
|
||||||
@@ -299,18 +293,18 @@ public fun Reader.copyTo(out: Writer, bufferSize: Int = defaultBufferSize): Long
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the entire content of the URL as a String with a character set name
|
* Reads the entire content of the URL as a String using specified charset.
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
public fun URL.readText(encoding: String = Charset.defaultCharset().name()): String = readBytes().toString(encoding)
|
public fun URL.readText(charset: String): String = readBytes().toString(charset)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 using UTF-8 or specified charset..
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
public fun URL.readText(encoding: Charset): String = readBytes().toString(encoding)
|
public fun URL.readText(charset: Charset = Charsets.UTF_8): String = readBytes().toString(charset)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the entire content of the URL as bytes
|
* Reads the entire content of the URL as bytes
|
||||||
|
|||||||
Reference in New Issue
Block a user