Skip to content

ByteBuffer and JUnitTests #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@
* text frames, and receiving frames through an event-based model.
*/
public class WebSocketImpl implements WebSocket {

public static final List<Draft> defaultdraftlist = new ArrayList<Draft>( 1 );
public static int RCVBUF = 16384;
public static/*final*/ boolean DEBUG = false; // must be final in the future in order to take advantage of VM optimization

static {
defaultdraftlist.add( new Draft_6455() );
}
/**
* Activate debug mode for additional infos
*/
public static boolean DEBUG = false; // must be final in the future in order to take advantage of VM optimization

/**
* Queue of buffers that need to be sent to the client.
Expand Down Expand Up @@ -70,12 +68,25 @@ public class WebSocketImpl implements WebSocket {
*/
private volatile boolean flushandclosestate = false;
private READYSTATE readystate = READYSTATE.NOT_YET_CONNECTED;

/**
* A list of drafts available for this websocket
*/
private List<Draft> knownDrafts;

/**
* The draft which is used by this websocket
*/
private Draft draft = null;

/**
* The role which this websocket takes in the connection
*/
private Role role;

/**
* The frame which had the opcode Continous set
*/
private Framedata current_continuous_frame = null;

/**
Expand Down Expand Up @@ -110,7 +121,8 @@ public WebSocketImpl( WebSocketListener listener, List<Draft> drafts ) {
this.role = Role.SERVER;
// draft.copyInstance will be called when the draft is first needed
if( drafts == null || drafts.isEmpty() ) {
knownDrafts = defaultdraftlist;
knownDrafts = new ArrayList<Draft>();
knownDrafts.add(new Draft_6455());
} else {
knownDrafts = drafts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import org.java_websocket.exceptions.InvalidDataException;
import org.java_websocket.exceptions.InvalidFrameException;
import org.java_websocket.util.ByteBufferUtils;
import org.java_websocket.util.Charsetfunctions;

import java.nio.ByteBuffer;

public class CloseFrameBuilder extends FramedataImpl1 implements CloseFrame {
/**
* Attribute for just an empty ByteBuffer
*/
static final ByteBuffer emptybytebuffer = ByteBuffer.allocate( 0 );

/**
* The close code used in this close frame
Expand Down Expand Up @@ -150,7 +147,7 @@ public void setPayload( ByteBuffer payload ) throws InvalidDataException {
@Override
public ByteBuffer getPayloadData() {
if( code == NOCODE )
return emptybytebuffer;
return ByteBufferUtils.getEmptyByteBuffer();
return super.getPayloadData();
}

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/java_websocket/framing/FramedataImpl1.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
import java.util.Arrays;

import org.java_websocket.exceptions.InvalidDataException;
import org.java_websocket.exceptions.InvalidFrameException;
import org.java_websocket.util.ByteBufferUtils;
import org.java_websocket.util.Charsetfunctions;

public class FramedataImpl1 implements FrameBuilder {
/**
* Attribute for just an empty array
*/
private static byte[] emptyarray = {};

/**
* Indicates that this is the final fragment in a message.
Expand Down Expand Up @@ -44,7 +40,7 @@ public FramedataImpl1() {
*/
public FramedataImpl1( Opcode op ) {
this.optcode = op;
unmaskedpayload = ByteBuffer.wrap( emptyarray );
unmaskedpayload = ByteBufferUtils.getEmptyByteBuffer();
}

/**
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/java_websocket/util/ByteBufferUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.java_websocket.util;

import java.nio.ByteBuffer;

/**
* Utility class for ByteBuffers
*/
public class ByteBufferUtils {

/**
* Private constructor for static class
*/
private ByteBufferUtils() {
}

/**
* Transfer from one ByteBuffer to another ByteBuffer
*
* @param source the ByteBuffer to copy from
* @param dest the ByteBuffer to copy to
*/
public static void transferByteBuffer( ByteBuffer source, ByteBuffer dest ) {
if( source == null || dest == null ) {
throw new IllegalArgumentException();
}
int fremain = source.remaining();
int toremain = dest.remaining();
if( fremain > toremain ) {
source.limit( Math.min( fremain, toremain ) );
dest.put( source );
} else {
dest.put( source );
}

}

/**
* Get a ByteBuffer with zero capacity
*
* @return empty ByteBuffer
*/
public static ByteBuffer getEmptyByteBuffer() {
return ByteBuffer.allocate( 0 );
}
}
26 changes: 7 additions & 19 deletions src/main/java/org/java_websocket/util/Charsetfunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

public class Charsetfunctions {

/**
* Private constructor for real static class
*/
private Charsetfunctions() {

}

public static CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;

/*
Expand Down Expand Up @@ -52,20 +59,6 @@ public static String stringUtf8( byte[] bytes ) throws InvalidDataException {
return stringUtf8( ByteBuffer.wrap( bytes ) );
}

/*public static String stringUtf8( byte[] bytes, int off, int length ) throws InvalidDataException {
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
decode.onMalformedInput( codingErrorAction );
decode.onUnmappableCharacter( codingErrorAction );
//decode.replaceWith( "X" );
String s;
try {
s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
} catch ( CharacterCodingException e ) {
throw new InvalidDataException( CloseFrame.NO_UTF8, e );
}
return s;
}*/

public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException {
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
decode.onMalformedInput( codingErrorAction );
Expand All @@ -82,11 +75,6 @@ public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException
return s;
}

public static void main( String[] args ) throws InvalidDataException {
stringUtf8( utf8Bytes( "\0" ) );
stringAscii( asciiBytes( "\0" ) );
}

/**
* Implementation of the "Flexible and Economical UTF-8 Decoder" algorithm
* by Björn Höhrmann (http://bjoern.hoehrmann.de/utf-8/decoder/dfa/)
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/java_websocket/AllTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.java_websocket;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Suite.class)
@Suite.SuiteClasses({
org.java_websocket.util.ByteBufferUtilsTest.class
})
/**
* Start all tests
*/
public class AllTests {
}
Loading