private void EnsureOpenStreamHeader()
 {
     if (_streamHeaderPosition == -1L)
     {
         Writer.Write(SerializerHeaders.Barf);
         _streamHeaderPosition = BarfStreamHeader.BeginWrite(Writer, BarfFormatter.CurrentFrameworkVersion);
     }
 }
示例#2
0
        internal static BarfStreamHeader ReadFrom(IPrimitiveReader reader)
        {
            var result = new BarfStreamHeader();
            var value  = reader.ReadVarInt32();

            if (value < 0)
            {
                reader.RaiseInvalidDataException();
            }
            result.FrameworkVersion = value;

            result.Flags = (HeaderFlags)reader.ReadByte();

            return(result);
        }
        private void EnsureCloseStreamHeader()
        {
            if (_recursionCount == 0)
            {
                var flags = HeaderFlags.None;

                if (_typeTable != null && _typeTable.Count > 0)
                {
                    flags |= HeaderFlags.HasNameTable;
                }

                BarfStreamHeader.EndWrite(Writer, _streamHeaderPosition, flags);

                if ((flags & HeaderFlags.HasNameTable) == HeaderFlags.HasNameTable)
                {
                    _typeTable.WriteTo(Writer);
                }
                _streamHeaderPosition = -1L;
                _typeTable            = null;
            }
        }
        /// <summary>
        /// Begins a region of code that deserializes a barf type.
        /// </summary>
        public BarfObjectHeader BeginObject <T>()
        {
            var def = BarfTypeDefinition.Get <T>(true);

            bool deserializeTypeTable = false;

            if (StreamHeader == null)
            {
                StreamHeader = BarfStreamHeader.ReadFrom(Reader);
                if (StreamHeader.FrameworkVersion > BarfFormatter.MaxFrameworkVersion)
                {
                    string message = string.Format(
                        "Encountered a BARF formatted stream with FrameworkVersion=\"{0}\" but MaxFrameworkVersion=\"{1}\".",
                        StreamHeader.FrameworkVersion,
                        BarfFormatter.MaxFrameworkVersion);
                    throw new UnhandledVersionException(message);
                }

                deserializeTypeTable = StreamHeader.Flags.IsSet(HeaderFlags.HasNameTable);
            }

            var objectHeader = BarfObjectHeader.ReadFrom(Reader);

            if (!objectHeader.IsNull)
            {
                if (objectHeader.Version < def.MinVersion)
                {
                    var message = string.Format(
                        "Binary data was encoded with Version=\"{0}\" but the current MinVersion=\"{1}\".",
                        objectHeader.Version,
                        def.MinVersion);
                    throw new UnhandledVersionException(message);
                }

                if (def.CurrentVersion < objectHeader.MinVersion)
                {
                    var message = string.Format(
                        "Binary data was encoded with a MinVersion=\"{0}\" but CurrentVersion=\"{1}\" is less than that.",
                        objectHeader.MinVersion,
                        def.CurrentVersion);
                    throw new UnhandledVersionException(message);
                }

                if (objectHeader.Version < def.MinDeserializeVersion)
                {
                    var message = string.Format(
                        "Binary data is Version=\"{0}\" but MinDeserializeVersion=\"{1}\".",
                        objectHeader.Version,
                        def.MinDeserializeVersion);
                    throw new UnhandledVersionException(message);
                }
            }

            if (deserializeTypeTable)
            {
                var currentPosition = Reader.BaseStream.Position;

                Reader.BaseStream.Seek(objectHeader.Length, SeekOrigin.Current);

                TypeTable = BarfTypeTable.ReadFrom(Reader);

                _streamEnd = Reader.BaseStream.Position;

                Reader.BaseStream.Seek(currentPosition, SeekOrigin.Begin);
            }

            ++_objectDepth;
            return(objectHeader);
        }