From 4f14c18aee4a8261892febb8d04cca8dcc3924ec Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 7 Mar 2012 19:16:18 +0000 Subject: [PATCH] added a few more file & stream helper methods --- libraries/stdlib/src/JavaIo.kt | 126 ++++++++++++++++++++++++++++----- 1 file changed, 110 insertions(+), 16 deletions(-) diff --git a/libraries/stdlib/src/JavaIo.kt b/libraries/stdlib/src/JavaIo.kt index 6dcd0a421b1..dd3a982f984 100644 --- a/libraries/stdlib/src/JavaIo.kt +++ b/libraries/stdlib/src/JavaIo.kt @@ -3,6 +3,9 @@ package kotlin.io import java.io.* import java.nio.charset.* import java.util.NoSuchElementException +import java.net.URL + +protected val defaultBufferSize: Int = 64 * 1024 inline fun print(message : Any?) { System.out?.print(message) @@ -273,22 +276,92 @@ fun File.isDescendant(file: File): Boolean { } /** -* Loads the text file as a String -*/ -fun File.loadText(): String { - val buffer = StringWriter() - FileReader(this).use { - it.copyTo(buffer) + * Returns the relative path of the given descendant of this file if its a descendant + */ +fun File.relativePath(descendant: File): String { + val prefix = this.directory.canonicalPath + val answer = descendant.canonicalPath + return if (answer.startsWith(prefix)) { + answer.substring(prefix.size + 1) + } else { + answer } +} + +/** + * Reads the entire content of the file as a String + * + * This method is not recommended on huge files. + */ +fun File.readText(): String { + return FileReader(this).use{ it.readText() } +} + +/** + * Reads the entire content of the file as bytes + * + * This method is not recommended on huge files. + */ +fun File.readBytes(): ByteArray { + return FileInputStream(this).use{ it.readBytes() } +} + +/** + * Writes the bytes as the contents of the file + */ +fun File.writeBytes(data: ByteArray): Unit { + return FileOutputStream(this).use{ it.write(data) } +} + +/** + * Writes the text as the contents of the file + */ +fun File.writeText(text: String): Unit { + return FileWriter(this).use{ it.write(text) } +} + +/** + * Copies this file to the given output file, returning the number of bytes copied + */ +fun File.copyTo(file: File, bufferSize: Int = defaultBufferSize): Long { + file.directory.mkdirs() + val input = FileInputStream(this) + return input.use{ + val output = FileOutputStream(file) + output.use{ + input.copyTo(output, bufferSize) + } + } +} + +/** + * Reads this stream completely into a byte array + * + * **Note** it is the callers responsibility to close this resource + */ +fun InputStream.readBytes(): ByteArray { + val buffer = ByteArrayOutputStream() + this.copyTo(buffer) + return buffer.toByteArray().sure() +} + +/** + * Reads this reader completely as a String + * + * **Note** it is the callers responsibility to close this resource + */ +fun Reader.readText(): String { + val buffer = StringWriter() + this.copyTo(buffer) return buffer.toString() ?: "" } /** -* Copies this stream to the given output stream, returning the number of bytes copied -* -* **Note** it is the callers responsibility to close both of these resources -*/ -fun InputStream.copyTo(out: OutputStream, bufferSize: Int = 64 * 1024): Long { + * Copies this stream to the given output stream, returning the number of bytes copied + * + * **Note** it is the callers responsibility to close both of these resources + */ +fun InputStream.copyTo(out: OutputStream, bufferSize: Int = defaultBufferSize): Long { var bytesCopied: Long = 0 val buffer = ByteArray(bufferSize) var bytes = read(buffer) @@ -302,11 +375,11 @@ fun InputStream.copyTo(out: OutputStream, bufferSize: Int = 64 * 1024): Long { /** -* Copies this reader to the given output writer, returning the number of bytes copied. -* -* **Note** it is the callers responsibility to close both of these resources -*/ -fun Reader.copyTo(out: Writer, bufferSize: Int = 64 * 1024): Long { + * Copies this reader to the given output writer, returning the number of bytes copied. + * + * **Note** it is the callers responsibility to close both of these resources + */ +fun Reader.copyTo(out: Writer, bufferSize: Int = defaultBufferSize): Long { var charsCopied: Long = 0 val buffer = CharArray(bufferSize) var chars = read(buffer) @@ -317,3 +390,24 @@ fun Reader.copyTo(out: Writer, bufferSize: Int = 64 * 1024): Long { } return charsCopied } + + +/** + * Reads the entire content of the URL as a String with an optional character set name + * + * This method is not recommended on huge files. + */ +fun URL.readText(encoding: String? = null): String { + val bytes = readBytes() + return if (encoding != null) String(bytes, encoding) else String(bytes) +} + +/** + * Reads the entire content of the URL as bytes + * + * This method is not recommended on huge files. + */ +fun URL.readBytes(): ByteArray { + return this.openStream().sure().use{ it.readBytes() } +} +