Move more logic of 'use' into 'closeSuppressed', rename the latter to 'closeFinally', that will result in more compact inline call expansions.
This commit is contained in:
@@ -31,31 +31,33 @@ package kotlin
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
|
||||
var closed = false
|
||||
var exception: Throwable? = null
|
||||
try {
|
||||
return block(this)
|
||||
} catch (e: Throwable) {
|
||||
closed = true
|
||||
this?.closeSuppressed(e)
|
||||
exception = e
|
||||
throw e
|
||||
} finally {
|
||||
if (this != null && !closed) {
|
||||
close()
|
||||
}
|
||||
this.closeFinally(exception)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes this [AutoCloseable] suppressing possible exception or error thrown by [AutoCloseable.close] function.
|
||||
* Closes this [AutoCloseable], suppressing possible exception or error thrown by [AutoCloseable.close] function when
|
||||
* it's being closed due to some other [cause] exception occurred.
|
||||
*
|
||||
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@PublishedApi
|
||||
internal fun AutoCloseable.closeSuppressed(cause: Throwable) {
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Throwable) {
|
||||
cause.addSuppressed(closeException)
|
||||
}
|
||||
internal fun AutoCloseable?.closeFinally(cause: Throwable?) = when {
|
||||
this == null -> {}
|
||||
cause == null -> close()
|
||||
else ->
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Throwable) {
|
||||
cause.addSuppressed(closeException)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,5 +6,5 @@ package kotlin
|
||||
@PublishedApi
|
||||
@Deprecated("Provided for binary compatibility")
|
||||
@JvmName("closeSuppressed")
|
||||
internal fun AutoCloseable.closeSuppressedDeprecated(cause: Throwable) = closeSuppressed(cause)
|
||||
internal fun AutoCloseable.closeSuppressedDeprecated(cause: Throwable) = closeFinally(cause)
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ internal open class PlatformImplementations {
|
||||
@SinceKotlin("1.1")
|
||||
@PublishedApi
|
||||
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
|
||||
internal fun platformCloseSuppressed(instance: Closeable, cause: Throwable) = instance.closeSuppressed(cause)
|
||||
internal fun platformCloseSuppressed(instance: Closeable, cause: Throwable) = instance.closeFinally(cause)
|
||||
|
||||
|
||||
@JvmField
|
||||
|
||||
@@ -30,27 +30,32 @@ import kotlin.internal.*
|
||||
*/
|
||||
@InlineOnly
|
||||
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
|
||||
var closed = false
|
||||
var exception: Throwable? = null
|
||||
try {
|
||||
return block(this)
|
||||
} catch (e: Throwable) {
|
||||
closed = true
|
||||
this?.closeSuppressed(e)
|
||||
exception = e
|
||||
throw e
|
||||
} finally {
|
||||
if (this != null && !closed) {
|
||||
close()
|
||||
}
|
||||
this.closeFinally(exception)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes this [Closeable], suppressing possible exception or error thrown by [Closeable.close] function when
|
||||
* it's being closed due to some other [cause] exception occurred.
|
||||
*
|
||||
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception, when it's supported.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@PublishedApi
|
||||
internal fun Closeable.closeSuppressed(cause: Throwable) {
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Throwable) {
|
||||
// on Java 7 we should call
|
||||
IMPLEMENTATIONS.addSuppressed(cause, closeException)
|
||||
}
|
||||
internal fun Closeable?.closeFinally(cause: Throwable?) = when {
|
||||
this == null -> {}
|
||||
cause == null -> close()
|
||||
else ->
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Throwable) {
|
||||
cause.addSuppressed(closeException)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1758,7 +1758,7 @@ public final class kotlin/io/ByteStreamsKt {
|
||||
}
|
||||
|
||||
public final class kotlin/io/CloseableKt {
|
||||
public static final fun closeSuppressed (Ljava/io/Closeable;Ljava/lang/Throwable;)V
|
||||
public static final fun closeFinally (Ljava/io/Closeable;Ljava/lang/Throwable;)V
|
||||
}
|
||||
|
||||
public final class kotlin/io/ConsoleKt {
|
||||
|
||||
Reference in New Issue
Block a user