-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomAccessFileRadAccessor.kt
66 lines (61 loc) · 1.95 KB
/
RandomAccessFileRadAccessor.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
@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.FileNotFoundException
import java.io.RandomAccessFile
/**
* Creates a new [RandomAccessData] instance backed by the given [RandomAccessFile].
*
* **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 [RandomAccessFile]
* @param offset the offset of the section
* @param size the optional length of the section. -1 means the rest of the file.
*/
@Blocking
@JvmOverloads
@JvmName("forRandomAccessFile")
public fun RandomAccessFileRadAccessor(
data: RandomAccessFile,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData {
val dataLength = data.length()
val size0 = if (size == -1L) dataLength - offset else size
checkOffsetAndCount(dataLength, offset, size0)
return RandomAccessFileRad(data, offset = offset, size = size0)
}
/**
* Creates a new [RandomAccessFile]-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.
*
* @throws IllegalArgumentException if the file doesn't exist
*/
@Blocking
@JvmOverloads
@JvmName("forRandomAccessFile")
@Throws(FileNotFoundException::class)
public fun RandomAccessFileRadAccessor(
data: File,
offset: Long = 0L,
size: Long = -1L,
): RandomAccessData =
RandomAccessFileRadAccessor(RandomAccessFile(data, "r"), offset = offset, size = size)