-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamFactoryRadAccessor.kt
136 lines (127 loc) · 3.97 KB
/
StreamFactoryRadAccessor.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
@file:JvmMultifileClass
@file:JvmName("Rad")
@file:Suppress("FunctionName")
package fluxo.io.rad
import fluxo.io.internal.Blocking
import fluxo.io.rad.StreamFactoryRad.ByteChannelFactory
import fluxo.io.rad.StreamFactoryRad.DataInputFactory
import fluxo.io.rad.StreamFactoryRad.InputStreamFactory
import fluxo.io.util.checkOffsetAndCount
import java.io.DataInput
import java.io.File
import java.io.InputStream
import java.nio.channels.ReadableByteChannel
private const val DEFAULT_MAX_POOL_SIZE = 15
/**
* Creates a new [RandomAccessData] instance based on the [InputStream] Factory
* from the given [File].
*
* **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 length of the section
* @param maxPoolSize the maximum number of streams to keep open
*/
@Blocking
@JvmOverloads
@JvmName("forStreamFactory")
public fun StreamFactoryRadAccessor(
data: File,
offset: Long = 0L,
size: Long = -1L,
maxPoolSize: Int = DEFAULT_MAX_POOL_SIZE,
): RandomAccessData {
val fullSize = data.length()
val size0 = if (size == -1L) fullSize - offset else size
val factory = InputStreamFactory(fullSize, data::inputStream)
return StreamFactoryRad(factory, offset = offset, size = size0, maxPoolSize = maxPoolSize)
}
/**
* Creates a new [RandomAccessData] instance based on the given [InputStream] [factory].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param fullSize the size of the underlying data
* @param offset the offset of the section
* @param size the length of the section
* @param factory the [InputStream] factory
*/
@Blocking
@JvmOverloads
@JvmName("forInputStreamFactory")
public fun StreamFactoryRadAccessor(
fullSize: Long,
offset: Long = 0L,
size: Long = fullSize - offset,
maxPoolSize: Int = DEFAULT_MAX_POOL_SIZE,
factory: () -> InputStream,
): RandomAccessData {
checkOffsetAndCount(fullSize, offset, size)
return StreamFactoryRad(
factory = InputStreamFactory(size, factory),
offset = offset,
size = size,
maxPoolSize = maxPoolSize,
)
}
/**
* Creates a new [RandomAccessData] instance based on the given [DataInput] [factory].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param fullSize the size of the underlying data
* @param offset the offset of the section
* @param size the length of the section
* @param factory the [DataInput] factory
*/
@Blocking
@JvmOverloads
@JvmName("forDataInputFactory")
public fun DataInputFactoryRadAccessor(
fullSize: Long,
offset: Long = 0L,
size: Long = fullSize - offset,
maxPoolSize: Int = DEFAULT_MAX_POOL_SIZE,
factory: () -> DataInput,
): RandomAccessData {
checkOffsetAndCount(fullSize, offset, size)
return StreamFactoryRad(
factory = DataInputFactory(size, factory),
offset = offset,
size = size,
maxPoolSize = maxPoolSize,
)
}
/**
* Creates a new [RandomAccessData] instance based on the given NIO [ReadableByteChannel] [factory].
*
* **WARNING: Remember to close the [RandomAccessData] when finished
* to properly release resources!*
*
* @param fullSize the size of the underlying data
* @param offset the offset of the section
* @param size the length of the section
* @param factory the NIO [ReadableByteChannel] factory
*/
@Blocking
@JvmOverloads
@JvmName("forByteChannelFactory")
public fun ByteChannelFactoryRadAccessor(
fullSize: Long,
offset: Long = 0L,
size: Long = fullSize - offset,
maxPoolSize: Int = DEFAULT_MAX_POOL_SIZE,
factory: () -> ReadableByteChannel,
): RandomAccessData {
checkOffsetAndCount(fullSize, offset, size)
return StreamFactoryRad(
factory = ByteChannelFactory(size, factory),
offset = offset,
size = size,
maxPoolSize = maxPoolSize,
)
}