-
Notifications
You must be signed in to change notification settings - Fork 26
/
Closeable.kt
52 lines (44 loc) · 1.15 KB
/
Closeable.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.smithy.kotlin.runtime.io
import aws.smithy.kotlin.runtime.InternalApi
// this really should live in the stdlib...
// https://youtrack.jetbrains.com/issue/KT-31066
/**
* A resource than can be closed. The [close] method is invoked to release resources
* an object is holding (e.g. such as open files)
*/
public expect interface Closeable {
/**
* Release any resources held by this object
*/
public fun close()
}
/**
* Executes the given [block] on this resource and then closes it whether an exception has
* occurred or not.
*/
public inline fun <C : Closeable, R> C.use(block: (C) -> R): R {
var closed = false
return try {
block(this)
} catch (first: Throwable) {
try {
closed = true
close()
} catch (second: Throwable) {
first.addSuppressed(second)
}
throw first
} finally {
if (!closed) {
close()
}
}
}
@InternalApi
public fun Any.closeIfCloseable() {
if (this is Closeable) close()
}