Varint encode as signed integer.

@param string $buf bytestring to hold varint encoding of $x.
@param int $x the signed integer to varint encode.
@return int number of bytes written to buf.
423675
public static function encodeSigned(string &$buf, int $x): int
    {
        $ux = $x << 1;
        if ($x < 0) {
            $ux = ~$ux;
        }
        return self::encodeUnsigned($buf, $ux);
    }
694856
int getInt(int bp) {
        return
            ((buf[bp] & 0xFF) << 24) +
            ((buf[bp+1] & 0xFF) << 16) +
            ((buf[bp+2] & 0xFF) << 8) +
            (buf[bp+3] & 0xFF);
    }
909000
public static int getInt(ByteBuffer buf, int index) {
        return buf.order() == BIG_ENDIAN ? getIntB(buf, index) : getIntL(buf, index);
    }
191983
function writeVarInt (buffer, number, offset) {
  varuint.encode(number, buffer, offset);
  return varuint.encode.bytes;
}
67339
public static long getVarInt(byte[] varInt) {
	long result=0;
	if (varInt.length==0) {
		 return result;
	}
	int unsignedByte=varInt[0] & 0xFF;
	if (unsignedByte<0xFD) {
		return unsignedByte;
	}
	int intSize=0;
	if (unsignedByte==0xFD) { 
		intSize=3;
	}
	else if (unsignedByte==0xFE) {
		intSize=5;
	}
	else if (unsignedByte==0XFF) {
		intSize=9;
	}
	byte[] rawDataInt=reverseByteArray(Arrays.copyOfRange(varInt, 1, intSize));
	ByteBuffer byteBuffer = ByteBuffer.wrap(rawDataInt);
	if (intSize==3) {
		 result=byteBuffer.getShort();
	}
	else if (intSize==5) {
		result=byteBuffer.getInt();
	}
	else if (intSize==9) {
		result=byteBuffer.getLong(); // Need to handle sign - available only in JDK8
	}
	return result;
}
1162460
public void encodeSelf(ObjectOutput buf) throws IOException {
    buf.writeByte((byte) type);
    switch (type) {
    case STRING:
      buf.writeUTF((String) value);
      return;
    case LONG:
      buf.writeLong(((Long) value).longValue()); // was NumericValue
      return;
    case DOUBLE:
      buf.writeDouble(((Double) value).doubleValue()); // was NumericValue
      return;
    case INT:
      buf.writeInt(((Integer) value).intValue()); // was NumericValue
      return;
    case FLOAT:
      buf.writeFloat(((Float) value).floatValue()); // was NumericValue
      return;
    case BOOLEAN:
      buf.writeBoolean(((Boolean) value).booleanValue()); // was BooleanValue
      return;
    case OBJECT:
      buf.writeObject(value);
    default:
      throw new IllegalStateException();
    }
  }
92393
public static function size_varint($i) {
/*		$len = 0;
		do {
			$i = $i >> 7;
			$len++;
		} while ($i != 0);
		return $len;
*/
		// TODO Change to a binary search
		if ($i < 0x80)
			return 1;
		if ($i < 0x4000)
			return 2;
		if ($i < 0x200000)
			return 3;
		if ($i < 0x10000000)
			return 4;
		if ($i < 0x800000000)
			return 5;
		if ($i < 0x40000000000)
			return 6;
		if ($i < 0x2000000000000)
			return 7;
		if ($i < 0x100000000000000)
			return 8;
		if ($i < 0x8000000000000000)
			return 9;
	}
977206
def encode_varint(v, f):
    
    assert v >= 0
    num_bytes = 0

    while True:
        b = v % 0x80
        v = v // 0x80

        if v > 0:
            b = b | 0x80

        f.write(FIELD_U8.pack(b))

        num_bytes += 1
        if v == 0:
            break

    return num_bytes
815920
def _parse_signed_int_components(buf):
    
    sign_bit = 0
    value = 0

    first = True
    while True:
        ch = buf.read(1)
        if ch == b'':
            break
        octet = ord(ch)
        if first:
            if octet & _SIGNED_INT_SIGN_MASK:
                sign_bit = 1
            value = octet & _SIGNED_INT_SIGN_VALUE_MASK
            first = False
        else:
            value <<= 8
            value |= octet

    return sign_bit, value
818453
public function writeBytes(string $buf, int $num = null): int
    {
        unset($buf);
        unset($num);
        throw new FileAccessDenied(
            'This is a read-only file handle.'
        );
    }
270830
private function parseSignedInt32()
    {
        try {
            $result = \unpack('N', $this->buffer)[1];

            if ($result & 0x80000000) {
                return $result - 0x100000000;
            }

            return $result;
        } finally {
            $this->buffer = mb_orig_substr($this->buffer, 4);
        }
    }
1065796
static int getUnsignedShort(ByteBuf buf, int offset) {
        return (buf.getByte(offset)     & 0xFF) << 8 |
                buf.getByte(offset + 1) & 0xFF;
    }
677877
public function get8() {
    if ($this->idx >= $this->limit) {
      throw new ProtocolBufferDecodeError("truncated");
    }
    $c = unpack("C*", substr($this->buf, $this->idx, 1));
    $this->idx += 1;
    return $c[1];
  }
330337
function readVarInt (buffer, offset) {
  const result = varuint.decode(buffer, offset);

  return {
    number: result,
    size: varuint.decode.bytes
  };
}
678795
public static int encodeOctetString(short[] octets, ByteBuffer buf) {
        if (octets == null) {
            octets = new short[0];
        }
        int pos = buf.position();
        for (int i = octets.length - 1; i >= 0; i--) {
            pos--;
            buf.put(pos, (byte) octets[i]);
        }
        buf.position(buf.position() - octets.length);
        int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE,
                ASN1_OCTET_STRING_TAG_NUM, octets.length, buf);
        return headerLength + octets.length;
    }
1228532
private static void encodeLELong(long val, byte[] buf, int off)
	{
		buf[off + 0] = (byte)val;
		buf[off + 1] = (byte)(val >>> 8);
		buf[off + 2] = (byte)(val >>> 16);
		buf[off + 3] = (byte)(val >>> 24);
		buf[off + 4] = (byte)(val >>> 32);
		buf[off + 5] = (byte)(val >>> 40);
		buf[off + 6] = (byte)(val >>> 48);
		buf[off + 7] = (byte)(val >>> 56);
	}
147060
public static void writeInt(ByteBuffer buf, int pos, int v) {
    buf.put(pos++, (byte) (0xff & (v >> 24)));
    buf.put(pos++, (byte) (0xff & (v >> 16)));
    buf.put(pos++, (byte) (0xff & (v >> 8)));
    buf.put(pos, (byte) (0xff & v));
  }