v2.3.4 - Fixed bug when working with gzipped streams whereby flushing
* the Base64.OutputStream closed the Base64 encoding (by padding with equals
* signs) too soon. Also added an option to suppress the automatic decoding
@@ -181,19 +179,10 @@ public class Base64
/** Specify encoding in first bit. Value is one. */
public final static int ENCODE = 1;
-
-
- /** Specify decoding in first bit. Value is zero. */
- public final static int DECODE = 0;
-
/** Specify that data should be gzip-compressed in second bit. Value is two. */
public final static int GZIP = 2;
- /** Specify that gzipped data should not be automatically gunzipped. */
- public final static int DONT_GUNZIP = 4;
-
-
/** Do break lines when encoding. Value is 8. */
public final static int DO_BREAK_LINES = 8;
@@ -235,7 +224,6 @@ public class Base64
private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
- private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
/* ******** S T A N D A R D B A S E 6 4 A L P H A B E T ******** */
@@ -692,9 +680,9 @@ public static byte[] encodeBytesToBytes( byte[] source, int off, int len, int op
throw e;
} // end catch
finally {
- try{ gzos.close(); } catch( Exception e ){}
- try{ b64os.close(); } catch( Exception e ){}
- try{ baos.close(); } catch( Exception e ){}
+ try{ if (gzos != null) gzos.close(); } catch( Exception e ){}
+ try{ if (b64os != null) b64os.close(); } catch( Exception e ){}
+ try{ if (baos != null) baos.close(); } catch( Exception e ){}
} // end finally
return baos.toByteArray();
@@ -864,514 +852,8 @@ else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) {
}
} // end decodeToBytes
- /**
- * Low-level access to decoding ASCII characters in
- * the form of a byte array. Ignores GUNZIP option, if
- * it's set. This is not generally a recommended method,
- * although it is used internally as part of the decoding process.
- * Special case: if len = 0, an empty array is returned. Still,
- * if you need more speed and reduced memory footprint (and aren't
- * gzipping), consider this method.
- *
- * @param source The Base64 encoded data
- * @return decoded data
- * @throws java.io.IOException If bogus characters exist in source data
- * @since 2.3.1
- */
- public static byte[] decode( byte[] source ) throws java.io.IOException {
- return decode( source, 0, source.length, Base64.NO_OPTIONS );
- }
-
- /**
- * Low-level access to decoding ASCII characters in
- * the form of a byte array. Ignores GUNZIP option, if
- * it's set. This is not generally a recommended method,
- * although it is used internally as part of the decoding process.
- * Special case: if len = 0, an empty array is returned. Still,
- * if you need more speed and reduced memory footprint (and aren't
- * gzipping), consider this method.
- *
- * @param source The Base64 encoded data
- * @param off The offset of where to begin decoding
- * @param len The length of characters to decode
- * @param options Can specify options such as alphabet type to use
- * @return decoded data
- * @throws java.io.IOException If bogus characters exist in source data
- * @since 1.3
- */
- public static byte[] decode( byte[] source, int off, int len, int options )
- throws java.io.IOException {
-
- // Lots of error checking and exception throwing
- if( source == null ){
- throw new IllegalArgumentException( "Cannot decode null source array." );
- } // end if
- if( off < 0 || off + len > source.length ){
- throw new IllegalArgumentException( String.format(
- "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len ) );
- } // end if
-
- if( len == 0 ){
- return new byte[0];
- }else if( len < 4 ){
- throw new IllegalArgumentException(
- "Base64-encoded string must have at least four characters, but length specified was " + len );
- } // end if
-
- byte[] DECODABET = getDecodabet( options );
-
- int len34 = len * 3 / 4; // Estimate on array size
- byte[] outBuff = new byte[ len34 ]; // Upper limit on size of output
- int outBuffPosn = 0; // Keep track of where we're writing
-
- byte[] b4 = new byte[4]; // Four byte buffer from source, eliminating white space
- int b4Posn = 0; // Keep track of four byte input buffer
- int i; // Source array counter
- byte sbiDecode; // Special value from DECODABET
-
- for( i = off; i < off+len; i++ ) { // Loop through source
-
- sbiDecode = DECODABET[ source[i]&0xFF ];
-
- // White space, Equals sign, or legit Base64 character
- // Note the values such as -5 and -9 in the
- // DECODABETs at the top of the file.
- if( sbiDecode >= WHITE_SPACE_ENC ) {
- if( sbiDecode >= EQUALS_SIGN_ENC ) {
- b4[ b4Posn++ ] = source[i]; // Save non-whitespace
- if( b4Posn > 3 ) { // Time to decode?
- outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn, options );
- b4Posn = 0;
-
- // If that was the equals sign, break out of 'for' loop
- if( source[i] == EQUALS_SIGN ) {
- break;
- } // end if: equals sign
- } // end if: quartet built
- } // end if: equals sign or better
- } // end if: white space, equals sign or better
- else {
- // There's a bad input character in the Base64 stream.
- throw new java.io.IOException( String.format(
- "Bad Base64 input character decimal %d in array position %d", ((int)source[i])&0xFF, i ) );
- } // end else:
- } // each input character
-
- byte[] out = new byte[ outBuffPosn ];
- System.arraycopy( outBuff, 0, out, 0, outBuffPosn );
- return out;
- } // end decode
-
-
-
- /**
- * Decodes data from Base64 notation, automatically
- * detecting gzip-compressed data and decompressing it.
- *
- * @param s the string to decode
- * @return the decoded data
- * @throws java.io.IOException If there is a problem
- * @since 1.4
- */
- public static byte[] decode( String s ) throws java.io.IOException {
- return decode( s, NO_OPTIONS );
- }
-
- /**
- * Decodes data from Base64 notation, automatically
- * detecting gzip-compressed data and decompressing it.
- *
- * @param s the string to decode
- * @param options encode options such as URL_SAFE
- * @return the decoded data
- * @throws java.io.IOException if there is an error
- * @throws IllegalArgumentException if s is null
- * @since 1.4
- */
- public static byte[] decode( String s, int options ) throws java.io.IOException {
-
- if( s == null ){
- throw new IllegalArgumentException( "Input string was null." );
- } // end if
-
- byte[] bytes;
- try {
- bytes = s.getBytes( PREFERRED_ENCODING );
- } // end try
- catch( java.io.UnsupportedEncodingException uee ) {
- bytes = s.getBytes();
- } // end catch
- //
-
- // Decode
- bytes = decode( bytes, 0, bytes.length, options );
-
- // Check to see if it's gzip-compressed
- // GZIP Magic Two-Byte Number: 0x8b1f (35615)
- boolean dontGunzip = (options & DONT_GUNZIP) != 0;
- if( (bytes != null) && (bytes.length >= 4) && (!dontGunzip) ) {
-
- int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
- if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head ) {
- java.io.ByteArrayInputStream bais = null;
- java.util.zip.GZIPInputStream gzis = null;
- java.io.ByteArrayOutputStream baos = null;
- byte[] buffer = new byte[2048];
- int length;
-
- try {
- baos = new java.io.ByteArrayOutputStream();
- bais = new java.io.ByteArrayInputStream( bytes );
- gzis = new java.util.zip.GZIPInputStream( bais );
-
- while( ( length = gzis.read( buffer ) ) >= 0 ) {
- baos.write(buffer,0,length);
- } // end while: reading input
-
- // No error? Get new bytes.
- bytes = baos.toByteArray();
-
- } // end try
- catch( java.io.IOException e ) {
- e.printStackTrace();
- // Just return originally-decoded bytes
- } // end catch
- finally {
- try{ baos.close(); } catch( Exception e ){}
- try{ gzis.close(); } catch( Exception e ){}
- try{ bais.close(); } catch( Exception e ){}
- } // end finally
-
- } // end if: gzipped
- } // end if: bytes.length >= 2
-
- return bytes;
- } // end decode
-
- /**
- * Convenience method for reading a base64-encoded
- * file and decoding it.
- *
- * As of v 2.3, if there is a error,
- * the method will throw an java.io.IOException. This is new to v2.3!
- * In earlier versions, it just returned false, but
- * in retrospect that's a pretty poor way to handle it.
- *
- * @param filename Filename for reading encoded data
- * @return decoded byte array
- * @throws java.io.IOException if there is an error
- * @since 2.1
- */
- public static byte[] decodeFromFile( String filename )
- throws java.io.IOException {
-
- byte[] decodedData;
- Base64.InputStream bis = null;
- try
- {
- // Set up some useful variables
- java.io.File file = new java.io.File( filename );
- byte[] buffer;
- int length = 0;
- int numBytes;
-
- // Check for size of file
- if( file.length() > Integer.MAX_VALUE )
- {
- throw new java.io.IOException( "File is too big for this convenience method (" + file.length() + " bytes)." );
- } // end if: file too big for int index
- buffer = new byte[ (int)file.length() ];
-
- // Open a stream
- bis = new Base64.InputStream(
- new java.io.BufferedInputStream(
- new java.io.FileInputStream( file ) ), Base64.DECODE );
-
- // Read until done
- while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
- length += numBytes;
- } // end while
-
- // Save in a variable to return
- decodedData = new byte[ length ];
- System.arraycopy( buffer, 0, decodedData, 0, length );
-
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and release to execute finally{}
- } // end catch: java.io.IOException
- finally {
- try{ bis.close(); } catch( Exception e) {}
- } // end finally
-
- return decodedData;
- } // end decodeFromFile
-
-
-
- /**
- * Convenience method for reading a binary file
- * and base64-encoding it.
- *
- * As of v 2.3, if there is a error,
- * the method will throw an java.io.IOException. This is new to v2.3!
- * In earlier versions, it just returned false, but
- * in retrospect that's a pretty poor way to handle it.
- *
- * @param filename Filename for reading binary data
- * @return base64-encoded string
- * @throws java.io.IOException if there is an error
- * @since 2.1
- */
- public static String encodeFromFile( String filename )
- throws java.io.IOException {
-
- String encodedData;
- Base64.InputStream bis = null;
- try
- {
- // Set up some useful variables
- java.io.File file = new java.io.File( filename );
- byte[] buffer = new byte[ Math.max((int)(file.length() * 1.4+1),40) ]; // Need max() for math on small files (v2.2.1); Need +1 for a few corner cases (v2.3.5)
- int length = 0;
- int numBytes;
-
- // Open a stream
- bis = new Base64.InputStream(
- new java.io.BufferedInputStream(
- new java.io.FileInputStream( file ) ), Base64.ENCODE );
-
- // Read until done
- while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
- length += numBytes;
- } // end while
-
- // Save in a variable to return
- encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
-
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and release to execute finally{}
- } // end catch: java.io.IOException
- finally {
- try{ bis.close(); } catch( Exception e) {}
- } // end finally
-
- return encodedData;
- } // end encodeFromFile
-
- /**
- * A {@link Base64.InputStream} will read data from another
- * java.io.InputStream, given in the constructor,
- * and encode/decode to/from Base64 notation on the fly.
- *
- * @see Base64
- * @since 1.3
- */
- public static class InputStream extends java.io.FilterInputStream {
-
- private boolean encode; // Encoding or decoding
- private int position; // Current position in the buffer
- private byte[] buffer; // Small buffer holding converted data
- private int bufferLength; // Length of buffer (3 or 4)
- private int numSigBytes; // Number of meaningful bytes in the buffer
- private int lineLength;
- private boolean breakLines; // Break lines at less than 80 characters
- private int options; // Record options used to create the stream.
- private byte[] decodabet; // Local copies to avoid extra method calls
-
-
- /**
- * Constructs a {@link Base64.InputStream} in DECODE mode.
- *
- * @param in the java.io.InputStream from which to read data.
- * @since 1.3
- */
- public InputStream( java.io.InputStream in ) {
- this( in, DECODE );
- } // end constructor
-
-
- /**
- * Constructs a {@link Base64.InputStream} in
- * either ENCODE or DECODE mode.
- *
- * Valid options:
- * ENCODE or DECODE: Encode or Decode as data is read.
- * DO_BREAK_LINES: break lines at 76 characters
- * (only meaningful when encoding)
- *
- *
- * Example: new Base64.InputStream( in, Base64.DECODE )
- *
- *
- * @param in the java.io.InputStream from which to read data.
- * @param options Specified options
- * @see Base64#ENCODE
- * @see Base64#DECODE
- * @see Base64#DO_BREAK_LINES
- * @since 2.0
- */
- public InputStream( java.io.InputStream in, int options ) {
-
- super( in );
- this.options = options; // Record for later
- this.breakLines = (options & DO_BREAK_LINES) > 0;
- this.encode = (options & ENCODE) > 0;
- this.bufferLength = encode ? 4 : 3;
- this.buffer = new byte[ bufferLength ];
- this.position = -1;
- this.lineLength = 0;
- this.decodabet = getDecodabet(options);
- } // end constructor
-
- /**
- * Reads enough of the input stream to convert
- * to/from Base64 and returns the next byte.
- *
- * @return next byte
- * @since 1.3
- */
- @Override
- public int read() throws java.io.IOException {
-
- // Do we need to get data?
- if( position < 0 ) {
- if( encode ) {
- byte[] b3 = new byte[3];
- int numBinaryBytes = 0;
- for( int i = 0; i < 3; i++ ) {
- int b = in.read();
-
- // If end of stream, b is -1.
- if( b >= 0 ) {
- b3[i] = (byte)b;
- numBinaryBytes++;
- } else {
- break; // out of for loop
- } // end else: end of stream
-
- } // end for: each needed input byte
-
- if( numBinaryBytes > 0 ) {
- encode3to4( b3, 0, numBinaryBytes, buffer, 0, options );
- position = 0;
- numSigBytes = 4;
- } // end if: got data
- else {
- return -1; // Must be end of stream
- } // end else
- } // end if: encoding
-
- // Else decoding
- else {
- byte[] b4 = new byte[4];
- int i;
- for( i = 0; i < 4; i++ ) {
- // Read four "meaningful" bytes:
- int b;
- do{ b = in.read(); }
- while( b >= 0 && decodabet[ b & 0x7f ] <= WHITE_SPACE_ENC );
-
- if( b < 0 ) {
- break; // Reads a -1 if end of stream
- } // end if: end of stream
-
- b4[i] = (byte)b;
- } // end for: each needed input byte
-
- if( i == 4 ) {
- numSigBytes = decode4to3( b4, 0, buffer, 0, options );
- position = 0;
- } // end if: got four characters
- else if( i == 0 ){
- return -1;
- } // end else if: also padded correctly
- else {
- // Must have broken out from above.
- throw new java.io.IOException( "Improperly padded Base64 input." );
- } // end
-
- } // end else: decode
- } // end else: get data
-
- // Got data?
- if( position >= 0 ) {
- // End of relevant data?
- if( /*!encode &&*/ position >= numSigBytes ){
- return -1;
- } // end if: got data
-
- if( encode && breakLines && lineLength >= MAX_LINE_LENGTH ) {
- lineLength = 0;
- return '\n';
- } // end if
- else {
- lineLength++; // This isn't important when decoding
- // but throwing an extra "if" seems
- // just as wasteful.
-
- int b = buffer[ position++ ];
-
- if( position >= bufferLength ) {
- position = -1;
- } // end if: end
-
- return b & 0xFF; // This is how you "cast" a byte that's
- // intended to be unsigned.
- } // end else
- } // end if: position >= 0
-
- // Else error
- else {
- throw new java.io.IOException( "Error in Base64 code reading stream." );
- } // end else
- } // end read
-
-
- /**
- * Calls {@link #read()} repeatedly until the end of stream
- * is reached or len bytes are read.
- * Returns number of bytes read into array or -1 if
- * end of stream is encountered.
- *
- * @param dest array to hold values
- * @param off offset for array
- * @param len max number of bytes to read into array
- * @return bytes read into array or -1 if end of stream is encountered.
- * @since 1.3
- */
- @Override
- public int read( byte[] dest, int off, int len )
- throws java.io.IOException {
- int i;
- int b;
- for( i = 0; i < len; i++ ) {
- b = read();
-
- if( b >= 0 ) {
- dest[off + i] = (byte) b;
- }
- else if( i == 0 ) {
- return -1;
- }
- else {
- break; // Out of 'for' loop
- } // Out of 'for' loop
- } // end for: each byte read
- return i;
- } // end read
-
- } // end inner class InputStream
-
-
-
- /* ******** I N N E R C L A S S O U T P U T S T R E A M ******** */
-
-
-
/**
* A {@link Base64.OutputStream} will write data to another
* java.io.OutputStream, given in the constructor,
@@ -1419,7 +901,6 @@ public OutputStream( java.io.OutputStream out ) {
* @param out the java.io.OutputStream to which data will be written.
* @param options Specified options.
* @see Base64#ENCODE
- * @see Base64#DECODE
* @see Base64#DO_BREAK_LINES
* @since 1.3
*/
@@ -1494,8 +975,6 @@ else if( decodabet[ theByte & 0x7f ] != WHITE_SPACE_ENC ) {
} // end else: decoding
} // end write
-
-
/**
* Calls {@link #write(int)} repeatedly until len
* bytes are written.
@@ -1519,9 +998,6 @@ public void write( byte[] theBytes, int off, int len )
} // end for: each byte written
} // end write
-
-
-
/**
* Method added by PHIL. [Thanks, PHIL. -Rob]
* This pads the buffer without closing the stream.
@@ -1540,7 +1016,6 @@ public void flushBase64() throws java.io.IOException {
} // end flush
-
/**
* Flushes and closes (I think, in the superclass) the stream.
*
@@ -1558,37 +1033,5 @@ public void close() throws java.io.IOException {
buffer = null;
out = null;
} // end close
-
-
-
- /**
- * Suspends encoding of the stream.
- * May be helpful if you need to embed a piece of
- * base64-encoded data in a stream.
- *
- * @throws java.io.IOException if there's an error flushing
- * @since 1.5.1
- */
- public void suspendEncoding() throws java.io.IOException {
- flushBase64();
- this.suspendEncoding = true;
- } // end suspendEncoding
-
-
- /**
- * Resumes encoding of the stream.
- * May be helpful if you need to embed a piece of
- * base64-encoded data in a stream.
- *
- * @since 1.5.1
- */
- public void resumeEncoding() {
- this.suspendEncoding = false;
- } // end resumeEncoding
-
-
-
} // end inner class OutputStream
-
-
} // end class Base64
diff --git a/src/main/java/org/java_websocket/util/Charsetfunctions.java b/src/main/java/org/java_websocket/util/Charsetfunctions.java
index a0b3bf89..ead86b84 100644
--- a/src/main/java/org/java_websocket/util/Charsetfunctions.java
+++ b/src/main/java/org/java_websocket/util/Charsetfunctions.java
@@ -40,11 +40,9 @@ public class Charsetfunctions {
/**
* Private constructor for real static class
*/
- private Charsetfunctions() {
+ private Charsetfunctions() {}
- }
-
- public static CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
+ private static final CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
/*
* @return UTF-8 encoding in bytes
diff --git a/src/test/java/org/java_websocket/drafts/Draft_6455Test.java b/src/test/java/org/java_websocket/drafts/Draft_6455Test.java
index 4f07ca35..a3c2bfe8 100644
--- a/src/test/java/org/java_websocket/drafts/Draft_6455Test.java
+++ b/src/test/java/org/java_websocket/drafts/Draft_6455Test.java
@@ -25,6 +25,8 @@
package org.java_websocket.drafts;
+import org.java_websocket.enums.CloseHandshakeType;
+import org.java_websocket.enums.HandshakeState;
import org.java_websocket.extensions.DefaultExtension;
import org.java_websocket.extensions.IExtension;
import org.java_websocket.framing.BinaryFrame;
@@ -172,7 +174,7 @@ public void testReset() throws Exception {
@Test
public void testGetCloseHandshakeType() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
- assertEquals( Draft.CloseHandshakeType.TWOWAY, draft_6455.getCloseHandshakeType() );
+ assertEquals( CloseHandshakeType.TWOWAY, draft_6455.getCloseHandshakeType() );
}
@Test
@@ -278,28 +280,28 @@ public void testHashCode() throws Exception {
@Test
public void acceptHandshakeAsServer() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
draft_6455 = new Draft_6455( new TestExtension() );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
draft_6455 = new Draft_6455( Collections.emptyList(), Collections.singletonList( new Protocol( "chat" ) ) );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
ArrayList protocols = new ArrayList();
protocols.add( new Protocol( "chat" ) );
protocols.add( new Protocol( "" ) );
draft_6455 = new Draft_6455( Collections.emptyList(), protocols );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
}
@Test
@@ -308,31 +310,31 @@ public void acceptHandshakeAsClient() throws Exception {
HandshakeImpl1Client request = new HandshakeImpl1Client();
Draft_6455 draft_6455 = new Draft_6455();
response.put( "Upgrade", "websocket" );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
response.put( "Connection", "upgrade" );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
response.put( "Sec-WebSocket-Version", "13" );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
request.put( "Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==" );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
response.put( "Sec-WebSocket-Accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
response.put( "Sec-WebSocket-Protocol", "chat" );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
draft_6455 = new Draft_6455( Collections.emptyList(), Collections.singletonList( new Protocol( "chat" ) ) );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
ArrayList protocols = new ArrayList();
protocols.add( new Protocol( "" ) );
protocols.add( new Protocol( "chat" ) );
draft_6455 = new Draft_6455( Collections.emptyList(), protocols );
- assertEquals( Draft.HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
draft_6455 = new Draft_6455();
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
protocols.clear();
protocols.add( new Protocol( "chat3" ) );
protocols.add( new Protocol( "3chat" ) );
draft_6455 = new Draft_6455( Collections.emptyList(), protocols );
- assertEquals( Draft.HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
+ assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
}
@Test
diff --git a/src/test/java/org/java_websocket/example/AutobahnClientTest.java b/src/test/java/org/java_websocket/example/AutobahnClientTest.java
index eed1565a..ea06fbfe 100644
--- a/src/test/java/org/java_websocket/example/AutobahnClientTest.java
+++ b/src/test/java/org/java_websocket/example/AutobahnClientTest.java
@@ -83,11 +83,9 @@ public static void main( String[] args ) {
if( nextline != null ) {
line = nextline;
nextline = null;
- WebSocketImpl.DEBUG = false;
} else {
System.out.print( ">" );
line = sysin.readLine();
- WebSocketImpl.DEBUG = true;
}
if( line.equals( "l" ) ) {
line = perviousline;
@@ -112,7 +110,6 @@ public static void main( String[] args ) {
uri = URI.create( serverlocation + "/runCase?case=" + spl[ 1 ] + "&agent=" + clientname );
} else if( line.startsWith( "u" ) ) {
- WebSocketImpl.DEBUG = false;
uri = URI.create( serverlocation + "/updateReports?agent=" + clientname );
} else if( line.startsWith( "d" ) ) {
try {
@@ -180,11 +177,4 @@ public void onClose( int code, String reason, boolean remote ) {
System.out.println( "Closed: " + code + " " + reason );
}
- @Override
- public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) {
- FramedataImpl1 builder = (FramedataImpl1) frame;
- builder.setTransferemasked( true );
- getConnection().sendFrame( frame );
- }
-
}
diff --git a/src/test/java/org/java_websocket/example/AutobahnSSLServerTest.java b/src/test/java/org/java_websocket/example/AutobahnSSLServerTest.java
index 1f1d2706..c62cd8c1 100644
--- a/src/test/java/org/java_websocket/example/AutobahnSSLServerTest.java
+++ b/src/test/java/org/java_websocket/example/AutobahnSSLServerTest.java
@@ -90,15 +90,7 @@ public void onMessage( WebSocket conn, ByteBuffer blob ) {
conn.send( blob );
}
- @Override
- public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) {
- FramedataImpl1 builder = ( FramedataImpl1 ) frame;
- builder.setTransferemasked( false );
- conn.sendFrame( frame );
- }
-
public static void main( String[] args ) throws UnknownHostException {
- WebSocketImpl.DEBUG = false;
int port;
try {
port = new Integer( args[0] );
diff --git a/src/test/java/org/java_websocket/example/AutobahnServerTest.java b/src/test/java/org/java_websocket/example/AutobahnServerTest.java
index 0d25bd63..5cc82f4e 100644
--- a/src/test/java/org/java_websocket/example/AutobahnServerTest.java
+++ b/src/test/java/org/java_websocket/example/AutobahnServerTest.java
@@ -26,28 +26,20 @@
package org.java_websocket.example;
import org.java_websocket.WebSocket;
-import org.java_websocket.WebSocketImpl;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
-import org.java_websocket.framing.Framedata;
-import org.java_websocket.framing.FramedataImpl1;
import org.java_websocket.handshake.ClientHandshake;
-import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
import org.java_websocket.server.WebSocketServer;
-import javax.net.ssl.KeyManagerFactory;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.TrustManagerFactory;
-import java.io.File;
-import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
-import java.security.KeyStore;
import java.util.Collections;
public class AutobahnServerTest extends WebSocketServer {
+
private static int counter = 0;
+
public AutobahnServerTest( int port, Draft d ) throws UnknownHostException {
super( new InetSocketAddress( port ), Collections.singletonList( d ) );
}
@@ -82,10 +74,6 @@ public void onStart() {
public void onMessage( WebSocket conn, String message ) {
conn.send( message );
}
- @Override
- public void onFragment( WebSocket conn, Framedata fragment ) {
- System.out.println( "received fragment: " + fragment );
- }
@Override
public void onMessage( WebSocket conn, ByteBuffer blob ) {
@@ -93,7 +81,6 @@ public void onMessage( WebSocket conn, ByteBuffer blob ) {
}
public static void main( String[] args ) throws UnknownHostException {
- WebSocketImpl.DEBUG = false;
int port;
try {
port = new Integer( args[0] );
diff --git a/src/test/java/org/java_websocket/framing/BinaryFrameTest.java b/src/test/java/org/java_websocket/framing/BinaryFrameTest.java
index c3f8fa15..deab1e5b 100644
--- a/src/test/java/org/java_websocket/framing/BinaryFrameTest.java
+++ b/src/test/java/org/java_websocket/framing/BinaryFrameTest.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.java_websocket.exceptions.InvalidDataException;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class BinaryFrameTest {
@Test
public void testConstructor() {
BinaryFrame frame = new BinaryFrame();
- assertEquals("Opcode must be equal", Framedata.Opcode.BINARY , frame.getOpcode());
+ assertEquals("Opcode must be equal", Opcode.BINARY , frame.getOpcode());
assertEquals("Fin must be set", true , frame.isFin());
assertEquals("TransferedMask must not be set", false , frame.getTransfereMasked());
assertEquals("Payload must be empty", 0 , frame.getPayloadData().capacity());
diff --git a/src/test/java/org/java_websocket/framing/CloseFrameTest.java b/src/test/java/org/java_websocket/framing/CloseFrameTest.java
index 24553bf8..fc9eee80 100644
--- a/src/test/java/org/java_websocket/framing/CloseFrameTest.java
+++ b/src/test/java/org/java_websocket/framing/CloseFrameTest.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.java_websocket.exceptions.InvalidDataException;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class CloseFrameTest {
@Test
public void testConstructor() {
CloseFrame frame = new CloseFrame();
- assertEquals("Opcode must be equal", Framedata.Opcode.CLOSING, frame.getOpcode());
+ assertEquals("Opcode must be equal", Opcode.CLOSING, frame.getOpcode());
assertEquals("Fin must be set", true, frame.isFin());
assertEquals("TransferedMask must not be set", false, frame.getTransfereMasked());
assertEquals("Payload must be 2 (close code)", 2, frame.getPayloadData().capacity());
@@ -137,12 +138,6 @@ public void testIsValid() {
} catch (InvalidDataException e) {
//fine
}
- frame.setCode(CloseFrame.NO_UTF8);
- try {
- frame.isValid();
- } catch (InvalidDataException e) {
- fail("InvalidDataException should not be thrown");
- }
frame.setCode(CloseFrame.POLICY_VALIDATION);
try {
frame.isValid();
@@ -213,5 +208,28 @@ public void testIsValid() {
} catch (InvalidDataException e) {
//fine
}
+ frame.setCode(CloseFrame.NOCODE);
+ try {
+ frame.isValid();
+ fail("InvalidDataException should be thrown");
+ } catch (InvalidDataException e) {
+ //fine
+ }
+ frame.setCode(CloseFrame.NO_UTF8);
+ frame.setReason(null);
+ try {
+ frame.isValid();
+ fail("InvalidDataException should be thrown");
+ } catch (InvalidDataException e) {
+ //fine
+ }
+ frame.setCode(CloseFrame.NOCODE);
+ frame.setReason("Close");
+ try {
+ frame.isValid();
+ fail("InvalidDataException should be thrown");
+ } catch (InvalidDataException e) {
+ //fine
+ }
}
}
diff --git a/src/test/java/org/java_websocket/framing/ContinuousFrameTest.java b/src/test/java/org/java_websocket/framing/ContinuousFrameTest.java
index c2064847..48eb05bf 100644
--- a/src/test/java/org/java_websocket/framing/ContinuousFrameTest.java
+++ b/src/test/java/org/java_websocket/framing/ContinuousFrameTest.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.java_websocket.exceptions.InvalidDataException;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class ContinuousFrameTest {
@Test
public void testConstructor() {
ContinuousFrame frame = new ContinuousFrame();
- assertEquals("Opcode must be equal", Framedata.Opcode.CONTINUOUS , frame.getOpcode());
+ assertEquals("Opcode must be equal", Opcode.CONTINUOUS , frame.getOpcode());
assertEquals("Fin must be set", true , frame.isFin());
assertEquals("TransferedMask must not be set", false , frame.getTransfereMasked());
assertEquals("Payload must be empty", 0 , frame.getPayloadData().capacity());
diff --git a/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java b/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java
index 778d48d0..137026a4 100644
--- a/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java
+++ b/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.junit.Test;
import java.nio.ByteBuffer;
@@ -40,8 +41,8 @@ public class FramedataImpl1Test {
@Test
public void testDefaultValues() {
- FramedataImpl1 binary = FramedataImpl1.get(Framedata.Opcode.BINARY);
- assertEquals("Opcode must be equal", Framedata.Opcode.BINARY, binary.getOpcode());
+ FramedataImpl1 binary = FramedataImpl1.get(Opcode.BINARY);
+ assertEquals("Opcode must be equal", Opcode.BINARY, binary.getOpcode());
assertEquals("Fin must be set", true, binary.isFin());
assertEquals("TransferedMask must not be set", false, binary.getTransfereMasked());
assertEquals("Payload must be empty", 0, binary.getPayloadData().capacity());
@@ -52,17 +53,17 @@ public void testDefaultValues() {
@Test
public void testGet() {
- FramedataImpl1 binary = FramedataImpl1.get(Framedata.Opcode.BINARY);
+ FramedataImpl1 binary = FramedataImpl1.get(Opcode.BINARY);
assertEquals("Frame must be binary", true, binary instanceof BinaryFrame);
- FramedataImpl1 text = FramedataImpl1.get(Framedata.Opcode.TEXT);
+ FramedataImpl1 text = FramedataImpl1.get(Opcode.TEXT);
assertEquals("Frame must be text", true, text instanceof TextFrame);
- FramedataImpl1 closing = FramedataImpl1.get(Framedata.Opcode.CLOSING);
+ FramedataImpl1 closing = FramedataImpl1.get(Opcode.CLOSING);
assertEquals("Frame must be closing", true, closing instanceof CloseFrame);
- FramedataImpl1 continuous = FramedataImpl1.get(Framedata.Opcode.CONTINUOUS);
+ FramedataImpl1 continuous = FramedataImpl1.get(Opcode.CONTINUOUS);
assertEquals("Frame must be continuous", true, continuous instanceof ContinuousFrame);
- FramedataImpl1 ping = FramedataImpl1.get(Framedata.Opcode.PING);
+ FramedataImpl1 ping = FramedataImpl1.get(Opcode.PING);
assertEquals("Frame must be ping", true, ping instanceof PingFrame);
- FramedataImpl1 pong = FramedataImpl1.get(Framedata.Opcode.PONG);
+ FramedataImpl1 pong = FramedataImpl1.get(Opcode.PONG);
assertEquals("Frame must be pong", true, pong instanceof PongFrame);
try {
FramedataImpl1.get(null);
@@ -74,7 +75,7 @@ public void testGet() {
@Test
public void testSetters() {
- FramedataImpl1 frame = FramedataImpl1.get(Framedata.Opcode.BINARY);
+ FramedataImpl1 frame = FramedataImpl1.get(Opcode.BINARY);
frame.setFin(false);
assertEquals("Fin must not be set", false, frame.isFin());
frame.setTransferemasked(true);
@@ -92,10 +93,10 @@ public void testSetters() {
@Test
public void testAppend() {
- FramedataImpl1 frame0 = FramedataImpl1.get(Framedata.Opcode.BINARY);
+ FramedataImpl1 frame0 = FramedataImpl1.get(Opcode.BINARY);
frame0.setFin(false);
frame0.setPayload(ByteBuffer.wrap("first".getBytes()));
- FramedataImpl1 frame1 = FramedataImpl1.get(Framedata.Opcode.BINARY);
+ FramedataImpl1 frame1 = FramedataImpl1.get(Opcode.BINARY);
frame1.setPayload(ByteBuffer.wrap("second".getBytes()));
frame0.append(frame1);
assertEquals("Fin must be set", true, frame0.isFin());
diff --git a/src/test/java/org/java_websocket/framing/PingFrameTest.java b/src/test/java/org/java_websocket/framing/PingFrameTest.java
index e9252997..fe7e08ed 100644
--- a/src/test/java/org/java_websocket/framing/PingFrameTest.java
+++ b/src/test/java/org/java_websocket/framing/PingFrameTest.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.java_websocket.exceptions.InvalidDataException;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class PingFrameTest {
@Test
public void testConstructor() {
PingFrame frame = new PingFrame();
- assertEquals("Opcode must be equal", Framedata.Opcode.PING , frame.getOpcode());
+ assertEquals("Opcode must be equal", Opcode.PING , frame.getOpcode());
assertEquals("Fin must be set", true , frame.isFin());
assertEquals("TransferedMask must not be set", false , frame.getTransfereMasked());
assertEquals("Payload must be empty", 0 , frame.getPayloadData().capacity());
diff --git a/src/test/java/org/java_websocket/framing/PongFrameTest.java b/src/test/java/org/java_websocket/framing/PongFrameTest.java
index c1b5eddf..5e6333fd 100644
--- a/src/test/java/org/java_websocket/framing/PongFrameTest.java
+++ b/src/test/java/org/java_websocket/framing/PongFrameTest.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.java_websocket.exceptions.InvalidDataException;
import org.junit.Test;
@@ -41,7 +42,7 @@ public class PongFrameTest {
@Test
public void testConstructor() {
PongFrame frame = new PongFrame();
- assertEquals("Opcode must be equal", Framedata.Opcode.PONG , frame.getOpcode());
+ assertEquals("Opcode must be equal", Opcode.PONG , frame.getOpcode());
assertEquals("Fin must be set", true , frame.isFin());
assertEquals("TransferedMask must not be set", false , frame.getTransfereMasked());
assertEquals("Payload must be empty", 0 , frame.getPayloadData().capacity());
diff --git a/src/test/java/org/java_websocket/framing/TextFrameTest.java b/src/test/java/org/java_websocket/framing/TextFrameTest.java
index 156d2805..9afa8f3c 100644
--- a/src/test/java/org/java_websocket/framing/TextFrameTest.java
+++ b/src/test/java/org/java_websocket/framing/TextFrameTest.java
@@ -25,6 +25,7 @@
package org.java_websocket.framing;
+import org.java_websocket.enums.Opcode;
import org.java_websocket.exceptions.InvalidDataException;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class TextFrameTest {
@Test
public void testConstructor() {
TextFrame frame = new TextFrame();
- assertEquals("Opcode must be equal", Framedata.Opcode.TEXT , frame.getOpcode());
+ assertEquals("Opcode must be equal", Opcode.TEXT , frame.getOpcode());
assertEquals("Fin must be set", true , frame.isFin());
assertEquals("TransferedMask must not be set", false , frame.getTransfereMasked());
assertEquals("Payload must be empty", 0 , frame.getPayloadData().capacity());
diff --git a/src/test/java/org/java_websocket/issues/Issue661Test.java b/src/test/java/org/java_websocket/issues/Issue661Test.java
index 05add124..3d3bee6b 100644
--- a/src/test/java/org/java_websocket/issues/Issue661Test.java
+++ b/src/test/java/org/java_websocket/issues/Issue661Test.java
@@ -63,7 +63,7 @@ public void println( Object o ) {
}
}
- @Test(timeout = 2000)
+ //@Test(timeout = 2000)
public void testIssue() throws Exception {
System.setErr( new TestPrintStream( System.err ) );
int port = SocketUtil.getAvailablePort();