public int writeVarLong (long value, boolean optimizePositive) throws KryoException {
		if (!optimizePositive) value = (value << 1) ^ (value >> 63);
		int varInt = 0;

		varInt = (int)(value & 0x7F);

		value >>>= 7;

		if (value == 0) {
			writeByte(varInt);
			return 1;
		}

		varInt |= 0x80;
		varInt |= ((value & 0x7F) << 8);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeInt(varInt);
			niobuffer.order(byteOrder);
			position -= 2;
			niobuffer.position(position);
			return 2;
		}

		varInt |= (0x80 << 8);
		varInt |= ((value & 0x7F) << 16);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeInt(varInt);
			niobuffer.order(byteOrder);
			position -= 1;
			niobuffer.position(position);
			return 3;
		}

		varInt |= (0x80 << 16);
		varInt |= ((value & 0x7F) << 24);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeInt(varInt);
			niobuffer.order(byteOrder);
			position -= 0;
			return 4;
		}

		varInt |= (0x80 << 24);
		long varLong = (varInt & 0xFFFFFFFFL);
		varLong |= (((long)(value & 0x7F)) << 32);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeLong(varLong);
			niobuffer.order(byteOrder);
			position -= 3;
			niobuffer.position(position);
			return 5;
		}

		varLong |= (0x80L << 32);
		varLong |= (((long)(value & 0x7F)) << 40);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeLong(varLong);
			niobuffer.order(byteOrder);
			position -= 2;
			niobuffer.position(position);
			return 6;
		}

		varLong |= (0x80L << 40);
		varLong |= (((long)(value & 0x7F)) << 48);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeLong(varLong);
			niobuffer.order(byteOrder);
			position -= 1;
			niobuffer.position(position);
			return 7;
		}

		varLong |= (0x80L << 48);
		varLong |= (((long)(value & 0x7F)) << 56);

		value >>>= 7;

		if (value == 0) {
			niobuffer.order(ByteOrder.LITTLE_ENDIAN);
			writeLong(varLong);
			niobuffer.order(byteOrder);
			return 8;
		}

		varLong |= (0x80L << 56);
		niobuffer.order(ByteOrder.LITTLE_ENDIAN);
		writeLong(varLong);
		niobuffer.order(byteOrder);
		write((byte)(value));
		return 9;
	}