-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteBufferRadAccessor.kt
211 lines (197 loc) · 6.39 KB
/
ByteBufferRadAccessor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@file:JvmMultifileClass
@file:JvmName("Rad")
@file:Suppress("FunctionName")
package fluxo.io.rad
import fluxo.io.internal.Blocking
import fluxo.io.util.checkOffsetAndCount
import fluxo.io.util.toIntChecked
import java.io.File
import java.io.FileDescriptor
import java.io.FileInputStream
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.channels.FileChannel.MapMode
/**
* Creates a new [RandomAccessData] instance backed by the given [ByteBuffer].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [ByteBuffer]
* @param offset the offset of the section
* @param size the length of the section
* @param resources the optional resources to close when finished
*/
@JvmOverloads
@JvmName("forByteBuffer")
public fun RadByteBufferAccessor(
data: ByteBuffer,
offset: Int = data.position(),
size: Int = data.limit() - offset,
vararg resources: AutoCloseable,
): RandomAccessData = ByteBufferRad(data, offset = offset, size = size, resources = resources)
/**
* Creates a new [ByteBuffer]-based [RandomAccessData] instance backed by the given [array][data].
*
* @param data the underlying [ByteArray]
* @param offset the offset of the section
* @param size the length of the section
*/
@JvmOverloads
@JvmName("forByteBuffer")
public fun RadByteBufferAccessor(
data: ByteArray,
offset: Int = 0,
size: Int = data.size - offset,
): RandomAccessData = ByteBufferRad(data, offset = offset, size = size)
private fun byteBufferMmapRad0(
channel: FileChannel,
offset: Long,
size: Int,
vararg resources: AutoCloseable,
): RandomAccessData {
val dataLength = channel.size()
val size0 = if (size == -1) (dataLength - offset).toIntChecked() else size
checkOffsetAndCount(dataLength, offset, size0.toLong())
val byteBuffer = channel.map(MapMode.READ_ONLY, offset, size0.toLong())
return ByteBufferRad(byteBuffer, offset = 0, size = size0, resources = resources)
}
/**
* Creates a new memory-mapping [ByteBuffer]-based [RandomAccessData] instance
* from the given [FileInputStream].
*
* It uses memory-mapped IO for the given section
* (maps a region of this channel's file directly into memory).
*
* **WARNING: Only files under [2 GB][Int.MAX_VALUE] can be mapped into memory!**
*
* **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.
*
* @see java.nio.channels.FileChannel.map
*/
@Blocking
@JvmOverloads
@JvmName("forByteBuffer")
public fun RadByteBufferAccessor(
data: FileInputStream,
offset: Long = 0L,
size: Int = -1,
): RandomAccessData {
val channel = data.channel
return byteBufferMmapRad0(channel, offset = offset, size = size, channel, data)
}
/**
* Creates a new memory-mapping [ByteBuffer]-based [RandomAccessData] instance
* from the given [FileChannel].
*
* It uses memory-mapped IO for the given section
* (maps a region of this channel's file directly into memory).
*
* **WARNING: Only files under [2 GB][Int.MAX_VALUE] can be mapped into memory!**
*
* **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.
*
* @see java.nio.channels.FileChannel.map
*/
@Blocking
@JvmOverloads
@JvmName("forByteBuffer")
public fun RadByteBufferAccessor(
data: FileChannel,
offset: Long = 0L,
size: Int = -1,
): RandomAccessData = byteBufferMmapRad0(data, offset = offset, size = size, data)
/**
* Creates a new memory-mapping [ByteBuffer]-based [RandomAccessData] instance
* for the given [File].
*
* It uses memory-mapped IO for the given section
* (maps a region of this channel's file directly into memory).
*
* **WARNING: Only files under [2 GB][Int.MAX_VALUE] can be mapped into memory!**
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [File]
* @param offset the offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
*
* @see java.nio.channels.FileChannel.map
*/
@Blocking
@JvmOverloads
@JvmName("forByteBuffer")
public fun RadByteBufferAccessor(
data: File,
offset: Long = 0L,
size: Int = -1,
): RandomAccessData {
val stream = FileInputStream(data)
val channel = stream.channel
return byteBufferMmapRad0(channel, offset = offset, size = size, channel, stream)
}
/**
* Creates a new memory-mapping [ByteBuffer]-based [RandomAccessData] instance
* from by the given [FileDescriptor].
*
* It uses memory-mapped IO for the given section
* (maps a region of this channel's file directly into memory).
*
* **WARNING: Only files under [2 GB][Int.MAX_VALUE] can be mapped into memory!**
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param data the underlying [FileDescriptor]
* @param offset the offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
*
* @see java.nio.channels.FileChannel.map
*/
@Blocking
@JvmOverloads
@JvmName("forByteBuffer")
public fun RadByteBufferAccessor(
data: FileDescriptor,
offset: Long = 0L,
size: Int = -1,
): RandomAccessData {
val stream = FileInputStream(data)
val channel = stream.channel
return byteBufferMmapRad0(channel, offset = offset, size = size, channel, stream)
}
@Blocking
@JvmOverloads
@JvmName("forMemoryMappedFile")
@Deprecated(
message = "Use RadByteBufferAccessor instead",
replaceWith = ReplaceWith("RadByteBufferAccessor"),
)
public fun RadMemoryMappedAccessor(
data: FileChannel,
offset: Long = 0L,
size: Int = -1,
): RandomAccessData = RadByteBufferAccessor(data, offset = offset, size = size)
@Blocking
@JvmOverloads
@JvmName("forMemoryMappedFile")
@Deprecated(
message = "Use RadByteBufferAccessor instead",
replaceWith = ReplaceWith("RadByteBufferAccessor"),
)
public fun RadMemoryMappedAccessor(
data: File,
offset: Long = 0L,
size: Int = -1,
): RandomAccessData = RadByteBufferAccessor(data, offset = offset, size = size)