Skip to content

Commit 0674c3f

Browse files
authored
Merge pull request TooTallNate#486 from marci4/master
ByteBuffer and JUnitTests
2 parents 64c1ef2 + b790f77 commit 0674c3f

File tree

9 files changed

+197
-288
lines changed

9 files changed

+197
-288
lines changed

‎src/main/java/org/java_websocket/WebSocketImpl.java‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@
3535
* text frames, and receiving frames through an event-based model.
3636
*/
3737
publicclassWebSocketImplimplementsWebSocket{
38-
39-
publicstaticfinalList<Draft> defaultdraftlist = newArrayList<Draft>( 1 );
4038
publicstaticintRCVBUF = 16384;
41-
publicstatic/*final*/booleanDEBUG = false; // must be final in the future in order to take advantage of VM optimization
4239

43-
static{
44-
defaultdraftlist.add( newDraft_6455() );
45-
}
40+
/**
41+
* Activate debug mode for additional infos
42+
*/
43+
publicstaticbooleanDEBUG = false; // must be final in the future in order to take advantage of VM optimization
4644

4745
/**
4846
* Queue of buffers that need to be sent to the client.
@@ -70,12 +68,25 @@ public class WebSocketImpl implements WebSocket{
7068
*/
7169
privatevolatilebooleanflushandclosestate = false;
7270
privateREADYSTATEreadystate = READYSTATE.NOT_YET_CONNECTED;
71+
72+
/**
73+
* A list of drafts available for this websocket
74+
*/
7375
privateList<Draft> knownDrafts;
7476

77+
/**
78+
* The draft which is used by this websocket
79+
*/
7580
privateDraftdraft = null;
7681

82+
/**
83+
* The role which this websocket takes in the connection
84+
*/
7785
privateRolerole;
7886

87+
/**
88+
* The frame which had the opcode Continous set
89+
*/
7990
privateFramedatacurrent_continuous_frame = null;
8091

8192
/**
@@ -110,7 +121,8 @@ public WebSocketImpl( WebSocketListener listener, List<Draft> drafts ){
110121
this.role = Role.SERVER;
111122
// draft.copyInstance will be called when the draft is first needed
112123
if( drafts == null || drafts.isEmpty() ){
113-
knownDrafts = defaultdraftlist;
124+
knownDrafts = newArrayList<Draft>();
125+
knownDrafts.add(newDraft_6455());
114126
} else{
115127
knownDrafts = drafts;
116128
}

‎src/main/java/org/java_websocket/framing/CloseFrameBuilder.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
importorg.java_websocket.exceptions.InvalidDataException;
44
importorg.java_websocket.exceptions.InvalidFrameException;
5+
importorg.java_websocket.util.ByteBufferUtils;
56
importorg.java_websocket.util.Charsetfunctions;
67

78
importjava.nio.ByteBuffer;
89

910
publicclassCloseFrameBuilderextendsFramedataImpl1implementsCloseFrame{
10-
/**
11-
* Attribute for just an empty ByteBuffer
12-
*/
13-
staticfinalByteBufferemptybytebuffer = ByteBuffer.allocate( 0 );
1411

1512
/**
1613
* The close code used in this close frame
@@ -150,7 +147,7 @@ public void setPayload( ByteBuffer payload ) throws InvalidDataException{
150147
@Override
151148
publicByteBuffergetPayloadData(){
152149
if( code == NOCODE )
153-
returnemptybytebuffer;
150+
returnByteBufferUtils.getEmptyByteBuffer();
154151
returnsuper.getPayloadData();
155152
}
156153

‎src/main/java/org/java_websocket/framing/FramedataImpl1.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
importjava.util.Arrays;
55

66
importorg.java_websocket.exceptions.InvalidDataException;
7-
importorg.java_websocket.exceptions.InvalidFrameException;
7+
importorg.java_websocket.util.ByteBufferUtils;
88
importorg.java_websocket.util.Charsetfunctions;
99

1010
publicclassFramedataImpl1implementsFrameBuilder{
11-
/**
12-
* Attribute for just an empty array
13-
*/
14-
privatestaticbyte[] emptyarray ={};
1511

1612
/**
1713
* Indicates that this is the final fragment in a message.
@@ -44,7 +40,7 @@ public FramedataImpl1(){
4440
*/
4541
publicFramedataImpl1( Opcodeop ){
4642
this.optcode = op;
47-
unmaskedpayload = ByteBuffer.wrap( emptyarray);
43+
unmaskedpayload = ByteBufferUtils.getEmptyByteBuffer();
4844
}
4945

5046
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packageorg.java_websocket.util;
2+
3+
importjava.nio.ByteBuffer;
4+
5+
/**
6+
* Utility class for ByteBuffers
7+
*/
8+
publicclassByteBufferUtils{
9+
10+
/**
11+
* Private constructor for static class
12+
*/
13+
privateByteBufferUtils(){
14+
}
15+
16+
/**
17+
* Transfer from one ByteBuffer to another ByteBuffer
18+
*
19+
* @param source the ByteBuffer to copy from
20+
* @param dest the ByteBuffer to copy to
21+
*/
22+
publicstaticvoidtransferByteBuffer( ByteBuffersource, ByteBufferdest ){
23+
if( source == null || dest == null ){
24+
thrownewIllegalArgumentException();
25+
}
26+
intfremain = source.remaining();
27+
inttoremain = dest.remaining();
28+
if( fremain > toremain ){
29+
source.limit( Math.min( fremain, toremain ) );
30+
dest.put( source );
31+
} else{
32+
dest.put( source );
33+
}
34+
35+
}
36+
37+
/**
38+
* Get a ByteBuffer with zero capacity
39+
*
40+
* @return empty ByteBuffer
41+
*/
42+
publicstaticByteBuffergetEmptyByteBuffer(){
43+
returnByteBuffer.allocate( 0 );
44+
}
45+
}

‎src/main/java/org/java_websocket/util/Charsetfunctions.java‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
publicclassCharsetfunctions{
1414

15+
/**
16+
* Private constructor for real static class
17+
*/
18+
privateCharsetfunctions(){
19+
20+
}
21+
1522
publicstaticCodingErrorActioncodingErrorAction = CodingErrorAction.REPORT;
1623

1724
/*
@@ -52,20 +59,6 @@ public static String stringUtf8( byte[] bytes ) throws InvalidDataException{
5259
returnstringUtf8( ByteBuffer.wrap( bytes ) );
5360
}
5461

55-
/*public static String stringUtf8( byte[] bytes, int off, int length ) throws InvalidDataException{
56-
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
57-
decode.onMalformedInput( codingErrorAction );
58-
decode.onUnmappableCharacter( codingErrorAction );
59-
//decode.replaceWith( "X" );
60-
String s;
61-
try{
62-
s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
63-
} catch ( CharacterCodingException e ){
64-
throw new InvalidDataException( CloseFrame.NO_UTF8, e );
65-
}
66-
return s;
67-
}*/
68-
6962
publicstaticStringstringUtf8( ByteBufferbytes ) throwsInvalidDataException{
7063
CharsetDecoderdecode = Charset.forName( "UTF8" ).newDecoder();
7164
decode.onMalformedInput( codingErrorAction );
@@ -82,11 +75,6 @@ public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException
8275
returns;
8376
}
8477

85-
publicstaticvoidmain( String[] args ) throwsInvalidDataException{
86-
stringUtf8( utf8Bytes( "\0" ) );
87-
stringAscii( asciiBytes( "\0" ) );
88-
}
89-
9078
/**
9179
* Implementation of the "Flexible and Economical UTF-8 Decoder" algorithm
9280
* by Björn Höhrmann (http://bjoern.hoehrmann.de/utf-8/decoder/dfa/)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packageorg.java_websocket;
2+
3+
importorg.junit.runner.RunWith;
4+
importorg.junit.runners.Suite;
5+
6+
7+
@RunWith(Suite.class)
8+
@Suite.SuiteClasses({
9+
org.java_websocket.util.ByteBufferUtilsTest.class
10+
})
11+
/**
12+
* Start all tests
13+
*/
14+
publicclassAllTests{
15+
}

0 commit comments

Comments
(0)