Exemplo n.º 1
0
        /// <summary>
        ///     Writes an array of booleans to the writer.
        /// </summary>
        /// <param name="value">The array of booleans to write.</param>
        public void Write(bool[] value)
        {
            int total = (int)Math.Ceiling(value.Length / 8.0);   //Total number of bytes that will be needed

            buffer.EnsureLength(Position + 4 + total);
            BigEndianHelper.WriteBytes(buffer.Buffer, Position, value.Length);

            int ptr = 0;                                    //Pointer to current boolean in value array

            //Repeat for each byte we will need
            for (int i = 0; i < total; i++)
            {
                byte b = 0;                                     //Temp holder of booleans being packed

                //Repeat for each bit in that byte
                for (int k = 7; k >= 0 && ptr < value.Length; k--)
                {
                    if (value[ptr])
                    {
                        b |= (byte)(1 << k);
                    }

                    ptr++;
                }

                buffer.Buffer[Position + 4 + i] = b;
            }

            Position    += 4 + total;
            buffer.Count = Math.Max(Length, Position);
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Writes a single unsigned 64bit integer to the writer.
 /// </summary>
 /// <param name="value">The unsigned 64bit integer to write.</param>
 public void Write(ulong value)
 {
     buffer.EnsureLength(Position + 8);
     BigEndianHelper.WriteBytes(buffer.Buffer, Position, value);
     Position    += 8;
     buffer.Count = Math.Max(Length, Position);
 }
Exemplo n.º 3
0
 /// <summary>
 ///     Writes an array of signed bytes to the writer.
 /// </summary>
 /// <param name="value">The array of signed bytes to write.</param>
 public void Write(sbyte[] value)
 {
     buffer.EnsureLength(Position + 4 + value.Length);
     BigEndianHelper.WriteBytes(buffer.Buffer, Position, value.Length);
     System.Buffer.BlockCopy(value, 0, buffer.Buffer, Position + 4, value.Length);
     Position    += 4 + value.Length;
     buffer.Count = Math.Max(Length, Position);
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Writes an array of characters to the writer using the given encoding.
        /// </summary>
        /// <param name="encoding">The encoding to use during the deserialization.</param>
        /// <param name="value">The array of characters to write.</param>
        public void Write(char[] value, Encoding encoding)
        {
            //Legacy implementation means we need to send number of bytes not chars
            int length = encoding.GetByteCount(value);

            buffer.EnsureLength(Position + 4 + length);
            BigEndianHelper.WriteBytes(buffer.Buffer, Position, length);
            encoding.GetBytes(value, 0, value.Length, buffer.Buffer, Position + 4);
            Position    += 4 + length;
            buffer.Count = Math.Max(Length, Position);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Writes an array of strings to the writer using the writer's encoding.
        /// </summary>
        /// <param name="value">The array of strings to write.</param>
        public void Write(string[] value)
        {
            buffer.EnsureLength(Position + 4);          //Encodings suck, just do this manually
            BigEndianHelper.WriteBytes(buffer.Buffer, Position, value.Length);
            Position    += 4;
            buffer.Count = Math.Max(Length, Position);

            foreach (string b in value)
            {
                Write(b);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Writes an array IDarkRiftSerializables to the writer.
        /// </summary>
        /// <param name="value">The array of serializable objects to write.</param>
        public void Write <T>(T[] value) where T : IDarkRiftSerializable
        {
            buffer.EnsureLength(Position + 4);
            BigEndianHelper.WriteBytes(buffer.Buffer, Position, value.Length);

            Position    += 4;
            buffer.Count = Math.Max(Length, Position);

            for (int i = 0; i < value.Length; i++)
            {
                Write(value[i]);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Writes an array unsigned 64bit integers to the writer.
        /// </summary>
        /// <param name="value">The array of unsigned 64bit integers to write.</param>
        public void Write(ulong[] value)
        {
            buffer.EnsureLength(Position + 4 + value.Length * 8);
            BigEndianHelper.WriteBytes(buffer.Buffer, Position, value.Length);

            for (int i = 0, j = Position + 4; i < value.Length; i++, j += 8)
            {
                BigEndianHelper.WriteBytes(buffer.Buffer, j, value[i]);
            }

            Position    += 4 + value.Length * 8;
            buffer.Count = Math.Max(Length, Position);
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Writes an array of singles to the writer.
        /// </summary>
        /// <param name="value">The array of singles to write.</param>
        public void Write(float[] value)
        {
            buffer.EnsureLength(Position + 4 + value.Length * 4);
            BigEndianHelper.WriteBytes(buffer.Buffer, Position, value.Length);

            for (int i = 0, j = Position + 4; i < value.Length; i++, j += 4)
            {
                byte[] b = BitConverter.GetBytes(value[i]);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(b);
                }

                System.Buffer.BlockCopy(b, 0, buffer.Buffer, j, 4);
            }

            Position    += 4 + value.Length * 4;
            buffer.Count = Math.Max(Length, Position);
        }