@@ -34,6 +34,13 @@ private void setCodeAndMessage( int code, String m ) throws InvalidDataException
3434 if ( m == null ) {
3535 m = "" ;
3636 }
37+ if ( code == CloseFrame .NOCODE ) {
38+ if ( !m .isEmpty () ) {
39+ throw new InvalidDataException ( PROTOCOL_ERROR , "A close frame must have a closecode if it has a reason" );
40+ }
41+ return ;// empty payload
42+ }
43+
3744 byte [] by = Charsetfunctions .utf8Bytes ( m );
3845 ByteBuffer buf = ByteBuffer .allocate ( 4 );
3946 buf .putInt ( code );
@@ -47,7 +54,7 @@ private void setCodeAndMessage( int code, String m ) throws InvalidDataException
4754
4855 private void initCloseCode () throws InvalidFrameException {
4956 code = CloseFrame .NOCODE ;
50- ByteBuffer payload = getPayloadData ();
57+ ByteBuffer payload = super . getPayloadData ();
5158 payload .mark ();
5259 if ( payload .remaining () >= 2 ) {
5360 ByteBuffer bb = ByteBuffer .allocate ( 4 );
@@ -71,18 +78,18 @@ public int getCloseCode() {
7178
7279 private void initMessage () throws InvalidDataException {
7380 if ( code == CloseFrame .NOCODE ) {
74- reason = Charsetfunctions .stringUtf8 ( getPayloadData () );
81+ reason = Charsetfunctions .stringUtf8 ( super . getPayloadData () );
7582 } else {
76- ByteBuffer b = getPayloadData ();
77- b . mark ();
83+ ByteBuffer b = super . getPayloadData ();
84+ int mark = b . position ();// because stringUtf8 also creates a mark
7885 try {
7986 b .position ( b .position () + 2 );
87+ reason = Charsetfunctions .stringUtf8 ( b );
8088 } catch ( IllegalArgumentException e ) {
8189 throw new InvalidFrameException ( e );
8290 } finally {
83- b .reset ( );
91+ b .position ( mark );
8492 }
85- reason = Charsetfunctions .stringUtf8 ( getPayloadData () );
8693 }
8794 }
8895
0 commit comments