-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeekableByteChannelRadAccessor.kt
119 lines (112 loc) · 3.7 KB
/
SeekableByteChannelRadAccessor.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@file:JvmMultifileClass
@file:JvmName("Rad")
@file:Suppress("FunctionName")
package fluxo.io.rad
import fluxo.io.internal.Blocking
import fluxo.io.util.checkOffsetAndCount
import java.io.File
import java.io.FileDescriptor
import java.io.FileInputStream
import java.nio.channels.SeekableByteChannel
/**
* Creates a new [RandomAccessData] instance backed by the given NIO [SeekableByteChannel].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* **WARNING:
* This implementation uses [synchronized] blocks to ensure thread safety!*
*
* @param data the underlying [SeekableByteChannel]
* @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
*
* @see java.nio.channels.FileChannel
* @see jdk.nio.zipfs.ByteArrayChannel
*/
@Blocking
@JvmOverloads
@JvmName("forSeekableByteChannel")
public fun RadSeekableByteChannelAccessor(
data: SeekableByteChannel,
offset: Long = 0L,
size: Long = -1L,
vararg resources: AutoCloseable = arrayOf(data),
): RandomAccessData {
val dataLength = data.size()
val size0 = if (size == -1L) dataLength - offset else size
checkOffsetAndCount(dataLength, offset, size0)
return SeekableByteChannelRad(data, offset = offset, size = size0, resources = resources)
}
/**
* Creates a new [SeekableByteChannel]-based [RandomAccessData] instance
* from the given [FileInputStream].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* **WARNING:
* This implementation uses [synchronized] blocks to ensure thread safety!*
*
* @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("forSeekableByteChannel")
public fun RadSeekableByteChannelAccessor(
data: FileInputStream,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData {
val channel = data.channel
return RadSeekableByteChannelAccessor(channel, offset = offset, size = size, channel, data)
}
/**
* Creates a new [SeekableByteChannel]-based [RandomAccessData] instance
* for the given [File].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* **WARNING:
* This implementation uses [synchronized] blocks to ensure thread safety!*
*
* @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("forSeekableByteChannel")
public fun RadSeekableByteChannelAccessor(
data: File,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData =
RadSeekableByteChannelAccessor(FileInputStream(data), offset = offset, size = size)
/**
* Creates a new [SeekableByteChannel]-based [RandomAccessData] instance
* from the given [FileDescriptor].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* **WARNING:
* This implementation uses [synchronized] blocks to ensure thread safety!*
*
* @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("forSeekableByteChannel")
public fun RadSeekableByteChannelAccessor(
data: FileDescriptor,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData =
RadSeekableByteChannelAccessor(FileInputStream(data), offset = offset, size = size)