Add write methods to DataBufferUtils

This commit adds an overloaded write method to `DataBufferUtils`. There
are three parameter variants: `OutputStream`, `WritableByteChannel`, and
`AsynchronousFileChannel`.

Issue: SPR-15726
This commit is contained in:
Arjen Poutsma
2017-07-14 12:07:35 +02:00
parent e37af83459
commit 83051b06b8
2 changed files with 249 additions and 14 deletions

View File

@@ -17,17 +17,23 @@
package org.springframework.core.io.buffer;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.stream.Collectors;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
@@ -112,6 +118,79 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
.verify();
}
@Test
public void writeOutputStream() throws Exception {
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
DataBuffer qux = stringBuffer("qux");
Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux);
Path tempFile = Files.createTempFile("DataBufferUtilsTests", null);
OutputStream os = Files.newOutputStream(tempFile);
Mono<Void> writeResult = DataBufferUtils.write(flux, os);
StepVerifier.create(writeResult)
.expectComplete()
.verify();
String result = Files.readAllLines(tempFile)
.stream()
.collect(Collectors.joining());
assertEquals("foobarbazqux", result);
os.close();
}
@Test
public void writeWritableByteChannel() throws Exception {
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
DataBuffer qux = stringBuffer("qux");
Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux);
Path tempFile = Files.createTempFile("DataBufferUtilsTests", null);
WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);
Mono<Void> writeResult = DataBufferUtils.write(flux, channel);
StepVerifier.create(writeResult)
.expectComplete()
.verify();
String result = Files.readAllLines(tempFile)
.stream()
.collect(Collectors.joining());
assertEquals("foobarbazqux", result);
channel.close();
}
@Test
public void writeAsynchronousFileChannel() throws Exception {
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
DataBuffer qux = stringBuffer("qux");
Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux);
Path tempFile = Files.createTempFile("DataBufferUtilsTests", null);
AsynchronousFileChannel channel =
AsynchronousFileChannel.open(tempFile, StandardOpenOption.WRITE);
Mono<Void> writeResult = DataBufferUtils.write(flux, channel, 0);
StepVerifier.create(writeResult)
.expectComplete()
.verify();
String result = Files.readAllLines(tempFile)
.stream()
.collect(Collectors.joining());
assertEquals("foobarbazqux", result);
channel.close();
}
@Test
public void takeUntilByteCount() throws Exception {
DataBuffer foo = stringBuffer("foo");