Encode the in buffer according to the Section 5.2. RFC 6455
779678
public static IoBuffer encodeOutgoingData(Packet packet) {
        log.debug("encode outgoing: {}", packet);
        // get the payload data
        IoBuffer data = packet.getData();
        // get the frame length based on the byte count
        int frameLen = data.limit();
        // start with frame length + 2b (header info)
        IoBuffer buffer = IoBuffer.allocate(frameLen + 2, false);
        buffer.setAutoExpand(true);
        // set the proper flags / opcode for the data
        byte frameInfo = (byte) (1 << 7);
        switch (packet.getType()) {
            case TEXT:
                if (log.isTraceEnabled()) {
                    log.trace("Encoding text frame  \r\n{}", new String(packet.getData().array()));
                }
                frameInfo = (byte) (frameInfo | 1);
                break;
            case BINARY:
                log.trace("Encoding binary frame");
                frameInfo = (byte) (frameInfo | 2);
                break;
            case CLOSE:
                frameInfo = (byte) (frameInfo | 8);
                break;
            case CONTINUATION:
                frameInfo = (byte) (frameInfo | 0);
                break;
            case PING:
                log.trace("ping out");
                frameInfo = (byte) (frameInfo | 9);
                break;
            case PONG:
                log.trace("pong out");
                frameInfo = (byte) (frameInfo | 0xa);
                break;
            default:
                break;
        }
        buffer.put(frameInfo);
        // set the frame length
        log.trace("Frame length {} ", frameLen);
        if (frameLen <= 125) {
            buffer.put((byte) ((byte) frameLen & (byte) 0x7F));
        } else if (frameLen > 125 && frameLen <= 65535) {
            buffer.put((byte) ((byte) 126 & (byte) 0x7F));
            buffer.putShort((short) frameLen);
        } else {
            buffer.put((byte) ((byte) 127 & (byte) 0x7F));
            buffer.putLong((int) frameLen);
        }
        buffer.put(data);
        buffer.flip();
        if (log.isTraceEnabled()) {
            log.trace("Encoded: {}", buffer);
        }
        return buffer;
    }
308058
function encode(buff) {
  const len = buff.byteLength;
  let off = 0;
  const stra = [];

  while (off < len) {
    let c1 = buff[off++] & 0xff;
    stra.push(BASE64_CODE[(c1 >> 2) & 0x3f]);
    c1 = (c1 & 0x03) << 4;
    if (off >= len) {
      stra.push(BASE64_CODE[c1 & 0x3f]);
      break;
    }
    let c2 = buff[off++] & 0xff;
    c1 |= (c2 >> 4) & 0x0f;
    stra.push(BASE64_CODE[c1 & 0x3f]);
    c1 = (c2 & 0x0f) << 2;
    if (off >= len) {
      stra.push(BASE64_CODE[c1 & 0x3f]);
      break;
    }
    c2 = buff[off++] & 0xff;
    c1 |= (c2 >> 6) & 0x03;
    stra.push(BASE64_CODE[c1 & 0x3f]);
    stra.push(BASE64_CODE[c2 & 0x3f]);
  }
  return stra.join('');
}
475210
def encode(self, version, padding, extension, cc, seqnum, marker, pt, ssrc, payload):
		""""""

		timestamp = int(time())
		print("timestamp: " + str(timestamp))
		self.header = bytearray(HEADER_SIZE)
		#--------------
		# TO COMPLETE
		#--------------
		# Fill the header bytearray with RTP header fields

		#RTP-version filed(V), must set to 2
		#padding(P),extension(X),number of contributing sources(CC) and marker(M) fields all set to zero in this lab

		#Because we have no other contributing sources(field CC == 0),the CSRC-field does not exist
		#Thus the length of the packet header is therefore 12 bytes


			#Above all done in ServerWorker.py

		# ...
		#header[] =

		#header[0] = version + padding + extension + cc + seqnum + marker + pt + ssrc
		self.header[0] = version << 6
		self.header[0] = self.header[0] | padding << 5
		self.header[0] = self.header[0] | extension << 4
		self.header[0] = self.header[0] | cc
		self.header[1] = marker << 7
		self.header[1] = self.header[1] | pt

		self.header[2] = seqnum >> 8
		self.header[3] = seqnum

		self.header[4] = (timestamp >> 24) & 0xFF
		self.header[5] = (timestamp >> 16) & 0xFF
		self.header[6] = (timestamp >> 8) & 0xFF
		self.header[7] = timestamp & 0xFF

		self.header[8] = ssrc >> 24
		self.header[9] = ssrc >> 16
		self.header[10] = ssrc >> 8
		self.header[11] = ssrc


		# Get the payload from the argument
		# self.payload = ...
		self.payload = payload
596445
function base64url_encode(input) {
	var buf = new Buffer(input);
    return Base64.encode(buf).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
}
1253516
function encodeHalf(f32){
  EHBuffer[0] = f32;
  var fltInt32 = EHIBuffer[0];

  var fltInt16 = (fltInt32 >> 31) << 5;
  var tmp = (fltInt32 >> 23) & 0xff;
  tmp = (tmp - 0x70) & (( (0x70 - tmp) >> 4) >> 27);
  fltInt16 = (fltInt16 | tmp) << 10;
  fltInt16 |= (fltInt32 >> 13) & 0x3ff;
  return fltInt16;
}
714624
function encode(data, ch) {
        if (!self.utfMouse) {
          if (ch === 255) return data.push(0);
          if (ch > 127) ch = 127;
          data.push(ch);
        } else {
          if (ch === 2047) return data.push(0);
          if (ch < 127) {
            data.push(ch);
          } else {
            if (ch > 2047) ch = 2047;
            data.push(0xC0 | (ch >> 6));
            data.push(0x80 | (ch & 0x3F));
          }
        }
      }
815963
@Override
    protected void encode(ByteArrayOutputStream buffer) throws IOException
    {
        buffer.write(message.getBytes(NetworkMessage.CHARSET));
    }
756446
function encode(source, label) {
  if (!Buffer.isBuffer(source)) {
    throw new TypeError('Argument `source` should be a Buffer.')
  }

  if (typeof label !== 'string') {
    throw new TypeError('Argument `label` should be a string in upper case.')
  }

  const prefix = before + ' ' + label + endline
  const suffix = after + ' ' + label + endline

  const body = source.toString('base64').replace(/(.{64})/g, '$1\r\n')

  return [prefix, body, suffix].join('\r\n')
}
1202111
function encode(buffer, extra) {
  if (!extra) extra = 0
  var blen = buffer.length
  var lenbytes = vencode(blen)
  var mb = new Buffer(extra + blen + lenbytes.length)
  for (var i = 0; i < lenbytes.length; i++) {
    mb.writeUInt8(lenbytes[i], extra + i)
  }
  buffer.copy(mb, lenbytes.length + extra, 0, blen)
  return mb
}
850878
public void encodeBuffer(byte aBuffer[], OutputStream aStream)
    throws IOException {
        ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
        encodeBuffer(inStream, aStream);
    }
604826
function bytes2base64(buffer) {
            var str = '';
            var length;
            var i;
            // ArrayBuffer
            if (buffer instanceof ArrayBuffer) {
                length = buffer.byteLength;
                var view = new DataView(buffer, 0, length);
                for (i = 0; i < length; i++) {
                    str += String.fromCharCode(view.getUint8(i, false));
                }
            }
            // Array
            else if (buffer.length) {
                length = buffer.length;
                for (i = 0; i < length; i++) {
                    str += String.fromCharCode(buffer[i]);
                }
            }

            return btoa(str);
        }
442474
def encode(self):
        
        # Call body before fist_line in case the query is changes.
        first_line = self.first_line()
        if self.body and self.wait_continue:
            self.headers['expect'] = '100-continue'
        headers = self.headers
        if self.unredirected_headers:
            headers = self.unredirected_headers.copy()
            headers.update(self.headers)
        buffer = [first_line.encode('ascii'), b'\r\n']
        buffer.extend((('%s: %s\r\n' % (name, value)).encode(CHARSET)
                      for name, value in headers.items()))
        buffer.append(b'\r\n')
        return b''.join(buffer)
560258
public static int encodeIA5String(String value, ByteBuffer buf) {
        int pos = buf.position();
        byte[] data = (value == null) ? new byte[0] : value.getBytes();
        for (int i = data.length - 1; i >= 0; i--) {
            pos--;
            buf.put(pos, data[i]);
        }
        buf.position(buf.position() - data.length);
        int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE,
                ASN1_IA5STRING_TAG_NUM, data.length, buf);
        return headerLength + data.length;
    }
1183219
function (buffer) {
        var str = '',
            uArrayVal = new Uint8Array(buffer),

            i,
            ii;

        for (i = 0, ii = uArrayVal.length; i < ii; i++) {
            str += String.fromCharCode(uArrayVal[i]);
        }

        return str;
    }
645493
private static final void encode(int tag, int length, StringBuilder buffer) {
        if (tag == PATTERN_ISO_ZONE && length >= 4) {
            throw new IllegalArgumentException("invalid ISO 8601 format: length=" + length);
        }
        if (length < 255) {
            buffer.append((char)(tag << 8 | length));
        } else {
            buffer.append((char)((tag << 8) | 0xff));
            buffer.append((char)(length >>> 16));
            buffer.append((char)(length & 0xffff));
        }
    }
1253533
public static LinkedBuffer encode(final byte[] input, int inOffset, int inLen,
            final WriteSession session, final LinkedBuffer lb) throws IOException
    {
        int outputSize = ((inLen + 2) / 3) * 4;
        session.size += outputSize;

        final int available = lb.buffer.length - lb.offset;
        if (outputSize > available)
        {
            int chunks = available / 4;
            if (chunks == 0)
            {
                // available size is less than 4
                if (outputSize > session.nextBufferSize)
                {
                    final byte[] encoded = new byte[outputSize];
                    encode(input, inOffset, inLen, encoded, 0);
                    // return a fresh buffer.
                    return new LinkedBuffer(session.nextBufferSize,
                            new LinkedBuffer(encoded, 0, outputSize, lb));
                }

                final byte[] encoded = new byte[session.nextBufferSize];
                encode(input, inOffset, inLen, encoded, 0);
                // continue with the existing byte array of the previous buffer
                return new LinkedBuffer(encoded, 0, outputSize, lb);
            }

            int inBefore = inOffset;
            final byte[] buffer = lb.buffer;
            int offset = lb.offset;
            byte b0, b1, b2;

            // process available
            while (chunks-- > 0)
            {
                b0 = input[inOffset++];
                b1 = input[inOffset++];
                b2 = input[inOffset++];
                buffer[offset++] = nibble2code[(b0 >>> 2) & 0x3f];
                buffer[offset++] = nibble2code[(b0 << 4) & 0x3f | (b1 >>> 4) & 0x0f];
                buffer[offset++] = nibble2code[(b1 << 2) & 0x3f | (b2 >>> 6) & 0x03];
                buffer[offset++] = nibble2code[b2 & 077];
            }

            inLen -= (inOffset - inBefore);

            outputSize -= (offset - lb.offset);

            lb.offset = offset;

            if (outputSize > session.nextBufferSize)
            {
                final byte[] encoded = new byte[outputSize];
                encode(input, inOffset, inLen, encoded, 0);
                // return a fresh buffer.
                return new LinkedBuffer(session.nextBufferSize,
                        new LinkedBuffer(encoded, 0, outputSize, lb));
            }

            final byte[] encoded = new byte[session.nextBufferSize];
            encode(input, inOffset, inLen, encoded, 0);
            // continue with the existing byte array of the previous buffer
            return new LinkedBuffer(encoded, 0, outputSize, lb);
        }

        encode(input, inOffset, inLen, lb.buffer, lb.offset);
        lb.offset += outputSize;

        return lb;
    }
143563
def _encode_mapping(mapping, f):
	""""""
	f.write(_TYPE_DICT)
	for key, value in sorted(mapping.items()):
		_encode_buffer(key, f)
		bencode(value, f)
	f.write(_TYPE_END)