From bdfeb347be317ef74489a3daaf52666ffd90c80e Mon Sep 17 00:00:00 2001 From: Roman Krejcik Date: Wed, 27 Aug 2014 16:39:06 +0200 Subject: [PATCH 1/3] close selector to relese socket (without it it now works property at least in java7u67 (but works on older some older version and eg open jdk) --- .../java/org/java_websocket/server/WebSocketServer.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/java_websocket/server/WebSocketServer.java b/src/main/java/org/java_websocket/server/WebSocketServer.java index a45f7e133..eef8d4d76 100644 --- a/src/main/java/org/java_websocket/server/WebSocketServer.java +++ b/src/main/java/org/java_websocket/server/WebSocketServer.java @@ -283,6 +283,7 @@ public void run() { server.configureBlocking( false ); ServerSocket socket = server.socket(); socket.setReceiveBufferSize( WebSocketImpl.RCVBUF ); + socket.setReuseAddress(true); socket.bind( address ); selector = Selector.open(); server.register( selector, server.validOps() ); @@ -396,6 +397,13 @@ public void run() { w.interrupt(); } } + if (selector != null) { + try { + selector.close(); + } catch (IOException e) { + onError( null, e); + } + } if( server != null ) { try { server.close(); From da65cf6da89998cf5529a83c48c91248b78dab50 Mon Sep 17 00:00:00 2001 From: Roman Krejcik Date: Fri, 4 Nov 2016 21:49:59 +0100 Subject: [PATCH 2/3] removed javadoc plugin to compile project in Java 8 --- pom.xml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/pom.xml b/pom.xml index 9189fba8d..799044e00 100644 --- a/pom.xml +++ b/pom.xml @@ -46,19 +46,6 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9 - - - attach-javadocs - - jar - - - - From 0d38304c920ea1a0775ad8e9a842e9434b79d39c Mon Sep 17 00:00:00 2001 From: Roman Krejcik Date: Thu, 11 Apr 2019 11:24:28 +0200 Subject: [PATCH 3/3] Fix runtime error when compiled with Java 9+ and run on Java 8 see same issue eg there https://github.com/apache/felix/pull/114 --- pom.xml | 60 +++++++++---------- .../org/java_websocket/SSLSocketChannel2.java | 29 ++++----- .../java_websocket/SocketChannelIOHelper.java | 9 +-- .../org/java_websocket/WebSocketImpl.java | 35 +++++------ .../java/org/java_websocket/drafts/Draft.java | 11 ++-- .../org/java_websocket/drafts/Draft_10.java | 25 ++++---- .../org/java_websocket/drafts/Draft_75.java | 13 ++-- .../org/java_websocket/drafts/Draft_76.java | 9 +-- .../framing/CloseFrameBuilder.java | 17 +++--- .../framing/FramedataImpl1.java | 17 +++--- .../java_websocket/util/Charsetfunctions.java | 7 ++- 11 files changed, 121 insertions(+), 111 deletions(-) diff --git a/pom.xml b/pom.xml index 799044e00..e3679cb80 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ A barebones WebSocket client and server implementation written in 100% Java UTF-8 - 1.6 + 1.8 @@ -74,33 +74,33 @@ http://github.com/TooTallNate/Java-WebSocket/blob/master/LICENSE - - - release-sign-artifacts - - performReleasetrue - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.1 - - - sign-artifacts - verify - - sign - - - - - rohmer.david@gmail.com - - - - - - + + + release-sign-artifacts + + performReleasetrue + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.1 + + + sign-artifacts + verify + + sign + + + + + rohmer.david@gmail.com + + + + + + diff --git a/src/main/java/org/java_websocket/SSLSocketChannel2.java b/src/main/java/org/java_websocket/SSLSocketChannel2.java index ba8d8f88a..5e796dc17 100644 --- a/src/main/java/org/java_websocket/SSLSocketChannel2.java +++ b/src/main/java/org/java_websocket/SSLSocketChannel2.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.net.Socket; import java.net.SocketAddress; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; import java.nio.channels.SelectableChannel; @@ -129,7 +130,7 @@ private synchronized void processHandshake() throws IOException { if( read == -1 ) { throw new IOException( "connection closed unexpectedly by peer" ); } - inCrypt.flip(); + ((Buffer)inCrypt).flip(); } inData.compact(); unwrap(); @@ -153,7 +154,7 @@ private synchronized void processHandshake() throws IOException { private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException { outCrypt.compact(); writeEngineResult = sslEngine.wrap( b, outCrypt ); - outCrypt.flip(); + ((Buffer)outCrypt).flip(); return outCrypt; } @@ -166,7 +167,7 @@ private synchronized ByteBuffer unwrap() throws SSLException { rem = inData.remaining(); readEngineResult = sslEngine.unwrap( inCrypt, inData ); } while ( readEngineResult.getStatus() == SSLEngineResult.Status.OK && ( rem != inData.remaining() || sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP ) ); - inData.flip(); + ((Buffer)inData).flip(); return inData; } @@ -194,12 +195,12 @@ protected void createBuffers( SSLSession session ) { if( inCrypt.capacity() != netBufferMax ) inCrypt = ByteBuffer.allocate( netBufferMax ); } - inData.rewind(); - inData.flip(); - inCrypt.rewind(); - inCrypt.flip(); - outCrypt.rewind(); - outCrypt.flip(); + ((Buffer)inData).rewind(); + ((Buffer)inData).flip(); + ((Buffer)inCrypt).rewind(); + ((Buffer)inCrypt).flip(); + ((Buffer)outCrypt).rewind(); + ((Buffer)outCrypt).flip(); bufferallocations++; } @@ -220,7 +221,7 @@ public int write( ByteBuffer src ) throws IOException { /** * Blocks when in blocking mode until at least one byte has been decoded.
* When not in blocking mode 0 may be returned. - * + * * @return the number of bytes read. **/ public int read( ByteBuffer dst ) throws IOException { @@ -253,10 +254,10 @@ public int read( ByteBuffer dst ) throws IOException { * Thats the case if inData is empty or inCrypt holds to less data than necessary for decryption */ assert ( inData.position() == 0 ); - inData.clear(); + ((Buffer)inData).clear(); if( !inCrypt.hasRemaining() ) - inCrypt.clear(); + ((Buffer)inCrypt).clear(); else inCrypt.compact(); @@ -264,7 +265,7 @@ public int read( ByteBuffer dst ) throws IOException { if( socketChannel.read( inCrypt ) == -1 ) { return -1; } - inCrypt.flip(); + ((Buffer)inCrypt).flip(); unwrap(); int transfered = transfereTo( inData, dst ); @@ -281,7 +282,7 @@ private int readRemaining( ByteBuffer dst ) throws SSLException { return transfereTo( inData, dst ); } if( !inData.hasRemaining() ) - inData.clear(); + ((Buffer)inData).clear(); // test if some bytes left from last read (e.g. BUFFER_UNDERFLOW) if( inCrypt.hasRemaining() ) { unwrap(); diff --git a/src/main/java/org/java_websocket/SocketChannelIOHelper.java b/src/main/java/org/java_websocket/SocketChannelIOHelper.java index e0da2bdc3..6239b09e7 100644 --- a/src/main/java/org/java_websocket/SocketChannelIOHelper.java +++ b/src/main/java/org/java_websocket/SocketChannelIOHelper.java @@ -1,6 +1,7 @@ package org.java_websocket; import java.io.IOException; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; import java.nio.channels.spi.AbstractSelectableChannel; @@ -10,9 +11,9 @@ public class SocketChannelIOHelper { public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel channel ) throws IOException { - buf.clear(); + ((Buffer)buf).clear(); int read = channel.read( buf ); - buf.flip(); + ((Buffer)buf).flip(); if( read == -1 ) { ws.eot(); @@ -26,9 +27,9 @@ public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel * @return returns whether there is more data left which can be obtained via {@link #readMore(ByteBuffer, WebSocketImpl, WrappedByteChannel)} **/ public static boolean readMore( final ByteBuffer buf, WebSocketImpl ws, WrappedByteChannel channel ) throws IOException { - buf.clear(); + ((Buffer)buf).clear(); int read = channel.readMore( buf ); - buf.flip(); + ((Buffer)buf).flip(); if( read == -1 ) { ws.eot(); diff --git a/src/main/java/org/java_websocket/WebSocketImpl.java b/src/main/java/org/java_websocket/WebSocketImpl.java index 669bee146..6d7958a74 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; import java.nio.channels.NotYetConnectedException; @@ -40,7 +41,7 @@ * Represents one end (client or server) of a single WebSocketImpl connection. * Takes care of the "handshake" phase, then allows for easy sending of * text frames, and receiving frames through an event-based model. - * + * */ public class WebSocketImpl implements WebSocket { @@ -101,7 +102,7 @@ public class WebSocketImpl implements WebSocket { private String closemessage = null; private Integer closecode = null; private Boolean closedremotely = null; - + private String resourceDescriptor = null; /** @@ -120,7 +121,7 @@ public WebSocketImpl( WebSocketListener listener , List drafts ) { /** * crates a websocket with client role - * + * * @param socket * may be unbound */ @@ -146,7 +147,7 @@ public WebSocketImpl( WebSocketListener listener , List drafts , Socket s } /** - * + * */ public void decode( ByteBuffer socketBuffer ) { assert ( socketBuffer.hasRemaining() ); @@ -180,16 +181,16 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) { } else { if( tmpHandshakeBytes.remaining() < socketBufferNew.remaining() ) { ByteBuffer buf = ByteBuffer.allocate( tmpHandshakeBytes.capacity() + socketBufferNew.remaining() ); - tmpHandshakeBytes.flip(); + ((Buffer)tmpHandshakeBytes).flip(); buf.put( tmpHandshakeBytes ); tmpHandshakeBytes = buf; } tmpHandshakeBytes.put( socketBufferNew ); - tmpHandshakeBytes.flip(); + ((Buffer)tmpHandshakeBytes).flip(); socketBuffer = tmpHandshakeBytes; } - socketBuffer.mark(); + ((Buffer)socketBuffer).mark(); try { if( draft == null ) { HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer ); @@ -212,7 +213,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) { d = d.copyInstance(); try { d.setParseMode( role ); - socketBuffer.reset(); + ((Buffer)socketBuffer).reset(); Handshakedata tmphandshake = d.translateHandshake( socketBuffer ); if( tmphandshake instanceof ClientHandshake == false ) { flushAndClose( CloseFrame.PROTOCOL_ERROR, "wrong http function", false ); @@ -295,7 +296,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) { } } catch ( IncompleteHandshakeException e ) { if( tmpHandshakeBytes.capacity() == 0 ) { - socketBuffer.reset(); + ((Buffer)socketBuffer).reset(); int newsize = e.getPreferedSize(); if( newsize == 0 ) { newsize = socketBuffer.capacity() + 16; @@ -307,8 +308,8 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) { tmpHandshakeBytes.put( socketBufferNew ); // tmpHandshakeBytes.flip(); } else { - tmpHandshakeBytes.position( tmpHandshakeBytes.limit() ); - tmpHandshakeBytes.limit( tmpHandshakeBytes.capacity() ); + ((Buffer)tmpHandshakeBytes).position( tmpHandshakeBytes.limit() ); + ((Buffer)tmpHandshakeBytes).limit( tmpHandshakeBytes.capacity() ); } } return false; @@ -438,7 +439,7 @@ public void close( int code, String message ) { } /** - * + * * @param remote * Indicates who "generated" code.
* true means that this endpoint received the code from the other endpoint.
@@ -539,7 +540,7 @@ public void close( InvalidDataException e ) { /** * Send Text data to the other end. - * + * * @throws IllegalArgumentException * @throws NotYetConnectedException */ @@ -552,7 +553,7 @@ public void send( String text ) throws WebsocketNotConnectedException { /** * Send Binary data (plain bytes) to the other end. - * + * * @throws IllegalArgumentException * @throws NotYetConnectedException */ @@ -594,7 +595,7 @@ public boolean hasBufferedData() { } private HandshakeState isFlashEdgeCase( ByteBuffer request ) throws IncompleteHandshakeException { - request.mark(); + ((Buffer)request).mark(); if( request.limit() > Draft.FLASH_POLICY_REQUEST.length ) { return HandshakeState.NOT_MATCHED; } else if( request.limit() < Draft.FLASH_POLICY_REQUEST.length ) { @@ -603,7 +604,7 @@ private HandshakeState isFlashEdgeCase( ByteBuffer request ) throws IncompleteHa for( int flash_policy_index = 0 ; request.hasRemaining() ; flash_policy_index++ ) { if( Draft.FLASH_POLICY_REQUEST[ flash_policy_index ] != request.get() ) { - request.reset(); + ((Buffer)request).reset(); return HandshakeState.NOT_MATCHED; } } @@ -619,7 +620,7 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali resourceDescriptor = handshakedata.getResourceDescriptor(); assert( resourceDescriptor != null ); - + // Notify Listener try { wsl.onWebsocketHandshakeSentAsClient( this, this.handshakerequest ); diff --git a/src/main/java/org/java_websocket/drafts/Draft.java b/src/main/java/org/java_websocket/drafts/Draft.java index 65b34de8f..e25efd5a1 100644 --- a/src/main/java/org/java_websocket/drafts/Draft.java +++ b/src/main/java/org/java_websocket/drafts/Draft.java @@ -1,5 +1,6 @@ package org.java_websocket.drafts; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.util.Collections; import java.util.Iterator; @@ -60,14 +61,14 @@ public static ByteBuffer readLine( ByteBuffer buf ) { cur = buf.get(); sbuf.put( cur ); if( prev == (byte) '\r' && cur == (byte) '\n' ) { - sbuf.limit( sbuf.position() - 2 ); - sbuf.position( 0 ); + ((Buffer)sbuf).limit( sbuf.position() - 2 ); + ((Buffer)sbuf).position( 0 ); return sbuf; } } // ensure that there wont be any bytes skipped - buf.position( buf.position() - sbuf.position() ); + ((Buffer)buf).position( buf.position() - sbuf.position() ); return null; } @@ -189,7 +190,7 @@ public List createHandshake( Handshakedata handshakedata, Role ownro bytebuffer.put( httpheader ); if( content != null ) bytebuffer.put( content ); - bytebuffer.flip(); + ((Buffer)bytebuffer).flip(); return Collections.singletonList( bytebuffer ); } @@ -220,7 +221,7 @@ public int checkAlloc( int bytecount ) throws LimitExedeedException , InvalidDat public void setParseMode( Role role ) { this.role = role; } - + public Role getRole() { return role; } diff --git a/src/main/java/org/java_websocket/drafts/Draft_10.java b/src/main/java/org/java_websocket/drafts/Draft_10.java index 305460a52..3a80fcd70 100644 --- a/src/main/java/org/java_websocket/drafts/Draft_10.java +++ b/src/main/java/org/java_websocket/drafts/Draft_10.java @@ -1,5 +1,6 @@ package org.java_websocket.drafts; import java.math.BigInteger; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -123,7 +124,7 @@ public ByteBuffer createBinaryFrame( Framedata framedata ) { buf.put( mes ); // translateFrame ( buf.array () , buf.array ().length ); assert ( buf.remaining() == 0 ) : buf.remaining(); - buf.flip(); + ((Buffer)buf).flip(); return buf; } @@ -248,20 +249,20 @@ public List translateFrame( ByteBuffer buffer ) throws LimitExedeedEx // complete an incomplete frame while ( true ) { try { - buffer.mark(); + ((Buffer)buffer).mark(); int available_next_byte_count = buffer.remaining();// The number of bytes received int expected_next_byte_count = incompleteframe.remaining();// The number of bytes to complete the incomplete frame if( expected_next_byte_count > available_next_byte_count ) { // did not receive enough bytes to complete the frame incompleteframe.put( buffer.array(), buffer.position(), available_next_byte_count ); - buffer.position( buffer.position() + available_next_byte_count ); + ((Buffer)buffer).position( buffer.position() + available_next_byte_count ); return Collections.emptyList(); } incompleteframe.put( buffer.array(), buffer.position(), expected_next_byte_count ); - buffer.position( buffer.position() + expected_next_byte_count ); + ((Buffer)buffer).position( buffer.position() + expected_next_byte_count ); - cur = translateSingleFrame( (ByteBuffer) incompleteframe.duplicate().position( 0 ) ); + cur = translateSingleFrame( (ByteBuffer) ((Buffer)incompleteframe.duplicate()).position( 0 ) ); frames.add( cur ); incompleteframe = null; break; // go on with the normal frame receival @@ -270,23 +271,23 @@ public List translateFrame( ByteBuffer buffer ) throws LimitExedeedEx int oldsize = incompleteframe.limit(); ByteBuffer extendedframe = ByteBuffer.allocate( checkAlloc( e.getPreferedSize() ) ); assert ( extendedframe.limit() > incompleteframe.limit() ); - incompleteframe.rewind(); + ((Buffer)incompleteframe).rewind(); extendedframe.put( incompleteframe ); incompleteframe = extendedframe; - + return translateFrame( buffer ); } } } while ( buffer.hasRemaining() ) {// Read as much as possible full frames - buffer.mark(); + ((Buffer)buffer).mark(); try { cur = translateSingleFrame( buffer ); frames.add( cur ); } catch ( IncompleteException e ) { // remember the incomplete data - buffer.reset(); + ((Buffer)buffer).reset(); int pref = e.getPreferedSize(); incompleteframe = ByteBuffer.allocate( checkAlloc( pref ) ); incompleteframe.put( buffer ); @@ -363,8 +364,8 @@ public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteExce payload.put( (byte) ( (byte) buffer.get( /*payloadstart + i*/) ^ (byte) maskskey[ i % 4 ] ) ); } } else { - payload.put( buffer.array(), buffer.position(), payload.limit() ); - buffer.position( buffer.position() + payload.limit() ); + payload.put( buffer.array(), ((Buffer)buffer).position(), payload.limit() ); + ((Buffer)buffer).position( ((Buffer)buffer).position() + payload.limit() ); } FrameBuilder frame; @@ -375,7 +376,7 @@ public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteExce frame.setFin( FIN ); frame.setOptcode( optcode ); } - payload.flip(); + ((Buffer)payload).flip(); frame.setPayload( payload ); return frame; } diff --git a/src/main/java/org/java_websocket/drafts/Draft_75.java b/src/main/java/org/java_websocket/drafts/Draft_75.java index 947a35ece..37adcae3a 100644 --- a/src/main/java/org/java_websocket/drafts/Draft_75.java +++ b/src/main/java/org/java_websocket/drafts/Draft_75.java @@ -1,5 +1,6 @@ package org.java_websocket.drafts; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.util.Collections; import java.util.LinkedList; @@ -72,11 +73,11 @@ public ByteBuffer createBinaryFrame( Framedata framedata ) { ByteBuffer pay = framedata.getPayloadData(); ByteBuffer b = ByteBuffer.allocate( pay.remaining() + 2 ); b.put( START_OF_FRAME ); - pay.mark(); + ((Buffer)pay).mark(); b.put( pay ); - pay.reset(); + ((Buffer)pay).reset(); b.put( END_OF_FRAME ); - b.flip(); + ((Buffer)b).flip(); return b; } @@ -136,14 +137,14 @@ protected List translateRegularFrame( ByteBuffer buffer ) throws Inva // currentFrame will be null if END_OF_FRAME was send directly after // START_OF_FRAME, thus we will send 'null' as the sent message. if( this.currentFrame != null ) { - currentFrame.flip(); + ((Buffer)currentFrame).flip(); FramedataImpl1 curframe = new FramedataImpl1(); curframe.setPayload( currentFrame ); curframe.setFin( true ); curframe.setOptcode( Opcode.TEXT ); readyframes.add( curframe ); this.currentFrame = null; - buffer.mark(); + ((Buffer)buffer).mark(); } readingState = false; } else if( readingState ) { // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow @@ -193,7 +194,7 @@ public ByteBuffer createBuffer() { } public ByteBuffer increaseBuffer( ByteBuffer full ) throws LimitExedeedException , InvalidDataException { - full.flip(); + ((Buffer)full).flip(); ByteBuffer newbuffer = ByteBuffer.allocate( checkAlloc( full.capacity() * 2 ) ); newbuffer.put( full ); return newbuffer; diff --git a/src/main/java/org/java_websocket/drafts/Draft_76.java b/src/main/java/org/java_websocket/drafts/Draft_76.java index 26f23531e..adc6c74aa 100644 --- a/src/main/java/org/java_websocket/drafts/Draft_76.java +++ b/src/main/java/org/java_websocket/drafts/Draft_76.java @@ -1,5 +1,6 @@ package org.java_websocket.drafts; +import java.nio.Buffer; import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.security.MessageDigest; @@ -28,9 +29,9 @@ public class Draft_76 extends Draft_75 { private boolean failed = false; private static final byte[] closehandshake = { -1, 0 }; - + private final Random reuseableRandom = new Random(); - + public static byte[] createChallenge( String key1, String key2, byte[] key3 ) throws InvalidHandshakeException { byte[] part1 = getPart( key1 ); @@ -191,10 +192,10 @@ public Handshakedata translateHandshake( ByteBuffer buf ) throws InvalidHandshak @Override public List translateFrame( ByteBuffer buffer ) throws InvalidDataException { - buffer.mark(); + ((Buffer)buffer).mark(); List frames = super.translateRegularFrame( buffer ); if( frames == null ) { - buffer.reset(); + ((Buffer)buffer).reset(); frames = readyframes; readingState = true; if( currentFrame == null ) diff --git a/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java b/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java index fee1b540d..3e8cbfb5a 100644 --- a/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java +++ b/src/main/java/org/java_websocket/framing/CloseFrameBuilder.java @@ -1,5 +1,6 @@ package org.java_websocket.framing; +import java.nio.Buffer; import java.nio.ByteBuffer; import org.java_websocket.exceptions.InvalidDataException; @@ -49,30 +50,30 @@ private void setCodeAndMessage( int code, String m ) throws InvalidDataException byte[] by = Charsetfunctions.utf8Bytes( m ); ByteBuffer buf = ByteBuffer.allocate( 4 ); buf.putInt( code ); - buf.position( 2 ); + ((Buffer)buf).position( 2 ); ByteBuffer pay = ByteBuffer.allocate( 2 + by.length ); pay.put( buf ); pay.put( by ); - pay.rewind(); + ((Buffer)pay).rewind(); setPayload( pay ); } private void initCloseCode() throws InvalidFrameException { code = CloseFrame.NOCODE; ByteBuffer payload = super.getPayloadData(); - payload.mark(); + ((Buffer)payload).mark(); if( payload.remaining() >= 2 ) { ByteBuffer bb = ByteBuffer.allocate( 4 ); - bb.position( 2 ); + ((Buffer)bb).position( 2 ); bb.putShort( payload.getShort() ); - bb.position( 0 ); + ((Buffer)bb).position( 0 ); code = bb.getInt(); if( code == CloseFrame.ABNORMAL_CLOSE || code == CloseFrame.TLS_ERROR || code == CloseFrame.NOCODE || code > 4999 || code < 1000 || code == 1004 ) { throw new InvalidFrameException( "closecode must not be sent over the wire: " + code ); } } - payload.reset(); + ((Buffer)payload).reset(); } @Override @@ -87,12 +88,12 @@ private void initMessage() throws InvalidDataException { ByteBuffer b = super.getPayloadData(); int mark = b.position();// because stringUtf8 also creates a mark try { - b.position( b.position() + 2 ); + ((Buffer)b).position( b.position() + 2 ); reason = Charsetfunctions.stringUtf8( b ); } catch ( IllegalArgumentException e ) { throw new InvalidFrameException( e ); } finally { - b.position( mark ); + ((Buffer)b).position( mark ); } } } diff --git a/src/main/java/org/java_websocket/framing/FramedataImpl1.java b/src/main/java/org/java_websocket/framing/FramedataImpl1.java index 5fba075b4..b35863f1a 100644 --- a/src/main/java/org/java_websocket/framing/FramedataImpl1.java +++ b/src/main/java/org/java_websocket/framing/FramedataImpl1.java @@ -1,5 +1,6 @@ package org.java_websocket.framing; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.util.Arrays; @@ -78,17 +79,17 @@ public void append( Framedata nextframe ) throws InvalidFrameException { ByteBuffer b = nextframe.getPayloadData(); if( unmaskedpayload == null ) { unmaskedpayload = ByteBuffer.allocate( b.remaining() ); - b.mark(); + ((Buffer)b).mark(); unmaskedpayload.put( b ); - b.reset(); + ((Buffer)b).reset(); } else { - b.mark(); - unmaskedpayload.position( unmaskedpayload.limit() ); - unmaskedpayload.limit( unmaskedpayload.capacity() ); + ((Buffer)b).mark(); + ((Buffer)unmaskedpayload).position( unmaskedpayload.limit() ); + ((Buffer)unmaskedpayload).limit( unmaskedpayload.capacity() ); if( b.remaining() > unmaskedpayload.remaining() ) { ByteBuffer tmp = ByteBuffer.allocate( b.remaining() + unmaskedpayload.capacity() ); - unmaskedpayload.flip(); + ((Buffer)unmaskedpayload).flip(); tmp.put( unmaskedpayload ); tmp.put( b ); unmaskedpayload = tmp; @@ -96,8 +97,8 @@ public void append( Framedata nextframe ) throws InvalidFrameException { } else { unmaskedpayload.put( b ); } - unmaskedpayload.rewind(); - b.reset(); + ((Buffer)unmaskedpayload).rewind(); + ((Buffer)b).reset(); } fin = nextframe.isFin(); } diff --git a/src/main/java/org/java_websocket/util/Charsetfunctions.java b/src/main/java/org/java_websocket/util/Charsetfunctions.java index bd8ad2997..c1bb1ae48 100644 --- a/src/main/java/org/java_websocket/util/Charsetfunctions.java +++ b/src/main/java/org/java_websocket/util/Charsetfunctions.java @@ -1,6 +1,7 @@ package org.java_websocket.util; import java.io.UnsupportedEncodingException; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; @@ -39,7 +40,7 @@ public static byte[] asciiBytes( String s ) { public static String stringAscii( byte[] bytes ) { return stringAscii( bytes, 0, bytes.length ); } - + public static String stringAscii( byte[] bytes, int offset, int length ){ try { return new String( bytes, offset, length, "ASCII" ); @@ -73,9 +74,9 @@ public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException // decode.replaceWith( "X" ); String s; try { - bytes.mark(); + ((Buffer)bytes).mark(); s = decode.decode( bytes ).toString(); - bytes.reset(); + ((Buffer)bytes).reset(); } catch ( CharacterCodingException e ) { throw new InvalidDataException( CloseFrame.NO_UTF8, e ); }