Browse Source

Additional shortcut with charset in WebSocketMessage

pull/1704/head
Rossen Stoyanchev 8 years ago
parent
commit
9c55dd5961
  1. 20
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java
  2. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketSession.java

20
spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@
*/ */
package org.springframework.web.reactive.socket; package org.springframework.web.reactive.socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
@ -67,13 +68,24 @@ public class WebSocketMessage {
} }
/** /**
* Return the message payload as UTF-8 text. This is a useful for text * A variant of {@link #getPayloadAsText(Charset)} that uses {@code UTF-8}
* WebSocket messages. * for decoding the raw content to text.
*/ */
public String getPayloadAsText() { public String getPayloadAsText() {
return getPayloadAsText(StandardCharsets.UTF_8);
}
/**
* A shortcut for decoding the raw content of the message to text with the
* given character encoding. This is useful for text WebSocket messages, or
* otherwise when the payload is expected to contain text.
* @param charset the character encoding
* @since 5.0.5
*/
public String getPayloadAsText(Charset charset) {
byte[] bytes = new byte[this.payload.readableByteCount()]; byte[] bytes = new byte[this.payload.readableByteCount()];
this.payload.read(bytes); this.payload.read(bytes);
return new String(bytes, StandardCharsets.UTF_8); return new String(bytes, charset);
} }
/** /**

2
spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketSession.java

@ -57,7 +57,7 @@ public interface WebSocketSession {
DataBufferFactory bufferFactory(); DataBufferFactory bufferFactory();
/** /**
* Get the flux of incoming messages. * Get access to the stream of incoming messages.
*/ */
Flux<WebSocketMessage> receive(); Flux<WebSocketMessage> receive();

Loading…
Cancel
Save