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 */
4143 public static final byte END_OF_FRAME = (byte ) 0xFF ;
4244
45+ /** Is only used to detect protocol violations */
4346 protected boolean readingState = false ;
44- private boolean inframe = false ;
47+
4548 protected List <Framedata > readyframes = new LinkedList <Framedata >();
4649 protected ByteBuffer currentFrame ;
47-
48-
50+
4951 private final Random reuseableRandom = new Random ();
50-
5152
5253 @ Override
5354 public HandshakeState acceptHandshakeAsClient ( ClientHandshake request , ServerHandshake response ) {
@@ -122,29 +123,29 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( ClientHandshake re
122123 }
123124
124125 protected List <Framedata > translateRegularFrame ( ByteBuffer buffer ) throws InvalidDataException {
126+
125127 while ( buffer .hasRemaining () ) {
126128 byte newestByte = buffer .get ();
127129 if ( newestByte == START_OF_FRAME ) { // Beginning of Frame
128130 if ( readingState )
129- return null ;
131+ throw new InvalidFrameException ( "unexpected START_OF_FRAME" ) ;
130132 readingState = true ;
131133 } else if ( newestByte == END_OF_FRAME ) { // End of Frame
132134 if ( !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.
136138 if ( this .currentFrame != null ) {
137139 currentFrame .flip ();
138140 FramedataImpl1 curframe = new FramedataImpl1 ();
139141 curframe .setPayload ( currentFrame );
140142 curframe .setFin ( true );
141- curframe .setOptcode ( inframe ? Opcode . CONTINUOUS : Opcode .TEXT );
143+ curframe .setOptcode ( Opcode .TEXT );
142144 readyframes .add ( curframe );
143145 this .currentFrame = null ;
144146 buffer .mark ();
145147 }
146148 readingState = false ;
147- inframe = false ;
148149 } else if ( readingState ) { // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
149150 if ( currentFrame == null ) {
150151 currentFrame = createBuffer ();
@@ -156,15 +157,11 @@ protected List<Framedata> translateRegularFrame( ByteBuffer buffer ) throws Inva
156157 return 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
169166 List <Framedata > frames = readyframes ;
170167 readyframes = new LinkedList <Framedata >();
@@ -196,9 +193,9 @@ public ByteBuffer createBuffer() {
196193 return ByteBuffer .allocate ( INITIAL_FAMESIZE );
197194 }
198195
199- public ByteBuffer increaseBuffer ( ByteBuffer full ) {
196+ public ByteBuffer increaseBuffer ( ByteBuffer full ) throws LimitExedeedException , InvalidDataException {
200197 full .flip ();
201- ByteBuffer newbuffer = ByteBuffer .allocate ( full .capacity () * 2 );
198+ ByteBuffer newbuffer = ByteBuffer .allocate ( checkAlloc ( full .capacity () * 2 ) );
202199 newbuffer .put ( full );
203200 return newbuffer ;
204201 }
0 commit comments