77import java .util .Random ;
88
99import org .java_websocket .exceptions .InvalidDataException ;
10+ import org .java_websocket .exceptions .InvalidFrameException ;
1011import org .java_websocket .exceptions .InvalidHandshakeException ;
12+ import org .java_websocket .exceptions .LimitExedeedException ;
1113import org .java_websocket .exceptions .NotSendableException ;
1214import org .java_websocket .framing .CloseFrame ;
1315import org .java_websocket .framing .FrameBuilder ;
@@ -40,14 +42,13 @@ public class Draft_75 extends Draft{
4042 */
4143public static final byte END_OF_FRAME = (byte ) 0xFF ;
4244
45+ /** Is only used to detect protocol violations */
4346protected boolean readingState = false ;
44- private boolean inframe = false ;
47+
4548protected List <Framedata > readyframes = new LinkedList <Framedata >();
4649protected ByteBuffer currentFrame ;
47-
48-
50+
4951private final Random reuseableRandom = new Random ();
50-
5152
5253@ Override
5354public HandshakeState acceptHandshakeAsClient ( ClientHandshake request , ServerHandshake response ){
@@ -122,29 +123,29 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( ClientHandshake re
122123 }
123124
124125protected List <Framedata > translateRegularFrame ( ByteBuffer buffer ) throws InvalidDataException {
126+
125127while ( buffer .hasRemaining () ){
126128byte newestByte = buffer .get ();
127129if ( newestByte == START_OF_FRAME ){// Beginning of Frame
128130if ( readingState )
129- return null ;
131+ throw new InvalidFrameException ( "unexpected START_OF_FRAME" ) ;
130132readingState = true ;
131133 } else if ( newestByte == END_OF_FRAME ){// End of Frame
132134if ( !readingState )
133- return null ;
135+ throw new InvalidFrameException ( "unexpected END_OF_FRAME" ) ;
134136// currentFrame will be null if END_OF_FRAME was send directly after
135137// START_OF_FRAME, thus we will send 'null' as the sent message.
136138if ( this .currentFrame != null ){
137139currentFrame .flip ();
138140FramedataImpl1 curframe = new FramedataImpl1 ();
139141curframe .setPayload ( currentFrame );
140142curframe .setFin ( true );
141- curframe .setOptcode ( inframe ? Opcode . CONTINUOUS : Opcode .TEXT );
143+ curframe .setOptcode ( Opcode .TEXT );
142144readyframes .add ( curframe );
143145this .currentFrame = null ;
144146buffer .mark ();
145147 }
146148readingState = false ;
147- inframe = false ;
148149 } else if ( readingState ){// Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
149150if ( currentFrame == null ){
150151currentFrame = createBuffer ();
@@ -156,15 +157,11 @@ protected List<Framedata> translateRegularFrame( ByteBuffer buffer ) throws Inva
156157return null ;
157158 }
158159 }
159- if ( readingState ){
160- FramedataImpl1 curframe = new FramedataImpl1 ();
161- currentFrame .flip ();
162- curframe .setPayload ( currentFrame );
163- curframe .setFin ( false );
164- curframe .setOptcode ( inframe ? Opcode .CONTINUOUS : Opcode .TEXT );
165- inframe = true ;
166- readyframes .add ( curframe );
167- }
160+
161+ // if no error occurred this block will be reached
162+ /*if( readingState ){
163+ checkAlloc(currentFrame.position()+1);
164+ }*/
168165
169166List <Framedata > frames = readyframes ;
170167readyframes = new LinkedList <Framedata >();
@@ -196,9 +193,9 @@ public ByteBuffer createBuffer(){
196193return ByteBuffer .allocate ( INITIAL_FAMESIZE );
197194 }
198195
199- public ByteBuffer increaseBuffer ( ByteBuffer full ){
196+ public ByteBuffer increaseBuffer ( ByteBuffer full ) throws LimitExedeedException , InvalidDataException {
200197full .flip ();
201- ByteBuffer newbuffer = ByteBuffer .allocate ( full .capacity () * 2 );
198+ ByteBuffer newbuffer = ByteBuffer .allocate ( checkAlloc ( full .capacity () * 2 ) );
202199newbuffer .put ( full );
203200return newbuffer ;
204201 }
0 commit comments