Skip to content

Commit

Permalink
chore: Use read readable byte channel instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Dec 25, 2022
1 parent 9497fe9 commit c92c230
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/main/java/io/github/nstdio/http/ext/SimpleStreamFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;

class SimpleStreamFactory implements StreamFactory {
private static void assertNotContains(OpenOption[] options, StandardOpenOption needle) {
Expand All @@ -52,4 +55,12 @@ public WritableByteChannel writable(Path path, OpenOption... options) throws IOE
public InputStream input(Path path, OpenOption... options) throws IOException {
return Files.newInputStream(path, options);
}

@Override
public ReadableByteChannel readable(Path path, OpenOption... options) throws IOException {
assertNotContains(options, WRITE);
assertNotContains(options, APPEND);

return Files.newByteChannel(path, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,60 @@
package io.github.nstdio.http.ext

import io.kotest.assertions.throwables.shouldThrowExactly
import io.kotest.matchers.shouldBe
import io.kotest.matchers.throwable.shouldHaveMessage
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.nio.ByteBuffer
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
import java.nio.file.StandardOpenOption.READ
import java.nio.file.StandardOpenOption.WRITE

class SimpleStreamFactoryTest {
private val anyPath = Path.of("any")

@TempDir
private lateinit var tempDir: Path

@Test
fun `Should not allow read option on write method`() {
//given
val factory = SimpleStreamFactory()

//when + then
shouldThrowExactly<IllegalArgumentException> {
factory.writable(Path.of("any"), READ, WRITE)
factory.writable(anyPath, READ, WRITE)
}.shouldHaveMessage("READ not allowed")
}

@ParameterizedTest
@ValueSource(strings = ["WRITE", "APPEND"])
fun `Should not allow write option on read method`(option: StandardOpenOption) {
//given
val factory = SimpleStreamFactory()

//when + then
shouldThrowExactly<IllegalArgumentException> {
factory.readable(anyPath, READ, option)
}.shouldHaveMessage("$option not allowed")
}

@Test
fun `Should create channel`() {
//given
val file = tempDir.resolve("temp")
Files.write(file, listOf("a"), StandardOpenOption.CREATE)

val factory = SimpleStreamFactory()

//when
val channel = factory.readable(file)

//then
channel.read(ByteBuffer.allocate(1)) shouldBe 1
}
}

0 comments on commit c92c230

Please sign in to comment.