Skip to content

Commit 09d8477

Browse files
committed
test: complete and fix tests, document changes
Signed-off-by: Art Shendrik <artyom.shendrik@gmail.com>
1 parent e221450 commit 09d8477

9 files changed

+43
-43
lines changed

ROADMAP.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<details>
66
<summary>Show</summary>
77

8-
* Port tests.
98
* Set up code coverage.
109
* Port benchmarks.
1110
* Set up multiplatform benchmarks.

fluxo-io-rad/src/commonJvmMain/kotlin/fluxo/io/rad/ByteBufferRadAccessor.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fluxo.io.rad
66

77
import fluxo.io.internal.Blocking
8+
import fluxo.io.util.checkOffsetAndCount
89
import fluxo.io.util.toIntChecked
910
import java.io.File
1011
import java.io.FileDescriptor
@@ -50,13 +51,15 @@ public fun RadByteBufferAccessor(
5051
): RandomAccessData = ByteBufferRad(data, offset = offset, size = size)
5152

5253

53-
private fun byteBufferRad0(
54+
private fun byteBufferMmapRad0(
5455
channel: FileChannel,
5556
offset: Long,
5657
size: Int,
5758
vararg resources: AutoCloseable,
5859
): RandomAccessData {
59-
val size0 = if (size == -1) (channel.size() - offset).toIntChecked() else size
60+
val dataLength = channel.size()
61+
val size0 = if (size == -1) (dataLength - offset).toIntChecked() else size
62+
checkOffsetAndCount(dataLength, offset, size0.toLong())
6063
val byteBuffer = channel.map(MapMode.READ_ONLY, offset, size0.toLong())
6164
return ByteBufferRad(byteBuffer, offset = 0, size = size0, resources = resources)
6265
}
@@ -75,7 +78,7 @@ private fun byteBufferRad0(
7578
*
7679
* @param data the underlying [FileInputStream]
7780
* @param offset the optional offset of the section
78-
* @param size the optional length of the section
81+
* @param size the optional length of the section. -1 means the rest of the file.
7982
*
8083
* @see java.nio.channels.FileChannel.map
8184
*/
@@ -88,7 +91,7 @@ public fun RadByteBufferAccessor(
8891
size: Int = -1,
8992
): RandomAccessData {
9093
val channel = data.channel
91-
return byteBufferRad0(channel, offset = offset, size = size, channel, data)
94+
return byteBufferMmapRad0(channel, offset = offset, size = size, channel, data)
9295
}
9396

9497
/**
@@ -105,7 +108,7 @@ public fun RadByteBufferAccessor(
105108
*
106109
* @param data the underlying [FileChannel]
107110
* @param offset the offset of the section
108-
* @param size the length of the section
111+
* @param size the optional length of the section. -1 means the rest of the file.
109112
*
110113
* @see java.nio.channels.FileChannel.map
111114
*/
@@ -116,7 +119,7 @@ public fun RadByteBufferAccessor(
116119
data: FileChannel,
117120
offset: Long = 0L,
118121
size: Int = -1,
119-
): RandomAccessData = byteBufferRad0(data, offset = offset, size = size, data)
122+
): RandomAccessData = byteBufferMmapRad0(data, offset = offset, size = size, data)
120123

121124
/**
122125
* Creates a new memory-mapping [ByteBuffer]-based [RandomAccessData] instance
@@ -132,9 +135,7 @@ public fun RadByteBufferAccessor(
132135
*
133136
* @param data the underlying [File]
134137
* @param offset the offset of the section
135-
* @param size the length of the section
136-
*
137-
* @TODO: Would it be better to use [FileChannel.size] instead of [File.length]?
138+
* @param size the optional length of the section. -1 means the rest of the file.
138139
*
139140
* @see java.nio.channels.FileChannel.map
140141
*/
@@ -144,11 +145,11 @@ public fun RadByteBufferAccessor(
144145
public fun RadByteBufferAccessor(
145146
data: File,
146147
offset: Long = 0L,
147-
size: Int = (data.length() - offset).toIntChecked(),
148+
size: Int = -1,
148149
): RandomAccessData {
149150
val stream = FileInputStream(data)
150151
val channel = stream.channel
151-
return byteBufferRad0(channel, offset = offset, size = size, channel, stream)
152+
return byteBufferMmapRad0(channel, offset = offset, size = size, channel, stream)
152153
}
153154

154155
/**
@@ -165,7 +166,7 @@ public fun RadByteBufferAccessor(
165166
*
166167
* @param data the underlying [FileDescriptor]
167168
* @param offset the offset of the section
168-
* @param size the length of the section
169+
* @param size the optional length of the section. -1 means the rest of the file.
169170
*
170171
* @see java.nio.channels.FileChannel.map
171172
*/
@@ -179,7 +180,7 @@ public fun RadByteBufferAccessor(
179180
): RandomAccessData {
180181
val stream = FileInputStream(data)
181182
val channel = stream.channel
182-
return byteBufferRad0(channel, offset = offset, size = size, channel, stream)
183+
return byteBufferMmapRad0(channel, offset = offset, size = size, channel, stream)
183184
}
184185

185186

fluxo-io-rad/src/commonJvmMain/kotlin/fluxo/io/rad/FileChannelRadAccessor.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.nio.channels.FileChannel
1919
*
2020
* @param data the underlying [FileChannel]
2121
* @param offset the offset of the section
22-
* @param size the length of the section
22+
* @param size the optional length of the section. -1 means the rest of the file.
2323
* @param resources the optional resources to close when finished
2424
*/
2525
@Blocking
@@ -44,7 +44,7 @@ public fun RadFileChannelAccessor(
4444
*
4545
* @param data the underlying [FileInputStream]
4646
* @param offset the optional offset of the section
47-
* @param size the optional length of the section
47+
* @param size the optional length of the section. -1 means the rest of the file.
4848
*/
4949
@Blocking
5050
@JvmOverloads
@@ -67,17 +67,15 @@ public fun RadFileChannelAccessor(
6767
*
6868
* @param data the underlying [File]
6969
* @param offset the optional offset of the section
70-
* @param size the optional length of the section
71-
*
72-
* @TODO: Would it be better to use [FileChannel.size] instead of [File.length]?
70+
* @param size the optional length of the section. -1 means the rest of the file.
7371
*/
7472
@Blocking
7573
@JvmOverloads
7674
@JvmName("forFileChannel")
7775
public fun RadFileChannelAccessor(
7876
data: File,
7977
offset: Long = 0L,
80-
size: Long = data.length() - offset,
78+
size: Long = -1L,
8179
): RandomAccessData = RadFileChannelAccessor(FileInputStream(data), offset = offset, size = size)
8280

8381
/**
@@ -89,7 +87,7 @@ public fun RadFileChannelAccessor(
8987
*
9088
* @param data the underlying [FileDescriptor]
9189
* @param offset the optional offset of the section
92-
* @param size the optional length of the section
90+
* @param size the optional length of the section. -1 means the rest of the file.
9391
*/
9492
@Blocking
9593
@JvmOverloads

fluxo-io-rad/src/commonJvmMain/kotlin/fluxo/io/rad/RandomAccessFileRadAccessor.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fluxo.io.rad
66

77
import fluxo.io.internal.Blocking
8+
import fluxo.io.util.checkOffsetAndCount
89
import java.io.File
910
import java.io.FileNotFoundException
1011
import java.io.RandomAccessFile
@@ -21,7 +22,7 @@ import java.io.RandomAccessFile
2122
*
2223
* @param data the underlying [RandomAccessFile]
2324
* @param offset the offset of the section
24-
* @param size the length of the section
25+
* @param size the optional length of the section. -1 means the rest of the file.
2526
*/
2627
@Blocking
2728
@JvmOverloads
@@ -31,7 +32,9 @@ public fun RandomAccessFileRadAccessor(
3132
offset: Long = 0L,
3233
size: Long = -1L,
3334
): RandomAccessData {
34-
val size0 = if (size == -1L) data.length() - offset else size
35+
val dataLength = data.length()
36+
val size0 = if (size == -1L) dataLength - offset else size
37+
checkOffsetAndCount(dataLength, offset, size0)
3538
return RandomAccessFileRad(data, offset = offset, size = size0)
3639
}
3740

@@ -47,9 +50,7 @@ public fun RandomAccessFileRadAccessor(
4750
*
4851
* @param data the underlying [File]
4952
* @param offset the optional offset of the section
50-
* @param size the optional length of the section
51-
*
52-
* @TODO: Would it be better to use [RandomAccessFile.length] instead of [File.length]?
53+
* @param size the optional length of the section. -1 means the rest of the file.
5354
*
5455
* @throws IllegalArgumentException if the file doesn't exist
5556
*/
@@ -60,6 +61,6 @@ public fun RandomAccessFileRadAccessor(
6061
public fun RandomAccessFileRadAccessor(
6162
data: File,
6263
offset: Long = 0L,
63-
size: Long = data.length() - offset,
64+
size: Long = -1L,
6465
): RandomAccessData =
6566
RandomAccessFileRadAccessor(RandomAccessFile(data, "r"), offset = offset, size = size)

fluxo-io-rad/src/commonJvmMain/kotlin/fluxo/io/rad/SeekableByteChannelRadAccessor.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fluxo.io.rad
66

77
import fluxo.io.internal.Blocking
8+
import fluxo.io.util.checkOffsetAndCount
89
import java.io.File
910
import java.io.FileDescriptor
1011
import java.io.FileInputStream
@@ -22,7 +23,7 @@ import java.nio.channels.SeekableByteChannel
2223
*
2324
* @param data the underlying [SeekableByteChannel]
2425
* @param offset the offset of the section
25-
* @param size the length of the section
26+
* @param size the optional length of the section. -1 means the rest of the file.
2627
* @param resources the optional resources to close when finished
2728
*
2829
* @see java.nio.channels.FileChannel
@@ -37,7 +38,9 @@ public fun RadSeekableByteChannelAccessor(
3738
size: Long = -1L,
3839
vararg resources: AutoCloseable = arrayOf(data),
3940
): RandomAccessData {
40-
val size0 = if (size == -1L) data.size() - offset else size
41+
val dataLength = data.size()
42+
val size0 = if (size == -1L) dataLength - offset else size
43+
checkOffsetAndCount(dataLength, offset, size0)
4144
return SeekableByteChannelRad(data, offset = offset, size = size0, resources = resources)
4245
}
4346

@@ -53,7 +56,7 @@ public fun RadSeekableByteChannelAccessor(
5356
*
5457
* @param data the underlying [FileInputStream]
5558
* @param offset the optional offset of the section
56-
* @param size the optional length of the section
59+
* @param size the optional length of the section. -1 means the rest of the file.
5760
*/
5861
@Blocking
5962
@JvmOverloads
@@ -79,17 +82,15 @@ public fun RadSeekableByteChannelAccessor(
7982
*
8083
* @param data the underlying [File]
8184
* @param offset the optional offset of the section
82-
* @param size the optional length of the section
83-
*
84-
* @TODO: Would it be better to use [SeekableByteChannel.size] instead of [File.length]?
85+
* @param size the optional length of the section. -1 means the rest of the file.
8586
*/
8687
@Blocking
8788
@JvmOverloads
8889
@JvmName("forSeekableByteChannel")
8990
public fun RadSeekableByteChannelAccessor(
9091
data: File,
9192
offset: Long = 0L,
92-
size: Long = data.length() - offset,
93+
size: Long = -1L,
9394
): RandomAccessData =
9495
RadSeekableByteChannelAccessor(FileInputStream(data), offset = offset, size = size)
9596

@@ -105,7 +106,7 @@ public fun RadSeekableByteChannelAccessor(
105106
*
106107
* @param data the underlying [FileDescriptor]
107108
* @param offset the optional offset of the section
108-
* @param size the optional length of the section
109+
* @param size the optional length of the section. -1 means the rest of the file.
109110
*/
110111
@Blocking
111112
@JvmOverloads

fluxo-io-rad/src/jvmTest/kotlin/fluxo/io/rad/RandomAccessDataByteBufferTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ internal class RadByteBufferAccessorTest(
5858

5959
assertIOB { RadByteBufferAccessor(tempFile, -1, 0) }
6060
assertIOB { RadByteBufferAccessor(tempFile, -1, 1) }
61-
assertIOB { RadByteBufferAccessor(tempFile, 0, -1) }
62-
assertIOB { RadByteBufferAccessor(tempFile, 1, -1) }
61+
assertIOB { RadByteBufferAccessor(tempFile, 0, -2) }
62+
assertIOB { RadByteBufferAccessor(tempFile, 1, -2) }
6363
assertIOB { RadByteBufferAccessor(tempFile, BYTES.size + 1L) }
6464
assertIOB { RadByteBufferAccessor(tempFile, BYTES.size.toLong(), 1) }
6565
assertIOB { RadByteBufferAccessor(tempFile, BYTES.size + 1L, 1) }

fluxo-io-rad/src/jvmTest/kotlin/fluxo/io/rad/RandomAccessDataFileChannelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ internal class RandomAccessDataFileChannelTest(
4848

4949
assertIOB { RadFileChannelAccessor(tempFile, -1, 0) }
5050
assertIOB { RadFileChannelAccessor(tempFile, -1, 1) }
51-
assertIOB { RadFileChannelAccessor(tempFile, 0, -1) }
52-
assertIOB { RadFileChannelAccessor(tempFile, 1, -1) }
51+
assertIOB { RadFileChannelAccessor(tempFile, 0, -2) }
52+
assertIOB { RadFileChannelAccessor(tempFile, 1, -2) }
5353
assertIOB { RadFileChannelAccessor(tempFile, BYTES.size + 1L) }
5454
assertIOB { RadFileChannelAccessor(tempFile, BYTES.size.toLong(), 1L) }
5555
assertIOB { RadFileChannelAccessor(tempFile, BYTES.size + 1L, 1L) }

fluxo-io-rad/src/jvmTest/kotlin/fluxo/io/rad/RandomAccessDataRafTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ internal class RandomAccessDataRafTest(
4444

4545
assertIOB { RandomAccessFileRadAccessor(tempFile, -1, 0) }
4646
assertIOB { RandomAccessFileRadAccessor(tempFile, -1, 1) }
47-
assertIOB { RandomAccessFileRadAccessor(tempFile, 0, -1) }
48-
assertIOB { RandomAccessFileRadAccessor(tempFile, 1, -1) }
47+
assertIOB { RandomAccessFileRadAccessor(tempFile, 0, -2) }
48+
assertIOB { RandomAccessFileRadAccessor(tempFile, 1, -2) }
4949
assertIOB { RandomAccessFileRadAccessor(tempFile, BYTES.size + 1L) }
5050
assertIOB { RandomAccessFileRadAccessor(tempFile, BYTES.size.toLong(), 1L) }
5151
assertIOB { RandomAccessFileRadAccessor(tempFile, BYTES.size + 1L, 1L) }

fluxo-io-rad/src/jvmTest/kotlin/fluxo/io/rad/RandomAccessDataSeekByteChannelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ internal class RadSeekableByteChannelAccessorTest(
5151

5252
assertIOB { RadSeekableByteChannelAccessor(tempFile, -1, 0) }
5353
assertIOB { RadSeekableByteChannelAccessor(tempFile, -1, 1) }
54-
assertIOB { RadSeekableByteChannelAccessor(tempFile, 0, -1) }
55-
assertIOB { RadSeekableByteChannelAccessor(tempFile, 1, -1) }
54+
assertIOB { RadSeekableByteChannelAccessor(tempFile, 0, -2) }
55+
assertIOB { RadSeekableByteChannelAccessor(tempFile, 1, -2) }
5656
assertIOB { RadSeekableByteChannelAccessor(tempFile, BYTES.size + 1L) }
5757
assertIOB { RadSeekableByteChannelAccessor(tempFile, BYTES.size.toLong(), 1L) }
5858
assertIOB { RadSeekableByteChannelAccessor(tempFile, BYTES.size + 1L, 1L) }

0 commit comments

Comments
 (0)