Search in sources :

Example 6 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class MockClientHttpResponse method setBody.

public void setBody(String body, Charset charset) {
    DataBuffer buffer = toDataBuffer(body, charset);
    this.body = Flux.just(buffer);
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 7 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class StandardWebSocketHandlerAdapter method toMessage.

private <T> WebSocketMessage toMessage(T message) {
    WebSocketSession session = this.delegateSession;
    Assert.state(session != null, "Cannot create message without a session");
    if (message instanceof String) {
        byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
        return new WebSocketMessage(Type.TEXT, session.bufferFactory().wrap(bytes));
    } else if (message instanceof ByteBuffer) {
        DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
        return new WebSocketMessage(Type.BINARY, buffer);
    } else if (message instanceof PongMessage) {
        DataBuffer buffer = session.bufferFactory().wrap(((PongMessage) message).getApplicationData());
        return new WebSocketMessage(Type.PONG, buffer);
    } else {
        throw new IllegalArgumentException("Unexpected message type: " + message);
    }
}
Also used : WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) PongMessage(javax.websocket.PongMessage) ByteBuffer(java.nio.ByteBuffer) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 8 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class ResourceRegionHttpMessageWriter method writeResourceRegion.

private Mono<Void> writeResourceRegion(ResourceRegion region, ReactiveHttpOutputMessage outputMessage) {
    if (outputMessage instanceof ZeroCopyHttpOutputMessage) {
        Optional<File> file = getFile(region.getResource());
        if (file.isPresent()) {
            ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) outputMessage;
            return zeroCopyResponse.writeWith(file.get(), region.getPosition(), region.getCount());
        }
    }
    // non-zero copy fallback, using ResourceRegionEncoder
    DataBufferFactory bufferFactory = outputMessage.bufferFactory();
    MediaType contentType = outputMessage.getHeaders().getContentType();
    Map<String, Object> hints = Collections.emptyMap();
    Flux<DataBuffer> body = this.encoder.encode(Mono.just(region), bufferFactory, TYPE, contentType, hints);
    return outputMessage.writeWith(body);
}
Also used : ZeroCopyHttpOutputMessage(org.springframework.http.ZeroCopyHttpOutputMessage) MediaType(org.springframework.http.MediaType) File(java.io.File) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 9 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReader method decodeData.

private Object decodeData(String data, ResolvableType dataType, Map<String, Object> hints) {
    if (String.class.isAssignableFrom(dataType.getRawClass())) {
        return data.substring(0, data.length() - 1);
    }
    byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
    Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes));
    return this.dataDecoders.stream().filter(e -> e.canDecode(dataType, MimeTypeUtils.APPLICATION_JSON)).findFirst().orElseThrow(() -> new CodecException("No suitable decoder found!")).decodeToMono(input, dataType, MimeTypeUtils.APPLICATION_JSON, hints).block(Duration.ZERO);
}
Also used : CodecException(org.springframework.core.codec.CodecException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 10 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageWriter method encodeString.

private Mono<DataBuffer> encodeString(String str, DataBufferFactory bufferFactory) {
    byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = bufferFactory.allocateBuffer(bytes.length).write(bytes);
    return Mono.just(buffer);
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer)

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