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);
            }
        }
        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);
        }
Beispiel #4
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));
        }
        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);
            }
        }
        /// <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);
            }
        }