use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageWriter method applyEncoder.
@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> applyEncoder(Object data, DataBufferFactory bufferFactory, Map<String, Object> hints) {
ResolvableType elementType = ResolvableType.forClass(data.getClass());
Optional<Encoder<?>> encoder = dataEncoders.stream().filter(e -> e.canEncode(elementType, MimeTypeUtils.APPLICATION_JSON)).findFirst();
return ((Encoder<T>) encoder.orElseThrow(() -> new CodecException("No suitable encoder found!"))).encode(Mono.just((T) data), bufferFactory, elementType, MimeTypeUtils.APPLICATION_JSON, hints).concatWith(encodeString("\n", bufferFactory));
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonEncoder method encode.
@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Assert.notNull(inputStream, "'inputStream' must not be null");
Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
Assert.notNull(elementType, "'elementType' must not be null");
if (inputStream instanceof Mono) {
return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
} else if (APPLICATION_STREAM_JSON.isCompatibleWith(mimeType)) {
return Flux.from(inputStream).map(value -> {
DataBuffer buffer = encodeValue(value, bufferFactory, elementType, hints);
buffer.write(new byte[] { '\n' });
return buffer;
});
}
ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
return Flux.from(inputStream).collectList().map(list -> encodeValue(list, bufferFactory, listType, hints)).flux();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class ResourceRegionEncoder method readResourceRegion.
private Flux<DataBuffer> readResourceRegion(ResourceRegion region, DataBufferFactory bufferFactory) {
Resource resource = region.getResource();
try {
if (resource.isFile()) {
File file = region.getResource().getFile();
AsynchronousFileChannel channel = AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ);
return DataBufferUtils.read(channel, region.getPosition(), bufferFactory, this.bufferSize);
}
} catch (IOException ignore) {
// fallback to resource.readableChannel(), below
}
try {
ReadableByteChannel channel = resource.readableChannel();
Flux<DataBuffer> in = DataBufferUtils.read(channel, bufferFactory, this.bufferSize);
return DataBufferUtils.skipUntilByteCount(in, region.getPosition());
} catch (IOException ex) {
return Flux.error(ex);
}
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class StringDecoder method splitOnNewline.
private static Flux<DataBuffer> splitOnNewline(DataBuffer dataBuffer) {
List<DataBuffer> results = new ArrayList<>();
int startIdx = 0;
int endIdx;
final int limit = dataBuffer.readableByteCount();
do {
endIdx = dataBuffer.indexOf(NEWLINE_DELIMITER, startIdx);
int length = endIdx != -1 ? endIdx - startIdx + 1 : limit - startIdx;
DataBuffer token = dataBuffer.slice(startIdx, length);
results.add(DataBufferUtils.retain(token));
startIdx = endIdx + 1;
} while (startIdx < limit && endIdx != -1);
DataBufferUtils.release(dataBuffer);
return Flux.fromIterable(results);
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class ByteArrayDecoderTests method decode.
@Test
public void decode() {
DataBuffer fooBuffer = stringBuffer("foo");
DataBuffer barBuffer = stringBuffer("bar");
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Flux<byte[]> output = this.decoder.decode(source, ResolvableType.forClassWithGenerics(Publisher.class, byte[].class), null, Collections.emptyMap());
StepVerifier.create(output).consumeNextWith(bytes -> assertArrayEquals("foo".getBytes(), bytes)).consumeNextWith(bytes -> assertArrayEquals("bar".getBytes(), bytes)).expectComplete().verify();
}
Aggregations