Skip to content

Commit c92c230

Browse files
committed
chore: Use read readable byte channel instead.
1 parent 9497fe9 commit c92c230

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/io/github/nstdio/http/ext/SimpleStreamFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
22+
import java.nio.channels.ReadableByteChannel;
2223
import java.nio.channels.WritableByteChannel;
2324
import java.nio.file.Files;
2425
import java.nio.file.OpenOption;
2526
import java.nio.file.Path;
2627
import java.nio.file.StandardOpenOption;
2728

29+
import static java.nio.file.StandardOpenOption.APPEND;
2830
import static java.nio.file.StandardOpenOption.READ;
31+
import static java.nio.file.StandardOpenOption.WRITE;
2932

3033
class SimpleStreamFactory implements StreamFactory {
3134
private static void assertNotContains(OpenOption[] options, StandardOpenOption needle) {
@@ -52,4 +55,12 @@ public WritableByteChannel writable(Path path, OpenOption... options) throws IOE
5255
public InputStream input(Path path, OpenOption... options) throws IOException {
5356
return Files.newInputStream(path, options);
5457
}
58+
59+
@Override
60+
public ReadableByteChannel readable(Path path, OpenOption... options) throws IOException {
61+
assertNotContains(options, WRITE);
62+
assertNotContains(options, APPEND);
63+
64+
return Files.newByteChannel(path, options);
65+
}
5566
}

src/test/kotlin/io/github/nstdio/http/ext/SimpleStreamFactoryTest.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,60 @@
1717
package io.github.nstdio.http.ext
1818

1919
import io.kotest.assertions.throwables.shouldThrowExactly
20+
import io.kotest.matchers.shouldBe
2021
import io.kotest.matchers.throwable.shouldHaveMessage
2122
import org.junit.jupiter.api.Test
23+
import org.junit.jupiter.api.io.TempDir
24+
import org.junit.jupiter.params.ParameterizedTest
25+
import org.junit.jupiter.params.provider.ValueSource
26+
import java.nio.ByteBuffer
27+
import java.nio.file.Files
2228
import java.nio.file.Path
29+
import java.nio.file.StandardOpenOption
2330
import java.nio.file.StandardOpenOption.READ
2431
import java.nio.file.StandardOpenOption.WRITE
2532

2633
class SimpleStreamFactoryTest {
34+
private val anyPath = Path.of("any")
35+
36+
@TempDir
37+
private lateinit var tempDir: Path
38+
2739
@Test
2840
fun `Should not allow read option on write method`() {
2941
//given
3042
val factory = SimpleStreamFactory()
3143

3244
//when + then
3345
shouldThrowExactly<IllegalArgumentException> {
34-
factory.writable(Path.of("any"), READ, WRITE)
46+
factory.writable(anyPath, READ, WRITE)
3547
}.shouldHaveMessage("READ not allowed")
3648
}
49+
50+
@ParameterizedTest
51+
@ValueSource(strings = ["WRITE", "APPEND"])
52+
fun `Should not allow write option on read method`(option: StandardOpenOption) {
53+
//given
54+
val factory = SimpleStreamFactory()
55+
56+
//when + then
57+
shouldThrowExactly<IllegalArgumentException> {
58+
factory.readable(anyPath, READ, option)
59+
}.shouldHaveMessage("$option not allowed")
60+
}
61+
62+
@Test
63+
fun `Should create channel`() {
64+
//given
65+
val file = tempDir.resolve("temp")
66+
Files.write(file, listOf("a"), StandardOpenOption.CREATE)
67+
68+
val factory = SimpleStreamFactory()
69+
70+
//when
71+
val channel = factory.readable(file)
72+
73+
//then
74+
channel.read(ByteBuffer.allocate(1)) shouldBe 1
75+
}
3776
}

0 commit comments

Comments
 (0)