Add methods to read/write from Path to DataBufferUtils

This commit adds two methods to DataBufferUtils:
- one that reads a Flux<DataBuffer> from a Path
- one that writes a Flux<DataBuffer> to a Path
This commit is contained in:
Arjen Poutsma
2019-06-14 10:23:25 +02:00
parent 99d10a671c
commit f08656c6cb
2 changed files with 106 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import io.netty.buffer.ByteBuf;
@@ -202,6 +203,13 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
subscriber.cancel();
}
@Test
public void readPath() throws IOException {
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource.getFile().toPath(), this.bufferFactory, 3);
verifyReadData(flux);
}
@Test
public void readResource() throws Exception {
Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, this.bufferFactory, 3);
@@ -474,6 +482,21 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
flux.subscribe(DataBufferUtils::release);
}
@Test
public void writePath() throws IOException {
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
Flux<DataBuffer> flux = Flux.just(foo, bar);
Mono<Void> result = DataBufferUtils.write(flux, tempFile);
StepVerifier.create(result)
.verifyComplete();
List<String> written = Files.readAllLines(tempFile);
assertThat(written).contains("foobar");
}
@Test
public void readAndWriteByteChannel() throws Exception {
Path source = Paths.get(