From 54977ee09ad24cddc6fd889e2076071f8e85f0f7 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 29 Jan 2016 04:23:50 +0300 Subject: [PATCH] Inline-only in kotlin.io --- libraries/stdlib/src/kotlin/io/Console.kt | 67 ++++++++++++------- .../stdlib/src/kotlin/io/FileReadWrite.kt | 21 ++++-- libraries/stdlib/src/kotlin/io/IOStreams.kt | 27 +++++--- libraries/stdlib/src/kotlin/io/ReadWrite.kt | 15 +++-- 4 files changed, 86 insertions(+), 44 deletions(-) diff --git a/libraries/stdlib/src/kotlin/io/Console.kt b/libraries/stdlib/src/kotlin/io/Console.kt index ef4aa30e2f0..ee9dcd0feee 100644 --- a/libraries/stdlib/src/kotlin/io/Console.kt +++ b/libraries/stdlib/src/kotlin/io/Console.kt @@ -7,114 +7,135 @@ import java.io.InputStreamReader import java.io.BufferedReader /** Prints the given message to the standard output stream. */ -public fun print(message: Any?) { +@kotlin.internal.InlineOnly +public inline fun print(message: Any?) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Int) { +@kotlin.internal.InlineOnly +public inline fun print(message: Int) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Long) { +@kotlin.internal.InlineOnly +public inline fun print(message: Long) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Byte) { +@kotlin.internal.InlineOnly +public inline fun print(message: Byte) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Short) { +@kotlin.internal.InlineOnly +public inline fun print(message: Short) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Char) { +@kotlin.internal.InlineOnly +public inline fun print(message: Char) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Boolean) { +@kotlin.internal.InlineOnly +public inline fun print(message: Boolean) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Float) { +@kotlin.internal.InlineOnly +public inline fun print(message: Float) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: Double) { +@kotlin.internal.InlineOnly +public inline fun print(message: Double) { System.out.print(message) } /** Prints the given message to the standard output stream. */ -public fun print(message: CharArray) { +@kotlin.internal.InlineOnly +public inline fun print(message: CharArray) { System.out.print(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Any?) { +@kotlin.internal.InlineOnly +public inline fun println(message: Any?) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Int) { +@kotlin.internal.InlineOnly +public inline fun println(message: Int) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Long) { +@kotlin.internal.InlineOnly +public inline fun println(message: Long) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Byte) { +@kotlin.internal.InlineOnly +public inline fun println(message: Byte) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Short) { +@kotlin.internal.InlineOnly +public inline fun println(message: Short) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Char) { +@kotlin.internal.InlineOnly +public inline fun println(message: Char) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Boolean) { +@kotlin.internal.InlineOnly +public inline fun println(message: Boolean) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Float) { +@kotlin.internal.InlineOnly +public inline fun println(message: Float) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: Double) { +@kotlin.internal.InlineOnly +public inline fun println(message: Double) { System.out.println(message) } /** Prints the given message and newline to the standard output stream. */ -public fun println(message: CharArray) { +@kotlin.internal.InlineOnly +public inline fun println(message: CharArray) { System.out.println(message) } /** Prints a newline to the standard output stream. */ -public fun println() { +@kotlin.internal.InlineOnly +public inline fun println() { System.out.println() } // Since System.in can change its value on the course of program running, // we should always delegate to current value and cannot just pass it to InputStreamReader constructor. // We could use "by" implementation, but we can only use "by" with interfaces and InputStream is abstract class. -private val stdin: BufferedReader = BufferedReader(InputStreamReader(object : InputStream() { +private val stdin: BufferedReader by lazy { BufferedReader(InputStreamReader(object : InputStream() { public override fun read(): Int { return System.`in`.read() } @@ -150,7 +171,7 @@ private val stdin: BufferedReader = BufferedReader(InputStreamReader(object : In public override fun read(b: ByteArray, off: Int, len: Int): Int { return System.`in`.read(b, off, len) } -})) +}))} /** * Reads a line of input from the standard input stream. diff --git a/libraries/stdlib/src/kotlin/io/FileReadWrite.kt b/libraries/stdlib/src/kotlin/io/FileReadWrite.kt index 4bd25f047de..f4833f0b43e 100644 --- a/libraries/stdlib/src/kotlin/io/FileReadWrite.kt +++ b/libraries/stdlib/src/kotlin/io/FileReadWrite.kt @@ -11,31 +11,36 @@ import java.nio.charset.Charset /** * Returns a new [FileReader] for reading the content of this file. */ -public fun File.reader(charset: Charset = Charsets.UTF_8): InputStreamReader = inputStream().reader(charset) +@kotlin.internal.InlineOnly +public inline fun File.reader(charset: Charset = Charsets.UTF_8): InputStreamReader = inputStream().reader(charset) /** * Returns a new [BufferedReader] for reading the content of this file. * * @param bufferSize necessary size of the buffer. */ -public fun File.bufferedReader(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedReader = reader(charset).buffered(bufferSize) +@kotlin.internal.InlineOnly +public inline fun File.bufferedReader(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedReader = reader(charset).buffered(bufferSize) /** * Returns a new [FileWriter] for writing the content of this file. */ -public fun File.writer(charset: Charset = Charsets.UTF_8): OutputStreamWriter = outputStream().writer(charset) +@kotlin.internal.InlineOnly +public inline fun File.writer(charset: Charset = Charsets.UTF_8): OutputStreamWriter = outputStream().writer(charset) /** * Returns a new [BufferedWriter] for writing the content of this file. * * @param bufferSize necessary size of the buffer. */ -public fun File.bufferedWriter(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedWriter = writer(charset).buffered(bufferSize) +@kotlin.internal.InlineOnly +public inline fun File.bufferedWriter(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedWriter = writer(charset).buffered(bufferSize) /** * Returns a new [PrintWriter] for writing the content of this file. */ -public fun File.printWriter(charset: Charset = Charsets.UTF_8): PrintWriter = PrintWriter(bufferedWriter(charset)) +@kotlin.internal.InlineOnly +public inline fun File.printWriter(charset: Charset = Charsets.UTF_8): PrintWriter = PrintWriter(bufferedWriter(charset)) /** * Gets the entire content of this file as a byte array. @@ -195,14 +200,16 @@ public fun File.readLines(charset: String): List = readLines(Charset.for /** * Constructs a new FileInputStream of this file and returns it as a result. */ -public fun File.inputStream(): FileInputStream { +@kotlin.internal.InlineOnly +public inline fun File.inputStream(): FileInputStream { return FileInputStream(this) } /** * Constructs a new FileOutputStream of this file and returns it as a result. */ -public fun File.outputStream(): FileOutputStream { +@kotlin.internal.InlineOnly +public inline fun File.outputStream(): FileOutputStream { return FileOutputStream(this) } diff --git a/libraries/stdlib/src/kotlin/io/IOStreams.kt b/libraries/stdlib/src/kotlin/io/IOStreams.kt index a2603d42d5d..225ff805e0d 100644 --- a/libraries/stdlib/src/kotlin/io/IOStreams.kt +++ b/libraries/stdlib/src/kotlin/io/IOStreams.kt @@ -41,32 +41,38 @@ public operator fun BufferedInputStream.iterator(): ByteIterator = /** Creates a new byte input stream for the string. */ -public fun String.byteInputStream(charset: Charset = Charsets.UTF_8): ByteArrayInputStream = ByteArrayInputStream(toByteArray(charset)) +@kotlin.internal.InlineOnly +public inline fun String.byteInputStream(charset: Charset = Charsets.UTF_8): ByteArrayInputStream = ByteArrayInputStream(toByteArray(charset)) /** * Creates an input stream for reading data from this byte array. */ -public fun ByteArray.inputStream(): ByteArrayInputStream = ByteArrayInputStream(this) +@kotlin.internal.InlineOnly +public inline fun ByteArray.inputStream(): ByteArrayInputStream = ByteArrayInputStream(this) /** * Creates an input stream for reading data from the specified portion of this byte array. * @param offset the start offset of the portion of the array to read. * @param length the length of the portion of the array to read. */ -public fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length) +@kotlin.internal.InlineOnly +public inline fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length) /** * Creates a buffered input stream wrapping this stream. * @param bufferSize the buffer size to use. */ -public fun InputStream.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedInputStream +@kotlin.internal.InlineOnly +public inline fun InputStream.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedInputStream = if (this is BufferedInputStream) this else BufferedInputStream(this, bufferSize) /** Creates a reader on this input stream using UTF-8 or the specified [charset]. */ -public fun InputStream.reader(charset: Charset = Charsets.UTF_8): InputStreamReader = InputStreamReader(this, charset) +@kotlin.internal.InlineOnly +public inline fun InputStream.reader(charset: Charset = Charsets.UTF_8): InputStreamReader = InputStreamReader(this, charset) /** Creates a buffered reader on this input stream using UTF-8 or the specified [charset]. */ -public fun InputStream.bufferedReader(charset: Charset = Charsets.UTF_8): BufferedReader = reader(charset).buffered() +@kotlin.internal.InlineOnly +public inline fun InputStream.bufferedReader(charset: Charset = Charsets.UTF_8): BufferedReader = reader(charset).buffered() /** Creates a reader on this input stream using the specified [charset]. */ @Deprecated("Use InputStream.reader(Charset) instead.", ReplaceWith("this.reader(charset(charset))"), level = DeprecationLevel.ERROR) @@ -80,14 +86,17 @@ public fun InputStream.bufferedReader(charset: String): BufferedReader = reader( * Creates a buffered output stream wrapping this stream. * @param bufferSize the buffer size to use. */ -public fun OutputStream.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedOutputStream +@kotlin.internal.InlineOnly +public inline fun OutputStream.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedOutputStream = if (this is BufferedOutputStream) this else BufferedOutputStream(this, bufferSize) /** Creates a writer on this output stream using UTF-8 or the specified [charset]. */ -public fun OutputStream.writer(charset: Charset = Charsets.UTF_8): OutputStreamWriter = OutputStreamWriter(this, charset) +@kotlin.internal.InlineOnly +public inline fun OutputStream.writer(charset: Charset = Charsets.UTF_8): OutputStreamWriter = OutputStreamWriter(this, charset) /** Creates a buffered writer on this output stream using UTF-8 or the specified [charset]. */ -public fun OutputStream.bufferedWriter(charset: Charset = Charsets.UTF_8): BufferedWriter = writer(charset).buffered() +@kotlin.internal.InlineOnly +public inline fun OutputStream.bufferedWriter(charset: Charset = Charsets.UTF_8): BufferedWriter = writer(charset).buffered() /** Creates a writer on this output stream using the specified [charset]. */ @Deprecated("Use OutputStream.writer(Charset) instead.", ReplaceWith("this.writer(charset(charset))"), level = DeprecationLevel.ERROR) diff --git a/libraries/stdlib/src/kotlin/io/ReadWrite.kt b/libraries/stdlib/src/kotlin/io/ReadWrite.kt index 94a56ccaf7b..46d96f5be5e 100644 --- a/libraries/stdlib/src/kotlin/io/ReadWrite.kt +++ b/libraries/stdlib/src/kotlin/io/ReadWrite.kt @@ -9,11 +9,13 @@ import java.util.NoSuchElementException /** Returns a buffered reader wrapping this Reader, or this Reader itself if it is already buffered. */ -public fun Reader.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedReader +@kotlin.internal.InlineOnly +public inline fun Reader.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedReader = if (this is BufferedReader) this else BufferedReader(this, bufferSize) /** Returns a buffered reader wrapping this Writer, or this Writer itself if it is already buffered. */ -public fun Writer.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedWriter +@kotlin.internal.InlineOnly +public inline fun Writer.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedWriter = if (this is BufferedWriter) this else BufferedWriter(this, bufferSize) /** @@ -44,7 +46,8 @@ public inline fun Reader.useLines(block: (Sequence) -> T): T = buffered().use { block(it.lineSequence()) } /** Creates a new reader for the string. */ -public fun String.reader(): StringReader = StringReader(this) +@kotlin.internal.InlineOnly +public inline fun String.reader(): StringReader = StringReader(this) /** * Returns a sequence of corresponding file lines. @@ -138,7 +141,8 @@ public fun URL.readText(charset: String): String = readBytes().toString(charset( * @param charset a character set to use. * @return a string with this URL entire content. */ -public fun URL.readText(charset: Charset = Charsets.UTF_8): String = readBytes().toString(charset) +@kotlin.internal.InlineOnly +public inline fun URL.readText(charset: Charset = Charsets.UTF_8): String = readBytes().toString(charset) /** * Reads the entire content of the URL as byte array. @@ -149,7 +153,7 @@ public fun URL.readText(charset: Charset = Charsets.UTF_8): String = readBytes() */ public fun URL.readBytes(): ByteArray = openStream().use { it.readBytes() } -// TODO: Move to kotlin package, rename to using +// TODO: Provide use kotlin package for AutoCloseable /** * Executes the given [block] function on this resource and then closes it down correctly whether an exception * is thrown or not. @@ -157,6 +161,7 @@ public fun URL.readBytes(): ByteArray = openStream().use { it.readBytes() } * @param block a function to process this closable resource. * @return the result of [block] function on this closable resource. */ +@kotlin.internal.InlineOnly public inline fun T.use(block: (T) -> R): R { var closed = false try {