33import java .nio .ByteBuffer ;
44import java .nio .charset .CharacterCodingException ;
55
6+ import net .tootallnate .websocket .exeptions .InvalidDataException ;
7+ import net .tootallnate .websocket .exeptions .InvalidFrameException ;
8+
69public class CloseFrameBuilder extends FramedataImpl1 implements CloseFrame {
710
11+ private int code ;
12+ private String reason ;
13+
814 public CloseFrameBuilder () {
915 super ( Opcode .CLOSING );
1016 setFin ( true );
1117 }
1218
13- public CloseFrameBuilder ( int code ) {
19+ public CloseFrameBuilder ( int code ) throws InvalidDataException {
1420 super ( Opcode .CLOSING );
1521 setFin ( true );
1622 setCodeAndMessage ( code , "" );
1723 }
1824
19- public CloseFrameBuilder ( int code , String m ) {
25+ public CloseFrameBuilder ( int code , String m ) throws InvalidDataException {
2026 super ( Opcode .CLOSING );
2127 setFin ( true );
2228 setCodeAndMessage ( code , m );
2329 }
2430
25- public void setCloseCode ( int code ) {
26- try {
27- setCodeAndMessage ( code , getMessage () );
28- } catch ( CharacterCodingException e ) {
29- assert ( false );
30- // not expected
31- throw new RuntimeException ( e );
32- }
33- }
34-
35- /** This operation changes the payload */
36- public void setMessage ( String message ) {
37- setCodeAndMessage ( getCloseCode (), message );
38- }
39-
40- public void setCodeAndMessage ( int code , String m ) {
31+ private void setCodeAndMessage ( int code , String m ) throws InvalidDataException {
4132 byte [] by = Charsetfunctions .utf8Bytes ( m );
4233 ByteBuffer buf = ByteBuffer .allocate ( 4 );
4334 buf .putInt ( code );
@@ -48,39 +39,60 @@ public void setCodeAndMessage( int code, String m ) {
4839 setPayload ( pay .array () );
4940 }
5041
51- @ Override
52- public int getCloseCode () {
53- int code = CloseFrame . NOCODE ;
54- if ( unmaskedpayload . array () .length >= 2 ) {
42+ private void initCloseCode () throws InvalidFrameException {
43+ code = CloseFrame . NOCODE ;
44+ byte [] payload = getPayloadData () ;
45+ if ( payload .length >= 2 ) {
5546 ByteBuffer bb = ByteBuffer .allocate ( 4 );
5647 bb .position ( 2 );
57- bb .put ( unmaskedpayload . array () , 0 , 2 );
48+ bb .put ( payload , 0 , 2 );
5849 bb .position ( 0 );
5950 code = bb .getInt ();
6051 if ( code < 0 || code > Short .MAX_VALUE )
6152 code = CloseFrame .NOCODE ;
53+ if ( code < CloseFrame .NORMAL || code > CloseFrame .EXTENSION || code == NOCODE || code == 1004 ) {
54+ throw new InvalidFrameException ( "bad code " + code );
55+ }
6256 }
63- return code ;
6457 }
6558
6659 @ Override
67- public String getMessage () throws CharacterCodingException {
68- if ( getCloseCode () == CloseFrame .NOCODE ) {
69- return Charsetfunctions .stringUtf8 ( unmaskedpayload .array () );
60+ public int getCloseCode () {
61+ return code ;
62+ }
63+
64+ private void initMessage () throws CharacterCodingException , InvalidFrameException {
65+ if ( code == CloseFrame .NOCODE ) {
66+ reason = Charsetfunctions .stringUtf8 ( getPayloadData () );
67+ } else {
68+ byte [] payload = getPayloadData ();
69+ reason = Charsetfunctions .stringUtf8 ( payload , 2 , payload .length - 2 );
7070 }
71- else {
72- return Charsetfunctions .stringUtf8 ( unmaskedpayload .array (),2 ,unmaskedpayload .array ().length -2 );
73- }
71+ }
72+
73+ @ Override
74+ public String getMessage () {
75+ return reason ;
7476 }
7577
7678 @ Override
7779 public String toString () {
78- return super .toString () + "code: " + getCloseCode () ;
80+ return super .toString () + "code: " + code ;
7981 }
8082
83+ @ Override
84+ public void setPayload ( byte [] payload ) throws InvalidDataException {
85+ super .setPayload ( payload );
86+ initCloseCode ();
87+ try {
88+ initMessage ();
89+ } catch ( CharacterCodingException e ) {
90+ throw new InvalidDataException ( CloseFrame .NO_UTF8 );
91+ }
92+ }
8193 @ Override
8294 public byte [] getPayloadData () {
83- if (getCloseCode ()== NOCODE )
95+ if ( code == NOCODE )
8496 return new byte [ 0 ];
8597 return super .getPayloadData ();
8698 }
0 commit comments