Exemplo n.º 1
0
        /// <summary>
        /// Reads a <see cref="Guid"/> value from an array of bytes.
        /// </summary>
        /// <param name="bytes">The array of bytes to read from.</param>
        /// <param name="position">The position in the array where the value should be read.</param>
        /// <returns></returns>
        public static Guid ReadGuid(this byte[] bytes, int position)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            // must be at least able to read a bool
            if (position < 0 || bytes.Length < position + SizeOfBool)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }

            // read the 'empty' bool
            if (bytes.ReadBool(position))
            {
                return(Guid.Empty);
            }

            // not 'empty', must be able to read a full guid
            if (bytes.Length < position + SizeOfGuid)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }

            position += SizeOfByte;

            // ReSharper disable once UseObjectOrCollectionInitializer
#pragma warning disable IDE0017 // Simplify object initialization
            var v = new JavaUuidOrder();
#pragma warning restore IDE0017 // Simplify object initialization

            v.X0 = bytes[position]; position += SizeOfByte;
            v.X1 = bytes[position]; position += SizeOfByte;
            v.X2 = bytes[position]; position += SizeOfByte;
            v.X3 = bytes[position]; position += SizeOfByte;

            v.X4 = bytes[position]; position += SizeOfByte;
            v.X5 = bytes[position]; position += SizeOfByte;
            v.X6 = bytes[position]; position += SizeOfByte;
            v.X7 = bytes[position]; position += SizeOfByte;

            v.X8 = bytes[position]; position += SizeOfByte;
            v.X9 = bytes[position]; position += SizeOfByte;
            v.XA = bytes[position]; position += SizeOfByte;
            v.XB = bytes[position]; position += SizeOfByte;

            v.XC = bytes[position]; position += SizeOfByte;
            v.XD = bytes[position]; position += SizeOfByte;
            v.XE = bytes[position]; position += SizeOfByte;
            v.XF = bytes[position];

            return(v.Value);
        }
        /// <summary>
        /// Writes a <see cref="Guid"/> value to an array of bytes.
        /// </summary>
        /// <param name="bytes">The array of bytes to write to.</param>
        /// <param name="position">The position in the array where the value should be written.</param>
        /// <param name="value">The value to write.</param>
        public static void WriteGuid(this byte[] bytes, int position, Guid value)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            // must at least be able to write a bool
            if (position < 0 || bytes.Length < position + SizeOfBool)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }

            // write the 'empty' bool
            bytes.WriteBool(position, value == Guid.Empty);
            if (value == Guid.Empty)
            {
                return;
            }

            // if not empty, must be able to write the full guid
            if (bytes.Length < position + SizeOfGuid)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }

            position += SizeOfByte;

            var v = new JavaUuidOrder {
                Value = value
            };

            bytes[position] = v.X0; position += SizeOfByte;
            bytes[position] = v.X1; position += SizeOfByte;
            bytes[position] = v.X2; position += SizeOfByte;
            bytes[position] = v.X3; position += SizeOfByte;

            bytes[position] = v.X4; position += SizeOfByte;
            bytes[position] = v.X5; position += SizeOfByte;
            bytes[position] = v.X6; position += SizeOfByte;
            bytes[position] = v.X7; position += SizeOfByte;

            bytes[position] = v.X8; position += SizeOfByte;
            bytes[position] = v.X9; position += SizeOfByte;
            bytes[position] = v.XA; position += SizeOfByte;
            bytes[position] = v.XB; position += SizeOfByte;

            bytes[position] = v.XC; position += SizeOfByte;
            bytes[position] = v.XD; position += SizeOfByte;
            bytes[position] = v.XE; position += SizeOfByte;
            bytes[position] = v.XF;
        }