-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileChannelRadAccessor.kt
99 lines (92 loc) · 2.98 KB
/
FileChannelRadAccessor.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@file:JvmMultifileClass
@file:JvmName("Rad")
@file:Suppress("FunctionName")
package fluxo.io.rad
import fluxo.io.internal.Blocking
import java.io.File
import java.io.FileDescriptor
import java.io.FileInputStream
import java.nio.channels.FileChannel
/**
* Creates a new [RandomAccessData] instance backed by the given [FileChannel].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [FileChannel]
* @param offset the offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
* @param resources the optional resources to close when finished
*/
@Blocking
@JvmOverloads
@JvmName("forFileChannel")
public fun RadFileChannelAccessor(
data: FileChannel,
offset: Long = 0L,
size: Long = -1L,
vararg resources: AutoCloseable = arrayOf(data),
): RandomAccessData {
val size0 = if (size == -1L) data.size() - offset else size
return FileChannelRad(data, offset = offset, size = size0, resources = resources)
}
/**
* Creates a new [FileChannel]-based [RandomAccessData] instance
* from the given [FileInputStream].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [FileInputStream]
* @param offset the optional offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
*/
@Blocking
@JvmOverloads
@JvmName("forFileChannel")
public fun RadFileChannelAccessor(
data: FileInputStream,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData {
val channel = data.channel
return RadFileChannelAccessor(channel, offset = offset, size = size, channel, data)
}
/**
* Creates a new [FileChannel]-based [RandomAccessData] instance
* for the given [File].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [File]
* @param offset the optional offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
*/
@Blocking
@JvmOverloads
@JvmName("forFileChannel")
public fun RadFileChannelAccessor(
data: File,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData = RadFileChannelAccessor(FileInputStream(data), offset = offset, size = size)
/**
* Creates a new [FileChannel]-based [RandomAccessData] instance
* from the given [FileDescriptor].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [FileDescriptor]
* @param offset the optional offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
*/
@Blocking
@JvmOverloads
@JvmName("forFileChannel")
public fun RadFileChannelAccessor(
data: FileDescriptor,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData = RadFileChannelAccessor(FileInputStream(data), offset = offset, size = size)