/// <exception cref="System.IO.IOException" />
 public DefaultPortableWriter(PortableSerializer serializer, IBufferObjectDataOutput @out, IClassDefinition cd)
 {
     this.serializer = serializer;
     this.@out = @out;
     this.cd = cd;
     writtenFields = new HashSet<string>(); //cd.GetFieldCount()
     begin = @out.Position();
     // room for final offset
     @out.WriteZeroBytes(4);
     @out.WriteInt(cd.GetFieldCount());
     offset = @out.Position();
     // one additional for raw data
     var fieldIndexesLength = (cd.GetFieldCount() + 1)*Bits.IntSizeInBytes;
     @out.WriteZeroBytes(fieldIndexesLength);
 }
        /// <exception cref="System.IO.IOException" />
        public DefaultPortableWriter(PortableSerializer serializer, IBufferObjectDataOutput @out, IClassDefinition cd)
        {
            _serializer    = serializer;
            _out           = @out;
            _cd            = cd;
            _writtenFields = new HashSet <string>(); //cd.GetFieldCount()
            _begin         = @out.Position();
            // room for final offset
            @out.WriteZeroBytes(4);
            @out.WriteInt(cd.GetFieldCount());
            _offset = @out.Position();
            // one additional for raw data
            var fieldIndexesLength = (cd.GetFieldCount() + 1) * Bits.IntSizeInBytes;

            @out.WriteZeroBytes(fieldIndexesLength);
        }
        /// <exception cref="System.IO.IOException" />
        public virtual void WritePortableArray(string fieldName, IPortable[] portables)
        {
            var fd  = SetPosition(fieldName, FieldType.PortableArray);
            var len = portables == null ? Bits.NullArray : portables.Length;

            _out.WriteInt(len);
            _out.WriteInt(fd.GetFactoryId());
            _out.WriteInt(fd.GetClassId());
            if (len > 0)
            {
                var offset = _out.Position();
                _out.WriteZeroBytes(len * 4);
                for (var i = 0; i < portables.Length; i++)
                {
                    var portable = portables[i];
                    CheckPortableAttributes(fd, portable);
                    var position = _out.Position();
                    _out.WriteInt(offset + i * Bits.IntSizeInBytes, position);
                    _serializer.WriteInternal(_out, portable);
                }
            }
        }
        /// <exception cref="System.IO.IOException" />
        public virtual void WritePortableArray(string fieldName, IPortable[] portables)
        {
            var fd  = SetPosition(fieldName, FieldType.PortableArray);
            var len = portables == null ? ArraySerializer.NullArrayLength : portables.Length;

            _out.Write(len);
            _out.Write(fd.FactoryId);
            _out.Write(fd.ClassId);
            if (len > 0)
            {
                var offset = _out.Position;
                _out.WriteZeroBytes(len * 4);
                for (var i = 0; i < portables.Length; i++)
                {
                    var portable = portables[i];
                    CheckPortableAttributes(fd, portable);
                    var position = _out.Position;
                    _out.Write(offset + i * BytesExtensions.SizeOfInt, position);
                    _serializer.WriteInternal(_out, portable);
                }
            }
        }