code: public static ByteBuffer rfc6455Encode(WsMessage message, int maskValue) {
          final boolean mask = true;
  
          boolean fin = true;   // TODO continued frames?
  
          ByteBuffer buf = message.getBytes();
          
          int remaining = buf.remaining();
  
          int offset = 2 + (mask ? 4 : 0) + calculateLengthSize(remaining);
          
          ByteBuffer b = ByteBuffer.allocate(offset + remaining);
  
          int start = b.position();
  
          byte b1 = (byte) (fin ? 0x80 : 0x00);
          byte b2 = (byte) (mask ? 0x80 : 0x00);
  
          b1 = doEncodeOpcode(b1, message);
          b2 |= lenBits(remaining);
  
          b.put(b1).put(b2);
  
          doEncodeLength(b, remaining);
  
          if (mask) {
              b.putInt(maskValue);
          }
          //put message data
          b.put(buf);
          
             if ( mask ) {
                 b.position(offset);
              mask(b, maskValue);
          }
          
            b.limit(b.position());
             b.position(start);
             return b;
      }
docs: Encode WebSocket message as a single frame, with the provided masking value applied.