private static void appendLength(ByteBuffer buffer,int length,boolean masked){
  if (length < 0) {
    throw new IllegalArgumentException("Length cannot be negative");
  }
  byte b=(masked ? (byte)0x80 : 0x00);
  if (length > 0xFFFF) {
    buffer.put((byte)(b | 0x7F));
    buffer.put((byte)0x00);
    buffer.put((byte)0x00);
    buffer.put((byte)0x00);
    buffer.put((byte)0x00);
    buffer.put((byte)((length >> 24) & 0xFF));
    buffer.put((byte)((length >> 16) & 0xFF));
    buffer.put((byte)((length >> 8) & 0xFF));
    buffer.put((byte)(length & 0xFF));
  }
 else   if (length >= 0x7E) {
    buffer.put((byte)(b | 0x7E));
    buffer.put((byte)(length >> 8));
    buffer.put((byte)(length & 0xFF));
  }
 else {
    buffer.put((byte)(b | length));
  }
}

private static void appendLength ( ByteBuffer buffer , int length , boolean masked ) { if ( length < 0 ) { throw new IllegalArgumentException ( "Length cannot be negative" ) ; } byte b = ( masked ? ( byte ) 0x80 : 0x00 ) ; if ( length > 0xFFFF ) { buffer . put ( ( byte ) ( b | 0x7F ) ) ; buffer . put ( ( byte ) 0x00 ) ; buffer . put ( ( byte ) 0x00 ) ; buffer . put ( ( byte ) 0x00 ) ; buffer . put ( ( byte ) 0x00 ) ; buffer . put ( ( byte ) ( ( length > > 24 ) & 0xFF ) ) ; buffer . put ( ( byte ) ( ( length > > 16 ) & 0xFF ) ) ; buffer . put ( ( byte ) ( ( length > > 8 ) & 0xFF ) ) ; buffer . put ( ( byte ) ( length & 0xFF ) ) ; } else if ( length >= 0x7E ) { buffer . put ( ( byte ) ( b | 0x7E ) ) ; buffer . put ( ( byte ) ( length > > 8 ) ) ; buffer . put ( ( byte ) ( length & 0xFF ) ) ; } else { buffer . put ( ( byte ) ( b | length ) ) ; } }
appends the length of the payload to the buffer
