Inline-only in kotlin.io

This commit is contained in:
Ilya Gorbunov
2016-01-29 04:23:50 +03:00
parent 47d580cbc5
commit 54977ee09a
4 changed files with 86 additions and 44 deletions
+44 -23
View File
@@ -7,114 +7,135 @@ import java.io.InputStreamReader
import java.io.BufferedReader import java.io.BufferedReader
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message to the standard output stream. */ /** 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) System.out.print(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints the given message and newline to the standard output stream. */ /** 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) System.out.println(message)
} }
/** Prints a newline to the standard output stream. */ /** Prints a newline to the standard output stream. */
public fun println() { @kotlin.internal.InlineOnly
public inline fun println() {
System.out.println() System.out.println()
} }
// Since System.in can change its value on the course of program running, // 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 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. // 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 { public override fun read(): Int {
return System.`in`.read() 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 { public override fun read(b: ByteArray, off: Int, len: Int): Int {
return System.`in`.read(b, off, len) return System.`in`.read(b, off, len)
} }
})) }))}
/** /**
* Reads a line of input from the standard input stream. * Reads a line of input from the standard input stream.
@@ -11,31 +11,36 @@ import java.nio.charset.Charset
/** /**
* Returns a new [FileReader] for reading the content of this file. * 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. * Returns a new [BufferedReader] for reading the content of this file.
* *
* @param bufferSize necessary size of the buffer. * @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. * 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. * Returns a new [BufferedWriter] for writing the content of this file.
* *
* @param bufferSize necessary size of the buffer. * @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. * 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. * Gets the entire content of this file as a byte array.
@@ -195,14 +200,16 @@ public fun File.readLines(charset: String): List<String> = readLines(Charset.for
/** /**
* Constructs a new FileInputStream of this file and returns it as a result. * 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) return FileInputStream(this)
} }
/** /**
* Constructs a new FileOutputStream of this file and returns it as a result. * 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) return FileOutputStream(this)
} }
+18 -9
View File
@@ -41,32 +41,38 @@ public operator fun BufferedInputStream.iterator(): ByteIterator =
/** Creates a new byte input stream for the string. */ /** 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. * 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. * 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 offset the start offset of the portion of the array to read.
* @param length the length 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. * Creates a buffered input stream wrapping this stream.
* @param bufferSize the buffer size to use. * @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) = if (this is BufferedInputStream) this else BufferedInputStream(this, bufferSize)
/** Creates a reader on this input stream using UTF-8 or the specified [charset]. */ /** 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]. */ /** 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]. */ /** 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) @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. * Creates a buffered output stream wrapping this stream.
* @param bufferSize the buffer size to use. * @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) = if (this is BufferedOutputStream) this else BufferedOutputStream(this, bufferSize)
/** Creates a writer on this output stream using UTF-8 or the specified [charset]. */ /** 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]. */ /** 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]. */ /** 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) @Deprecated("Use OutputStream.writer(Charset) instead.", ReplaceWith("this.writer(charset(charset))"), level = DeprecationLevel.ERROR)
+10 -5
View File
@@ -9,11 +9,13 @@ import java.util.NoSuchElementException
/** Returns a buffered reader wrapping this Reader, or this Reader itself if it is already buffered. */ /** 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) = 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. */ /** 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) = if (this is BufferedWriter) this else BufferedWriter(this, bufferSize)
/** /**
@@ -44,7 +46,8 @@ public inline fun <T> Reader.useLines(block: (Sequence<String>) -> T): T =
buffered().use { block(it.lineSequence()) } buffered().use { block(it.lineSequence()) }
/** Creates a new reader for the string. */ /** 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. * 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. * @param charset a character set to use.
* @return a string with this URL entire content. * @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. * 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() } 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 * Executes the given [block] function on this resource and then closes it down correctly whether an exception
* is thrown or not. * 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. * @param block a function to process this closable resource.
* @return the result of [block] function on this closable resource. * @return the result of [block] function on this closable resource.
*/ */
@kotlin.internal.InlineOnly
public inline fun <T : Closeable, R> T.use(block: (T) -> R): R { public inline fun <T : Closeable, R> T.use(block: (T) -> R): R {
var closed = false var closed = false
try { try {