public char?[] ReadNullableChars(int count)
        {
            if (ReadBoolean())
            {
                return(null);
            }
            else
            {
                int     len   = ReadInt32();
                char?[] chars = SafeMemoryAllocator.CreateArray <char?>(len);

                for (int i = 0; i < len; i++)
                {
                    if (i < count)
                    {
                        chars[i] = ReadNullableChar();
                    }
                    else
                    {
                        ReadNullableChar();
                    }
                }

                return(chars);
            }
        }
        /// <summary>
        /// Reads a collection of objects which implement ICustomSerializable from the current stream.
        /// </summary>
        public List <T> ReadList <T>() where T : ICustomSerializable, new()
        {
            int count = ReadInt32();

            if (count == -1)
            {
                return(null);
            }
            else
            {
                List <T> list = SafeMemoryAllocator.CreateList <T>(count);

                for (int i = 0; i < count; i++)
                {
                    if (ReadBoolean())
                    {
                        T item = new T();

                        item.Deserialize(this);

                        list.Add(item);
                    }
                    else
                    {
                        list.Add(default(T));
                    }
                }
                return(list);
            }
        }
        public byte?[] ReadNullableBytes(int count)
        {
            if (ReadBoolean())
            {
                return(null);
            }
            else
            {
                int     len   = ReadInt32();
                byte?[] bytes = SafeMemoryAllocator.CreateArray <byte?>(len);

                for (int i = 0; i < len; i++)
                {
                    if (i < count)
                    {
                        bytes[i] = ReadNullableByte();
                    }
                    else
                    {
                        ReadNullableByte();
                    }
                }

                return(bytes);
            }
        }
        /// <summary>
        /// Reads an object of type <see cref="byte[]"/> from the current stream
        /// and advances the stream position.
        /// This method reads directly from the underlying stream.
        /// </summary>
        /// <param name="count">number of bytes read</param>
        /// <returns>object read from the stream</returns>
        public byte[] ReadBytes(int count)
        {
            byte[] buffer = SafeMemoryAllocator.CreateArray <byte>(count);
            int    offset = 0;

            do
            {
                int read = stream.Read(buffer, offset, count);
                if (read == 0)
                {
                    break;
                }
                offset += read;
                count  -= read;
            }while (count > 0);

            if (offset != buffer.Length)
            {
                string length   = "unknown";
                string position = "unknown";
                if (stream.CanSeek)
                {
                    length   = stream.Length.ToString();
                    position = stream.Position.ToString();
                }

                //this will happen when the stream is shorter than count
                throw new ApplicationException(
                          string.Format("CompactBinaryReader: Tried to read past end of stream, failed to read bytes from stream. count: {0}, offset {1}, buffer.Length {2}, stream.Length {3}, stream.Position {4}",
                                        count, offset, buffer.Length, length, position));
            }

            return(buffer);
            //return reader.ReadBytes(count);
        }
Ejemplo n.º 5
0
        private static string ReadString(IPrimitiveReader reader)
        {
            int length = reader.ReadVarInt32();

            if (length < -1)
            {
                throw new InvalidDataException();
            }

            if (length == -1)
            {
                return(null);
            }
            if (length == 0)
            {
                return(string.Empty);
            }

            var ms = reader.BaseStream as MemoryStream;

            if (ms != null && ms.IsPubliclyVisible())
            {
                var buffer = ms.GetBuffer();
                var result = _encoding.GetString(buffer, (int)ms.Position, length);
                ms.Seek(length, SeekOrigin.Current);
                return(result);
            }

            var bytes = SafeMemoryAllocator.CreateArray <byte>(length);

            reader.BaseStream.Read(bytes, 0, length);
            return(_encoding.GetString(bytes));
        }
Ejemplo n.º 6
0
        public override object Read(CompactBinaryReader reader)
        {
            int length = reader.ReadInt32();

            UInt64[] array = SafeMemoryAllocator.CreateArray <UInt64>(length);
            for (int i = 0; i < length; i++)
            {
                array[i] = reader.ReadUInt64();
            }
            return(array);
        }
        public int[] ReadInt32Array()
        {
            int count = ReadInt32();

            if (count == -1)
            {
                return(null);
            }
            else
            {
                int[] array = SafeMemoryAllocator.CreateArray <int>(count);

                for (int i = 0; i < count; i++)
                {
                    int value = ReadInt32();
                    array[i] = value;
                }
                return(array);
            }
        }
Ejemplo n.º 8
0
        private static IEnumerable <string> ReadSegments(
            IPrimitiveReader reader,
            char segmentDelimiter)
        {
            int count = reader.ReadVarInt32();

            if (count == 0)
            {
                yield break;
            }

            var segments = SafeMemoryAllocator.CreateList <string>(count);

            for (; count > 0; --count)
            {
                segments.Add(ReadString(reader));
            }

            var builder = new StringBuilder();

            count = reader.ReadVarInt32();
            for (; count > 0; --count)
            {
                while (true)
                {
                    int segmentId = reader.ReadVarInt32();
                    if (segmentId < 0)
                    {
                        builder.Append(segments[-segmentId - 1]);
                        break;
                    }
                    builder.Append(segments[segmentId - 1]);
                    builder.Append(segmentDelimiter);
                }
                yield return(builder.ToString());

                builder.Length = 0;
            }
        }
        /// <summary>
        /// Reads an array of generic objects which implement ICustomSerializable from the current stream.
        /// </summary>
        public T[] ReadArray <T>() where T : ICustomSerializable, new()
        {
            int count = ReadInt32();

            if (count == -1)
            {
                return(null);
            }
            else
            {
                T[] array = SafeMemoryAllocator.CreateArray <T>(count);

                for (int i = 0; i < count; i++)
                {
                    T item = new T();

                    item.Deserialize(this);

                    array[i] = item;
                }
                return(array);
            }
        }