19
19
import java .io .InputStream ;
20
20
import java .nio .ByteBuffer ;
21
21
22
+ import org .springframework .util .Assert ;
23
+
22
24
23
25
/**
24
26
* Represents a binary WebSocket message.
28
30
*/
29
31
public final class BinaryMessage extends WebSocketMessage <ByteBuffer > {
30
32
31
- private final byte [] bytes ;
33
+ private byte [] bytes ;
32
34
33
35
private final boolean last ;
34
36
@@ -48,8 +50,19 @@ public BinaryMessage(byte[] payload) {
48
50
}
49
51
50
52
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
+ }
53
66
this .last = isLast ;
54
67
}
55
68
@@ -58,17 +71,16 @@ public boolean isLast() {
58
71
}
59
72
60
73
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 ());
71
76
}
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 ;
72
84
}
73
85
74
86
public InputStream getInputStream () {
0 commit comments