Skip to content

Commit ff2e9aa

Browse files
committed
Minor tweaks to WebSocketMessage
- payload is now required - byte data is obtained and stored only once (allowing multiple calls) - minor formatting polish
1 parent a14161f commit ff2e9aa

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

spring-websocket/src/main/java/org/springframework/websocket/BinaryMessage.java

+25-13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.InputStream;
2020
import java.nio.ByteBuffer;
2121

22+
import org.springframework.util.Assert;
23+
2224

2325
/**
2426
* Represents a binary WebSocket message.
@@ -28,7 +30,7 @@
2830
*/
2931
public final class BinaryMessage extends WebSocketMessage<ByteBuffer> {
3032

31-
private final byte[] bytes;
33+
private byte[] bytes;
3234

3335
private final boolean last;
3436

@@ -48,8 +50,19 @@ public BinaryMessage(byte[] payload) {
4850
}
4951

5052
public BinaryMessage(byte[] payload, boolean isLast) {
51-
super((payload != null) ? ByteBuffer.wrap(payload) : null);
52-
this.bytes = payload;
53+
this(payload, 0, (payload == null ? 0 : payload.length), isLast);
54+
}
55+
56+
public BinaryMessage(byte[] payload, int offset, int len) {
57+
this(payload, offset, len, true);
58+
}
59+
60+
public BinaryMessage(byte[] payload, int offset, int len, boolean isLast) {
61+
super(payload != null ? ByteBuffer.wrap(payload, offset, len) : null);
62+
if(payload != null && offset == 0 && len == payload.length) {
63+
// FIXME better if a message always needs a payload?
64+
this.bytes = payload;
65+
}
5366
this.last = isLast;
5467
}
5568

@@ -58,17 +71,16 @@ public boolean isLast() {
5871
}
5972

6073
public byte[] getByteArray() {
61-
if (this.bytes != null) {
62-
return this.bytes;
63-
}
64-
else if (getPayload() != null){
65-
byte[] result = new byte[getPayload().remaining()];
66-
getPayload().get(result);
67-
return result;
68-
}
69-
else {
70-
return null;
74+
if(this.bytes == null && getPayload() != null) {
75+
this.bytes = getRemainingBytes(getPayload());
7176
}
77+
return this.bytes;
78+
}
79+
80+
private byte[] getRemainingBytes(ByteBuffer payload) {
81+
byte[] result = new byte[getPayload().remaining()];
82+
getPayload().get(result);
83+
return result;
7284
}
7385

7486
public InputStream getInputStream() {

spring-websocket/src/main/java/org/springframework/websocket/TextMessage.java

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.Reader;
1919
import java.io.StringReader;
2020

21-
2221
/**
2322
* Represents a text WebSocket message.
2423
*
@@ -27,7 +26,6 @@
2726
*/
2827
public final class TextMessage extends WebSocketMessage<String> {
2928

30-
3129
public TextMessage(String payload) {
3230
super(payload);
3331
}

spring-websocket/src/main/java/org/springframework/websocket/WebSocketMessage.java

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.websocket;
1717

18+
import org.springframework.util.Assert;
1819
import org.springframework.util.ObjectUtils;
1920

2021

@@ -29,6 +30,7 @@ public abstract class WebSocketMessage<T> {
2930

3031

3132
WebSocketMessage(T payload) {
33+
Assert.notNull(payload, "Payload must not be null");
3234
this.payload = payload;
3335
}
3436

0 commit comments

Comments
 (0)