Exemplo n.º 1
0
        /// <summary>
        ///     Reads an array of booleans from the reader.
        /// </summary>
        /// <param name="destination">The array to read booleans into.</param>
        /// <param name="offset">The offset at which to write bytes into the array.</param>
        public void ReadBooleansInto(bool[] destination, int offset)
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            int total = (int)Math.Ceiling(length / 8.0);    //Number of bytes the booleans are stored in

            if (Position + 4 + total > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {total} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            int ptr = offset;                                    //The index of the array we're writing to.

            //Repeat for each byte we will need
            for (int i = 0; i < total; i++)
            {
                byte b = buffer.Buffer[buffer.Offset + Position + 4 + i];

                //Repeat for each bit in that byte
                for (int k = 7; k >= 0 && ptr < length; k--)
                {
                    destination[ptr++] = (b & (1 << k)) != 0;
                }
            }

            Position += 4 + total;
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Reads a single 32bit integer from the reader.
        /// </summary>
        /// <returns>The 32bit integer read.</returns>
        public int ReadInt32()
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected 4 bytes but reader only has {Length - Position} bytes remaining.");
            }

            int v = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            Position += 4;

            return(v);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Reads an array of strings from the reader using the reader's encoding.
        /// </summary>
        /// <param name="destination">The array to read strings into.</param>
        /// <param name="offset">The offset at which to write bytes into the array.</param>
        public void ReadStringsInto(string[] destination, int offset)
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            Position += 4;

            for (int i = 0; i < length; i++)
            {
                destination[i + offset] = ReadString();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Reads an array of signed bytes from the reader.
        /// </summary>
        /// <param name="destination">The array to read sbytes into.</param>
        /// <param name="offset">The offset at which to write bytes into the array.</param>
        public void ReadSBytesInto(sbyte[] destination, int offset)
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            if (Position + 4 + length > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            Buffer.BlockCopy(buffer.Buffer, buffer.Offset + Position + 4, destination, offset, length);

            Position += 4 + length;
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Reads an array of strings from the reader using the reader's encoding.
        /// </summary>
        /// <returns>The array of strings read.</returns>
        public string[] ReadStrings()
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            Position += 4;

            string[] array = new string[length];
            for (int i = 0; i < length; i++)
            {
                array[i] = ReadString();
            }

            return(array);
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Reads an array of characters from the reader using the given encoding.
        /// </summary>
        /// <param name="destination">The array to read characters into.</param>
        /// <param name="offset">The offset at which to write bytes into the array.</param>
        /// <param name="encoding">The encoding to use during the deserialization.</param>
        public void ReadCharsInto(char[] destination, int offset, Encoding encoding)
        {
            //Read number of bytes not chars
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            if (Position + 4 + length > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            encoding.GetChars(buffer.Buffer, buffer.Offset + Position + 4, length, destination, offset);

            Position += 4 + length;
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Reads an array unsigned 16bit integers from the reader.
        /// </summary>
        /// <param name="destination">The array to read strings into.</param>
        /// <param name="offset">The offset at which to write bytes into the array.</param>
        public void ReadUInt16sInto(ushort[] destination, int offset)
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            if (Position + 4 + length * 2 > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length * 2} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            for (int i = 0, j = buffer.Offset + Position + 4; i < length; i++, j += 2)
            {
                destination[i + offset] = BigEndianHelper.ReadUInt16(buffer.Buffer, j);
            }

            Position += 4 + length * 2;
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Reads an array of signed bytes from the reader.
        /// </summary>
        /// <returns>The array of signed bytes read.</returns>
        public sbyte[] ReadSBytes()
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            if (Position + 4 + length > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            sbyte[] array = new sbyte[length];
            Buffer.BlockCopy(buffer.Buffer, buffer.Offset + Position + 4, array, 0, length);

            Position += 4 + length;

            return(array);
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Reads a single string from the reader using the given encoding.
        /// </summary>
        /// <param name="encoding">The encoding to deserialize the string using.</param>
        /// <returns>The string read.</returns>
        public string ReadString(Encoding encoding)
        {
            //Read number of bytes not chars
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            if (Position + 4 + length > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            string v = encoding.GetString(buffer.Buffer, buffer.Offset + Position + 4, length);

            Position += 4 + length;

            return(v);
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Reads an array unsigned 32bit integers from the reader.
        /// </summary>
        /// <returns>The array of unsigned 32bit integers read.</returns>
        public uint[] ReadUInt32s()
        {
            if (Position + 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining.");
            }

            int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position);

            if (Position + 4 + length * 4 > Length)
            {
                throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length * 4} bytes but reader only has {Length - Position - 4} bytes remaining.");
            }

            uint[] array = new uint[length];
            for (int i = 0, j = buffer.Offset + Position + 4; i < length; i++, j += 4)
            {
                array[i] = BigEndianHelper.ReadUInt32(buffer.Buffer, j);
            }

            Position += 4 + length * 4;

            return(array);
        }