/*
====== These methods apply to all whole numbers with minor modifications ========
====== byte, short, int, long ======
193690
@Override
    public Long convert(Object value) {
        if (value instanceof Number) {
            double d = ((Number)value).doubleValue();
            if (Double.isNaN(d) || Math.round(d)!=d) throw new IllegalArgumentException("Not a valid long: " + value);
            return ((Number)value).longValue();
        } else if (value instanceof String) {
            return Long.parseLong((String)value);
        } else if (value instanceof Idfiable) {
            return ((Idfiable)value).longId();
        } else return null;
    }
168026
@Override public byte[] apply(byte[] data, int offset, int length) throws Exception {
        ByteArrayDataInputStream in=new ByteArrayDataInputStream(data, offset, length);
        byte command=in.readByte();
        switch(command) {
            case PUT:
                K key=Util.objectFromStream(in);
                V val=Util.objectFromStream(in);
                V old_val=map.put(key, val);
                notifyPut(key, val, old_val);
                return old_val == null? null : Util.objectToByteBuffer(old_val);
            case REMOVE:
                key=Util.objectFromStream(in);
                old_val=map.remove(key);
                notifyRemove(key, old_val);
                return old_val == null? null : Util.objectToByteBuffer(old_val);
            default:
                throw new IllegalArgumentException("command " + command + " is unknown");
        }
    }
558186
public void putLong(long i) {
        _buffer[_offset++] = (byte) (i >>> 56);
        _buffer[_offset++] = (byte) ((i >> 48) & 0xff);
        _buffer[_offset++] = (byte) ((i >> 40) & 0xff);
        _buffer[_offset++] = (byte) ((i >> 32) & 0xff);
        _buffer[_offset++] = (byte) ((i >> 24) & 0xff);
        _buffer[_offset++] = (byte) ((i >> 16) & 0xff);
        _buffer[_offset++] = (byte) ((i >> 8) & 0xff);
        _buffer[_offset++] = (byte) (i & 0xff);
    }
663759
public static byte[] toBytes(long value, int length) {
        if (length <= 0) return new byte[0];
        if (length > 8) length = 8;
        byte[] out = new byte[length];
        for (int i = length - 1; i >= 0; i--){
            out[i] = (byte)(value & 0xFFL);
            value = value >> 8;
        }
        return out;
    }
1292949
private void writeULong(int s) {
        byte b1 = (byte)((s >> 24) & 0xff);
        byte b2 = (byte)((s >> 16) & 0xff);
        byte b3 = (byte)((s >> 8) & 0xff);
        byte b4 = (byte)(s & 0xff);
        writeByte(b1);
        writeByte(b2);
        writeByte(b3);
        writeByte(b4);
    }
26252
public static long toLong(byte[] b, int off, boolean littleEndian) {
		if (littleEndian) {
			return (toInt(b, off, LITTLE_ENDIAN) & 0xFFFFFFFFL) |
					((toInt(b, off + 4, LITTLE_ENDIAN) & 0xFFFFFFFFL) << 32);
		}
		return ((toInt(b, off, BIG_ENDIAN) & 0xFFFFFFFFL) << 32) |
				(toInt(b, off + 4, BIG_ENDIAN) & 0xFFFFFFFFL);
	}
278035
public static void writeShort(byte b[], int offset, short value) {
    b[offset++] = (byte) (value >>> 8);
    b[offset] = (byte)value;
  }
1132608
@Override
  public void writeLong(long v) throws IOException {
    work[0] = (byte) (0xffL & v);
    work[1] = (byte) (0xffL & (v >> 8));
    work[2] = (byte) (0xffL & (v >> 16));
    work[3] = (byte) (0xffL & (v >> 24));
    work[4] = (byte) (0xffL & (v >> 32));
    work[5] = (byte) (0xffL & (v >> 40));
    work[6] = (byte) (0xffL & (v >> 48));
    work[7] = (byte) (0xffL & (v >> 56));
    write(work, 0, 8);
  }
19838
private static final long gatherPartialLongLE(final byte[] data, final int index, final int available) {
        if(available >= 4) {
            int i = gatherIntLE(data, index);
            long l = uintToLong(i);

            int left = available - 4;

            if(left == 0) {
                return l;
            }

            int i2 = gatherPartialIntLE(data, index + 4, left);

            l <<= (left << 3);
            l |= (long) i2;

            return l;
        } else {
            return (long) gatherPartialIntLE(data, index, available);
        }
    }
478632
final void writeLong(long value)
    {
        byte writeBuffer[] = new byte[8];
        writeBuffer[0] = (byte) (value >>> 56);
        writeBuffer[1] = (byte) (value >>> 48);
        writeBuffer[2] = (byte) (value >>> 40);
        writeBuffer[3] = (byte) (value >>> 32);
        writeBuffer[4] = (byte) (value >>> 24);
        writeBuffer[5] = (byte) (value >>> 16);
        writeBuffer[6] = (byte) (value >>> 8);
        writeBuffer[7] = (byte) (value >>> 0);
        write(writeBuffer, 0, 8);
    }
377579
function put_short(w) {
		w &= 0xffff;
		if (outoff + outcnt < OUTBUFSIZ - 2) {
			outbuf[outoff + outcnt++] = (w & 0xff);
			outbuf[outoff + outcnt++] = (w >>> 8);
		} else {
			put_byte(w & 0xff);
			put_byte(w >>> 8);
		}
	}
546511
private static void writeLong(byte[] buffer, int offset, long value) {
    buffer[offset    ] = (byte) (value >> 56);
    buffer[offset + 1] = (byte) (value >> 48);
    buffer[offset + 2] = (byte) (value >> 40);
    buffer[offset + 3] = (byte) (value >> 32);
    buffer[offset + 4] = (byte) (value >> 24);
    buffer[offset + 5] = (byte) (value >> 16);
    buffer[offset + 6] = (byte) (value >> 8);
    buffer[offset + 7] = (byte) value;
  }
328910
private static int byteArrayToInt(byte[] data)
    {
        // byte[] -> int
        int number = 0;
        for (int i = 0; i < 4; ++i)
        {
            number |= (data[3-i] & 0xff) << (i << 3);
        }
        return number;
    }
1200814
private void applyHorizontalLimit()
    {
        if (mover.getX() < limitLeft && limitLeft != Integer.MIN_VALUE)
        {
            mover.teleportX(limitLeft);
        }
        else if (mover.getX() > limitRight && limitRight != Integer.MAX_VALUE)
        {
            mover.teleportX(limitRight);
        }
    }
1165591
public static int writeShort(byte[] target, int offset, short value) {
        target[offset] = (byte) (value >>> 8 & 255);
        target[offset + 1] = (byte) (value & 255);
        return Short.BYTES;
    }
1322613
function(length) {
  if(typeof length !== "number") return null;
  if(length < 0) return null;

  let len = [];
  let digit = 0;

  do {
    digit = length % 128 | 0;
    length = length / 128 | 0;
    if (length > 0) {
        digit = digit | 0x80;
    }
    len.push(digit);
  } while (length > 0);

  return len;
}
57613
void writeLong(long value) throws IOException {
        if (value < MIN_FIX_INT) {
            // large negative
            if (value >= Byte.MIN_VALUE) {
                output.writeByte(SINT_8);
                output.writeByte((byte) value);
            } else if (value >= Short.MIN_VALUE) {
                output.writeByte(SINT_16);
                output.writeShort((short) value);
            } else if (value >= Integer.MIN_VALUE) {
                output.writeByte(SINT_32);
                output.writeInt((int) value);
            } else {
                output.writeByte(SINT_64);
                output.writeLong(value);
            }
        } else if (value < MAX_FIX_INT) {
            // in range -64 to 127
            output.writeByte((byte) value);
        } else {
            // large positive
            if (value < 0xFF) {
                output.writeByte(UINT_8);
                output.writeByte((byte) value);
            } else if (value < 0xFFFF) {
                output.writeByte(UINT_16);
                output.writeShort((short) value);
            } else if (value < 0xFFFFFFFFL) {
                output.writeByte(UINT_32);
                output.writeInt((int) value);
            } else {
                output.writeByte(UINT_64);
                output.writeLong(value);
            }
        }
    }