From 556f2dabf616d1f7a68bb088316ddbf34300378f Mon Sep 17 00:00:00 2001 From: marci4 Date: Tue, 23 May 2017 09:39:45 +0200 Subject: [PATCH 1/3] ByteBufferUtils + JUnit Test --- .../java_websocket/util/ByteBufferUtils.java | 45 ++++++++++ .../util/ByteBufferUtilsTest.java | 82 +++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/main/java/org/java_websocket/util/ByteBufferUtils.java create mode 100644 src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java diff --git a/src/main/java/org/java_websocket/util/ByteBufferUtils.java b/src/main/java/org/java_websocket/util/ByteBufferUtils.java new file mode 100644 index 000000000..171905b88 --- /dev/null +++ b/src/main/java/org/java_websocket/util/ByteBufferUtils.java @@ -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 ); + } +} diff --git a/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java b/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java new file mode 100644 index 000000000..c1f8f53f5 --- /dev/null +++ b/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java @@ -0,0 +1,82 @@ +package org.java_websocket.util; + +import org.java_websocket.util.ByteBufferUtils; +import org.junit.Test; + +import java.nio.ByteBuffer; + +import static org.junit.Assert.*; + +/** + * JUnit Test for the new ByteBufferUtils class + */ +public class ByteBufferUtilsTest { + + private static byte[] smallArray = { 0, -1, -2, -3, -4 }; + private static byte[] bigArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + @Test + public void testEmptyByteBufferCapacity() { + ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); + assertEquals( "capacity must be 0", byteBuffer.capacity(), 0 ); + } + + @Test + public void testEmptyByteBufferLimit() { + ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); + assertEquals( "limit must be 0", byteBuffer.limit(), 0 ); + } + + @Test + public void testEmptyByteBufferNewObject() { + ByteBuffer byteBuffer0 = ByteBufferUtils.getEmptyByteBuffer(); + ByteBuffer byteBuffer1 = ByteBufferUtils.getEmptyByteBuffer(); + assertTrue( "Allocated new object", byteBuffer0 != byteBuffer1 ); + } + + @Test + public void testTransferByteBufferSmallToEmpty() { + ByteBuffer small = ByteBuffer.wrap( smallArray ); + ByteBuffer empty = ByteBufferUtils.getEmptyByteBuffer(); + ByteBufferUtils.transferByteBuffer( small, empty ); + assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray ); + assertEquals( "capacity of the empty bytebuffer should still be 0", empty.capacity(), 0 ); + } + + @Test + public void testTransferByteBufferSmallToBig() { + ByteBuffer small = ByteBuffer.wrap( smallArray ); + ByteBuffer big = ByteBuffer.wrap( bigArray ); + ByteBufferUtils.transferByteBuffer( small, big ); + assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray ); + assertEquals( big.get( 0 ), smallArray[0] ); + assertEquals( big.get( 1 ), smallArray[1] ); + assertEquals( big.get( 2 ), smallArray[2] ); + assertEquals( big.get( 3 ), smallArray[3] ); + assertEquals( big.get( 4 ), smallArray[4] ); + assertEquals( big.get( 5 ), bigArray[5] ); + assertEquals( big.get( 6 ), bigArray[6] ); + assertEquals( big.get( 7 ), bigArray[7] ); + assertEquals( big.get( 8 ), bigArray[8] ); + } + + @Test + public void testTransferByteBufferBigToSmall() { + ByteBuffer small = ByteBuffer.wrap( smallArray ); + ByteBuffer big = ByteBuffer.wrap( bigArray ); + ByteBufferUtils.transferByteBuffer( big, small ); + assert ( small.get( 0 ) == bigArray[0] ); + assert ( small.get( 1 ) == bigArray[1] ); + assert ( small.get( 2 ) == bigArray[2] ); + assert ( small.get( 3 ) == bigArray[3] ); + assert ( small.get( 4 ) == bigArray[4] ); + assert ( big.array() == bigArray ); + } + + @Test + public void testTransferByteBufferCheckNull() { + ByteBuffer source = ByteBufferUtils.getEmptyByteBuffer(); + ByteBuffer dest = ByteBufferUtils.getEmptyByteBuffer(); + ByteBufferUtils.transferByteBuffer( source, null ); + } +} From f4f7954aa62fb702fc23ce3a591f5b7c154a15ef Mon Sep 17 00:00:00 2001 From: Marcel P Date: Tue, 23 May 2017 13:22:19 +0200 Subject: [PATCH 2/3] Additional JUnit tests Finalized tests for ByteBufferUtils Removed old tests --- .../java_websocket/util/Charsetfunctions.java | 26 +- .../java/org/java_websocket/AllTests.java | 15 ++ .../AutobahnClientScenario.java | 241 ------------------ .../java_websocket/AutobahnClientTest.java | 10 - .../util/ByteBufferUtilsTest.java | 147 ++++++----- 5 files changed, 108 insertions(+), 331 deletions(-) create mode 100644 src/test/java/org/java_websocket/AllTests.java delete mode 100644 src/test/java/org/java_websocket/AutobahnClientScenario.java delete mode 100644 src/test/java/org/java_websocket/AutobahnClientTest.java diff --git a/src/main/java/org/java_websocket/util/Charsetfunctions.java b/src/main/java/org/java_websocket/util/Charsetfunctions.java index 6bf18ccd3..97484b3e1 100644 --- a/src/main/java/org/java_websocket/util/Charsetfunctions.java +++ b/src/main/java/org/java_websocket/util/Charsetfunctions.java @@ -12,6 +12,13 @@ public class Charsetfunctions { + /** + * Private constructor for real static class + */ + private Charsetfunctions() { + + } + public static CodingErrorAction codingErrorAction = CodingErrorAction.REPORT; /* @@ -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 ); @@ -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/) diff --git a/src/test/java/org/java_websocket/AllTests.java b/src/test/java/org/java_websocket/AllTests.java new file mode 100644 index 000000000..e260d6871 --- /dev/null +++ b/src/test/java/org/java_websocket/AllTests.java @@ -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 { +} diff --git a/src/test/java/org/java_websocket/AutobahnClientScenario.java b/src/test/java/org/java_websocket/AutobahnClientScenario.java deleted file mode 100644 index 114b62377..000000000 --- a/src/test/java/org/java_websocket/AutobahnClientScenario.java +++ /dev/null @@ -1,241 +0,0 @@ -package org.java_websocket; - -import cucumber.annotation.After; -import cucumber.annotation.en.Given; -import cucumber.annotation.en.Then; -import cucumber.annotation.en.When; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.drafts.Draft; -import org.java_websocket.handshake.ClientHandshake; -import org.java_websocket.handshake.ServerHandshake; -import org.java_websocket.server.WebSocketServer; -import org.junit.Assert; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.UnknownHostException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.CountDownLatch; - -public class AutobahnClientScenario { - - private class AutobahnServer extends WebSocketServer { - - public AutobahnServer(int port, Draft d) throws UnknownHostException { - super(new InetSocketAddress(port), Collections.singletonList(d)); - } - - @Override - public void onOpen(WebSocket conn, ClientHandshake handshake) { - //throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void onClose(WebSocket conn, int code, String reason, boolean remote) { - //throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void onMessage(WebSocket conn, String message) { - //throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void onError(WebSocket conn, Exception ex) { - //throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void onStart() { - - } - - } - - private class AutobahnClient extends WebSocketClient { - - private final CountDownLatch connectionOpenedLatch; - private final Map openHandShakeFields; - private String message; - - public AutobahnClient(Draft draft, URI uri) { - super(uri, draft); - connectionOpenedLatch = new CountDownLatch(1); - openHandShakeFields = new HashMap(); - } - - @Override - public void onOpen(ServerHandshake handshakedata) { - Iterator it = handshakedata.iterateHttpFields(); - while(it.hasNext()) { - String key = it.next(); - System.out.printf("%s %s%n", key, handshakedata.getFieldValue(key)); // TODO Remove this - openHandShakeFields.put(key, handshakedata.getFieldValue(key)); - } - connectionOpenedLatch.countDown(); - } - - @Override - public void onMessage(String message) { - // TODO Test message receiving - } - - @Override - public void onClose(int code, String reason, boolean remote) { - // TODO Check connection closing - } - - @Override - public void onError(Exception ex) { - // TODO Check error handling - ex.printStackTrace(); - connectionOpenedLatch.countDown(); - } - - } - - private static Draft getDraft(int number) { - Exception exception; - try { - return (Draft) Class.forName("org.java_websocket.drafts.Draft_" + number).newInstance(); - } catch(InstantiationException e) { - exception = e; - } catch(IllegalAccessException e) { - exception = e; - } catch(ClassNotFoundException e) { - exception = e; - } - throw new RuntimeException(exception); - } - - private String protocol; - private String host; - private Integer port; - private String query; - private Draft draft; - - private AutobahnServer autobahnServer; - - @Given("^the Autobahn Server is running using Draft_(\\d+) on port (\\d+)$") - public void startAutobahnServer(int draft, int port) throws UnknownHostException { - autobahnServer = new AutobahnServer(port, getDraft(draft)); - autobahnServer.start(); - } - - @Given("^protocol is (.+)$") - public void createProtocol(String protocol) { - this.protocol = protocol; - } - - @Given("^the host is (.+)$") - public void createHost(String host) { - this.host = host; - } - - @Given("^the port is (\\d+)$") - public void createPort(int port) { - this.port = port; - } - - @Given("^the query string is (.+)$") - public void createQuery(String query) { - this.query = query; - } - - @Given("^the draft is Draft_(\\d+)") - public void createDraft(int draft) { - this.draft = getDraft(draft); - } - - private AutobahnClient autobahnClient; - - @When("^the client connects to the server$") - public void connectToServer() { - URI uri; - try { - uri = new URI(this.protocol, null, this.host, this.port, null, this.query, null); - } catch(URISyntaxException e) { - throw new RuntimeException(e); - } - - System.out.println(uri); - autobahnClient = new AutobahnClient(this.draft, uri); - try { - autobahnClient.connectBlocking(); - autobahnClient.connectionOpenedLatch.await(); - } catch(InterruptedException e) { - Assert.assertTrue(e.getMessage(), false); - e.printStackTrace(); - } - } - - @Then("^the server response should contain (.+)$") - public void checkMethod(String method) { - // TODO Implement check - //assertTrue(method.contains("GET")); - } - - @Then("^the response's query should contain (.+)$") - public void checkQuery(String query) { - // TODO Implement check - //assertTrue(query.contains(this.query)); - } - - @Then("^the response's http version should contain (.+)$") - public void checkHttpVersion(String httpversion) { - // TODO Implement check - //assertTrue(.contains("HTTP/" + major + "." + minor)); - } - - @Then("^the response's handshake should contain (.+)$") - public void checkHandshake(String handshake) { - Assert.assertEquals(handshake, autobahnClient.openHandShakeFields.get("Connection")); - } - - @Then("^the response's host should contain (.+)$") - public void checkHost(String host) { - // TODO Implement check - //assertTrue(host.contains(this.host)); - } - - @Then("^the response's websocket key should contain (.+)$") - public void checkWebSocketKey(String websocketKey) { - // TODO Implement check - //Assert.assertTrue(autobahnClient.openHandShakeFields.containsKey(websocketKey)); - //assertTrue(websocketKey.contains("Sec-WebSocket-Key:")); - } - - @Then("^the response's websocket version should contain (.+)$") - public void checkWebSocketVersion(String websocketVersion) { - // TODO Implement check - //assertTrue(websocketVersion.contains("Sec-WebSocket-Version:")); - } - - @Then("^the response's upgraded protocol should contain (.+)$") - public void checkUpgradedProtocol(String upgradedProtocol) { - Assert.assertEquals(upgradedProtocol, autobahnClient.openHandShakeFields.get("Upgrade")); - } - - @After - public void cleanup() { - try { - autobahnClient.closeBlocking(); - } catch(InterruptedException e) { - e.printStackTrace(); - } - - try { - autobahnServer.stop(); - } catch(IOException e) { - e.printStackTrace(); - } catch(InterruptedException e) { - e.printStackTrace(); - } - } - -} diff --git a/src/test/java/org/java_websocket/AutobahnClientTest.java b/src/test/java/org/java_websocket/AutobahnClientTest.java deleted file mode 100644 index 600054066..000000000 --- a/src/test/java/org/java_websocket/AutobahnClientTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.java_websocket; - -import org.junit.runner.RunWith; - -import cucumber.junit.Cucumber; - -@RunWith(Cucumber.class) -public class AutobahnClientTest { - -} diff --git a/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java b/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java index c1f8f53f5..e22cadcf4 100644 --- a/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java +++ b/src/test/java/org/java_websocket/util/ByteBufferUtilsTest.java @@ -1,6 +1,5 @@ package org.java_websocket.util; -import org.java_websocket.util.ByteBufferUtils; import org.junit.Test; import java.nio.ByteBuffer; @@ -12,71 +11,97 @@ */ public class ByteBufferUtilsTest { - private static byte[] smallArray = { 0, -1, -2, -3, -4 }; - private static byte[] bigArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + /** + * A small byte array with some data + */ + private static byte[] smallArray = {0, -1, -2, -3, -4}; - @Test - public void testEmptyByteBufferCapacity() { - ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); - assertEquals( "capacity must be 0", byteBuffer.capacity(), 0 ); - } + /** + * A big byte array with some data + */ + private static byte[] bigArray = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - @Test - public void testEmptyByteBufferLimit() { - ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); - assertEquals( "limit must be 0", byteBuffer.limit(), 0 ); - } + @Test + public void testEmptyByteBufferCapacity() { + ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); + assertEquals("capacity must be 0", 0, byteBuffer.capacity()); + } - @Test - public void testEmptyByteBufferNewObject() { - ByteBuffer byteBuffer0 = ByteBufferUtils.getEmptyByteBuffer(); - ByteBuffer byteBuffer1 = ByteBufferUtils.getEmptyByteBuffer(); - assertTrue( "Allocated new object", byteBuffer0 != byteBuffer1 ); - } + @Test + public void testEmptyByteBufferNewObject() { + ByteBuffer byteBuffer0 = ByteBufferUtils.getEmptyByteBuffer(); + ByteBuffer byteBuffer1 = ByteBufferUtils.getEmptyByteBuffer(); + assertTrue("Allocated new object", byteBuffer0 != byteBuffer1); + } - @Test - public void testTransferByteBufferSmallToEmpty() { - ByteBuffer small = ByteBuffer.wrap( smallArray ); - ByteBuffer empty = ByteBufferUtils.getEmptyByteBuffer(); - ByteBufferUtils.transferByteBuffer( small, empty ); - assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray ); - assertEquals( "capacity of the empty bytebuffer should still be 0", empty.capacity(), 0 ); - } + @Test + public void testTransferByteBufferSmallToEmpty() { + ByteBuffer small = ByteBuffer.wrap(smallArray); + ByteBuffer empty = ByteBufferUtils.getEmptyByteBuffer(); + ByteBufferUtils.transferByteBuffer(small, empty); + assertArrayEquals("Small bytebuffer should not change", smallArray, small.array()); + assertEquals("Capacity of the empty bytebuffer should still be 0", 0, empty.capacity()); + } - @Test - public void testTransferByteBufferSmallToBig() { - ByteBuffer small = ByteBuffer.wrap( smallArray ); - ByteBuffer big = ByteBuffer.wrap( bigArray ); - ByteBufferUtils.transferByteBuffer( small, big ); - assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray ); - assertEquals( big.get( 0 ), smallArray[0] ); - assertEquals( big.get( 1 ), smallArray[1] ); - assertEquals( big.get( 2 ), smallArray[2] ); - assertEquals( big.get( 3 ), smallArray[3] ); - assertEquals( big.get( 4 ), smallArray[4] ); - assertEquals( big.get( 5 ), bigArray[5] ); - assertEquals( big.get( 6 ), bigArray[6] ); - assertEquals( big.get( 7 ), bigArray[7] ); - assertEquals( big.get( 8 ), bigArray[8] ); - } + @Test + public void testTransferByteBufferSmallToBig() { + ByteBuffer small = ByteBuffer.wrap(smallArray); + ByteBuffer big = ByteBuffer.wrap(bigArray); + ByteBufferUtils.transferByteBuffer(small, big); + assertArrayEquals("Small bytebuffer should not change", smallArray, small.array()); + assertEquals("Big bytebuffer not same to source 0", smallArray[0], big.get(0)); + assertEquals("Big bytebuffer not same to source 1", smallArray[1], big.get(1)); + assertEquals("Big bytebuffer not same to source 2", smallArray[2], big.get(2)); + assertEquals("Big bytebuffer not same to source 3", smallArray[3], big.get(3)); + assertEquals("Big bytebuffer not same to source 4", smallArray[4], big.get(4)); + assertEquals("Big bytebuffer not same to source 5", bigArray[5], big.get(5)); + assertEquals("Big bytebuffer not same to source 6", bigArray[6], big.get(6)); + assertEquals("Big bytebuffer not same to source 7", bigArray[7], big.get(7)); + assertEquals("Big bytebuffer not same to source 8", bigArray[8], big.get(8)); + } - @Test - public void testTransferByteBufferBigToSmall() { - ByteBuffer small = ByteBuffer.wrap( smallArray ); - ByteBuffer big = ByteBuffer.wrap( bigArray ); - ByteBufferUtils.transferByteBuffer( big, small ); - assert ( small.get( 0 ) == bigArray[0] ); - assert ( small.get( 1 ) == bigArray[1] ); - assert ( small.get( 2 ) == bigArray[2] ); - assert ( small.get( 3 ) == bigArray[3] ); - assert ( small.get( 4 ) == bigArray[4] ); - assert ( big.array() == bigArray ); - } + @Test + public void testTransferByteBufferBigToSmall() { + ByteBuffer small = ByteBuffer.wrap(smallArray); + ByteBuffer big = ByteBuffer.wrap(bigArray); + ByteBufferUtils.transferByteBuffer(big, small); + assertArrayEquals("Big bytebuffer should not change", bigArray, big.array()); + assertEquals("Small bytebuffer not same to source 0", bigArray[0], small.get(0)); + assertEquals("Small bytebuffer not same to source 1", bigArray[1], small.get(1)); + assertEquals("Small bytebuffer not same to source 2", bigArray[2], small.get(2)); + assertEquals("Small bytebuffer not same to source 3", bigArray[3], small.get(3)); + assertEquals("Small bytebuffer not same to source 4", bigArray[4], small.get(4)); + } - @Test - public void testTransferByteBufferCheckNull() { - ByteBuffer source = ByteBufferUtils.getEmptyByteBuffer(); - ByteBuffer dest = ByteBufferUtils.getEmptyByteBuffer(); - ByteBufferUtils.transferByteBuffer( source, null ); - } + @Test + public void testTransferByteBufferCheckNullDest() { + ByteBuffer source = ByteBuffer.wrap(smallArray); + try { + ByteBufferUtils.transferByteBuffer(source, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + //Fine + } + } + + @Test + public void testTransferByteBufferCheckNullSource() { + ByteBuffer dest = ByteBuffer.wrap(smallArray); + try { + ByteBufferUtils.transferByteBuffer(null, dest); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + //Fine + } + } + + @Test + public void testTransferByteBufferCheckNullBoth() { + try { + ByteBufferUtils.transferByteBuffer(null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + //Fine + } + } } From b790f771a0a70714f10b1381144f149b46584ed5 Mon Sep 17 00:00:00 2001 From: Marcel P Date: Tue, 23 May 2017 14:47:13 +0200 Subject: [PATCH 3/3] Usage of ByteBufferUtils --- .../org/java_websocket/WebSocketImpl.java | 26 ++++++++++++++----- .../framing/CloseFrameBuilder.java | 7 ++--- .../framing/FramedataImpl1.java | 8 ++---- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/java_websocket/WebSocketImpl.java b/src/main/java/org/java_websocket/WebSocketImpl.java index 6f0326669..00f6160c1 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -35,14 +35,12 @@ * text frames, and receiving frames through an event-based model. */ public class WebSocketImpl implements WebSocket { - - public static final List defaultdraftlist = new ArrayList( 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. @@ -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 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; /** @@ -110,7 +121,8 @@ public WebSocketImpl( WebSocketListener listener, List 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(); + knownDrafts.add(new Draft_6455()); } else { knownDrafts = drafts; } diff --git a/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java b/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java index a10a790e3..3f44eb28b 100644 --- a/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java +++ b/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java @@ -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 @@ -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(); } diff --git a/src/main/java/org/java_websocket/framing/FramedataImpl1.java b/src/main/java/org/java_websocket/framing/FramedataImpl1.java index 9dce2e4f3..7d0f8e1d3 100644 --- a/src/main/java/org/java_websocket/framing/FramedataImpl1.java +++ b/src/main/java/org/java_websocket/framing/FramedataImpl1.java @@ -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. @@ -44,7 +40,7 @@ public FramedataImpl1() { */ public FramedataImpl1( Opcode op ) { this.optcode = op; - unmaskedpayload = ByteBuffer.wrap( emptyarray ); + unmaskedpayload = ByteBufferUtils.getEmptyByteBuffer(); } /**