forked from TooTallNate/Java-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFramedataImpl1.java
More file actions
136 lines (114 loc) · 3.17 KB
/
FramedataImpl1.java
File metadata and controls
136 lines (114 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.java_websocket.framing;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.java_websocket.exceptions.InvalidDataException;
import org.java_websocket.exceptions.InvalidFrameException;
import org.java_websocket.util.Charsetfunctions;
public class FramedataImpl1 implements FrameBuilder {
/**
* Attribute for just an empty array
*/
private static byte[] emptyarray = {};
/**
* Indicates that this is the final fragment in a message.
*/
protected boolean fin;
/**
* Defines the interpretation of the "Payload data".
*/
protected Opcode optcode;
/**
* The unmasked "Payload data" which was sent in this frame
*/
private ByteBuffer unmaskedpayload;
/**
* Defines whether the "Payload data" is masked.
*/
protected boolean transferemasked;
/**
* Constructor for a FramedataImpl without any attributes set
*/
public FramedataImpl1() {
}
/**
* Constructor for a FramedataImpl without any attributes set apart from the opcode
* @param op the opcode to use
*/
public FramedataImpl1( Opcode op ) {
this.optcode = op;
unmaskedpayload = ByteBuffer.wrap( emptyarray );
}
/**
* Helper constructor which helps to create "echo" frames.
* The new object will use the same underlying payload data.
* @param f The Framedata to copy data from
*/
public FramedataImpl1( Framedata f ) {
fin = f.isFin();
optcode = f.getOpcode();
unmaskedpayload = f.getPayloadData();
transferemasked = f.getTransfereMasked();
}
@Override
public boolean isFin() {
return fin;
}
@Override
public Opcode getOpcode() {
return optcode;
}
@Override
public boolean getTransfereMasked() {
return transferemasked;
}
@Override
public ByteBuffer getPayloadData() {
return unmaskedpayload;
}
@Override
public void setFin( boolean fin ) {
this.fin = fin;
}
@Override
public void setOptcode( Opcode optcode ) {
this.optcode = optcode;
}
@Override
public void setPayload( ByteBuffer payload ) throws InvalidDataException {
unmaskedpayload = payload;
}
@Override
public void setTransferemasked( boolean transferemasked ) {
this.transferemasked = transferemasked;
}
@Override
public void append( Framedata nextframe ) {
ByteBuffer b = nextframe.getPayloadData();
if( unmaskedpayload == null ) {
unmaskedpayload = ByteBuffer.allocate( b.remaining() );
b.mark();
unmaskedpayload.put( b );
b.reset();
} else {
b.mark();
unmaskedpayload.position( unmaskedpayload.limit() );
unmaskedpayload.limit( unmaskedpayload.capacity() );
if( b.remaining() > unmaskedpayload.remaining() ) {
ByteBuffer tmp = ByteBuffer.allocate( b.remaining() + unmaskedpayload.capacity() );
unmaskedpayload.flip();
tmp.put( unmaskedpayload );
tmp.put( b );
unmaskedpayload = tmp;
} else {
unmaskedpayload.put( b );
}
unmaskedpayload.rewind();
b.reset();
}
fin = nextframe.isFin();
}
@Override
public String toString() {
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", payloadlength:[pos:" + unmaskedpayload.position() + ", len:" + unmaskedpayload.remaining() + "], payload:" + Arrays.toString( Charsetfunctions.utf8Bytes( new String( unmaskedpayload.array() ) ) ) + "}";
}
}