Search in sources :

Example 11 with DataBuffer

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));
}
Also used : Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) Flux(reactor.core.publisher.Flux) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Map(java.util.Map) Optional(java.util.Optional) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Encoder(org.springframework.core.codec.Encoder) Assert(org.springframework.util.Assert) Encoder(org.springframework.core.codec.Encoder) CodecException(org.springframework.core.codec.CodecException) ResolvableType(org.springframework.core.ResolvableType)

Example 12 with DataBuffer

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();
}
Also used : SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) CodecException(org.springframework.core.codec.CodecException) MimeType(org.springframework.util.MimeType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Map(java.util.Map) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) JavaType(com.fasterxml.jackson.databind.JavaType) ResolvableType(org.springframework.core.ResolvableType) Encoder(org.springframework.core.codec.Encoder) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) DataBuffer(org.springframework.core.io.buffer.DataBuffer) APPLICATION_STREAM_JSON(org.springframework.http.MediaType.APPLICATION_STREAM_JSON) Flux(reactor.core.publisher.Flux) List(java.util.List) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter) Assert(org.springframework.util.Assert) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 13 with DataBuffer

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);
    }
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) File(java.io.File) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 14 with DataBuffer

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);
}
Also used : ArrayList(java.util.ArrayList) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 15 with DataBuffer

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();
}
Also used : Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) AbstractDataBufferAllocatingTestCase(org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase) Assert.assertFalse(org.junit.Assert.assertFalse) Publisher(org.reactivestreams.Publisher) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ResolvableType(org.springframework.core.ResolvableType) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Collections(java.util.Collections) Publisher(org.reactivestreams.Publisher) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)30 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23