99import org .java_websocket .exeptions .InvalidDataException ;
1010import org .java_websocket .exeptions .InvalidHandshakeException ;
1111import org .java_websocket .exeptions .NotSendableException ;
12+ import org .java_websocket .framing .CloseFrame ;
1213import org .java_websocket .framing .FrameBuilder ;
1314import org .java_websocket .framing .Framedata ;
1415import org .java_websocket .framing .Framedata .Opcode ;
@@ -39,10 +40,10 @@ public class Draft_75 extends Draft {
3940 */
4041 public static final byte END_OF_FRAME = (byte ) 0xFF ;
4142
42- private boolean readingState = false ;
43+ protected boolean readingState = false ;
4344 private boolean inframe = false ;
44-
45- private ByteBuffer currentFrame ;
45+ protected List < Framedata > readyframes = new LinkedList < Framedata >();
46+ protected ByteBuffer currentFrame ;
4647
4748 @ Override
4849 public HandshakeState acceptHandshakeAsClient ( ClientHandshake request , ServerHandshake response ) {
@@ -59,6 +60,10 @@ public HandshakeState acceptHandshakeAsServer( ClientHandshake handshakedata ) {
5960
6061 @ Override
6162 public ByteBuffer createBinaryFrame ( Framedata framedata ) {
63+ if ( framedata .getOpcode () != Opcode .TEXT ) {
64+ throw new RuntimeException ( "only text frames supported" );
65+ }
66+
6267 byte [] pay = framedata .getPayloadData ();
6368 ByteBuffer b = ByteBuffer .allocate ( pay .length + 2 );
6469 b .put ( START_OF_FRAME );
@@ -91,10 +96,10 @@ public List<Framedata> createFrames( String text, boolean mask ) {
9196 public ClientHandshakeBuilder postProcessHandshakeRequestAsClient ( ClientHandshakeBuilder request ) throws InvalidHandshakeException {
9297 request .put ( "Upgrade" , "WebSocket" );
9398 request .put ( "Connection" , "Upgrade" );
94- if (!request .hasFieldValue ( "Origin" )) {
95- request .put ( "Origin" , "random" + new Random ().nextInt () );
99+ if ( !request .hasFieldValue ( "Origin" ) ) {
100+ request .put ( "Origin" , "random" + new Random ().nextInt () );
96101 }
97-
102+
98103 return request ;
99104 }
100105
@@ -110,34 +115,39 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( ClientHandshake re
110115 return response ;
111116 }
112117
113- @ Override
114- public List <Framedata > translateFrame ( ByteBuffer buffer ) throws InvalidDataException {
115- List <Framedata > frames = new LinkedList <Framedata >();
118+ protected List <Framedata > translateRegularFrame ( ByteBuffer buffer ) throws InvalidDataException {
116119 while ( buffer .hasRemaining () ) {
117120 byte newestByte = buffer .get ();
118- if ( newestByte == START_OF_FRAME && !readingState ) { // Beginning of Frame
119- this .currentFrame = null ;
121+ if ( newestByte == START_OF_FRAME ) { // Beginning of Frame
122+ if ( readingState )
123+ return null ;
120124 readingState = true ;
121- } else if ( newestByte == END_OF_FRAME && readingState ) { // End of Frame
125+ } else if ( newestByte == END_OF_FRAME ) { // End of Frame
126+ if ( !readingState )
127+ return null ;
122128 // currentFrame will be null if END_OF_FRAME was send directly after
123129 // START_OF_FRAME, thus we will send 'null' as the sent message.
124130 if ( this .currentFrame != null ) {
125131 FramedataImpl1 curframe = new FramedataImpl1 ();
126132 curframe .setPayload ( currentFrame .array () );
127133 curframe .setFin ( true );
128134 curframe .setOptcode ( inframe ? Opcode .CONTINIOUS : Opcode .TEXT );
129- frames .add ( curframe );
135+ readyframes .add ( curframe );
136+ this .currentFrame = null ;
137+ buffer .mark ();
130138 }
131139 readingState = false ;
132140 inframe = false ;
133- } else { // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
141+ } else if ( readingState ) { // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
134142 ByteBuffer frame = ByteBuffer .allocate ( checkAlloc ( ( this .currentFrame != null ? this .currentFrame .capacity () : 0 ) + 1 ) );
135143 if ( this .currentFrame != null ) {
136144 this .currentFrame .rewind ();
137145 frame .put ( this .currentFrame );
138146 }
139147 frame .put ( newestByte );
140148 this .currentFrame = frame ;
149+ } else {
150+ return null ;
141151 }
142152 }
143153 if ( readingState ) {
@@ -146,7 +156,20 @@ public List<Framedata> translateFrame( ByteBuffer buffer ) throws InvalidDataExc
146156 curframe .setFin ( false );
147157 curframe .setOptcode ( inframe ? Opcode .CONTINIOUS : Opcode .TEXT );
148158 inframe = true ;
149- frames .add ( curframe );
159+ readyframes .add ( curframe );
160+ }
161+
162+ List <Framedata > frames = readyframes ;
163+ readyframes = new LinkedList <Framedata >();
164+ this .currentFrame = null ;
165+ return frames ;
166+ }
167+
168+ @ Override
169+ public List <Framedata > translateFrame ( ByteBuffer buffer ) throws InvalidDataException {
170+ List <Framedata > frames = translateRegularFrame ( buffer );
171+ if ( frames == null ) {
172+ throw new InvalidDataException ( CloseFrame .PROTOCOL_ERROR );
150173 }
151174 return frames ;
152175 }
@@ -156,10 +179,10 @@ public void reset() {
156179 readingState = false ;
157180 this .currentFrame = null ;
158181 }
159-
182+
160183 @ Override
161- public boolean hasCloseHandshake () {
162- return false ;
184+ public CloseHandshakeType getCloseHandshakeType () {
185+ return CloseHandshakeType . NONE ;
163186 }
164187
165188}
0 commit comments