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);
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations