コード例 #1
0
        /// <summary>
        /// Uses the <paramref name="reader"/> to read a custom <paramref name="type"/> (class).
        /// </summary>
        /// <param name="type">Must not be null.</param>
        /// <param name="reader">Must not be null.</param>
        /// <returns>Returns null if the custom type is not supported.</returns>
        protected virtual object ReadValueForCustomType(Type type, FileChunkReader reader)
        {
            Check.IfArgumentNull(type, nameof(type));
            Check.IfArgumentNull(reader, nameof(reader));

            if (type.FullName == typeof(FourCharacterCode).FullName)
            {
                return(reader.ReadFourCharacterCode());
            }

            if (type.FullName == typeof(Stream).FullName)
            {
                return(reader.GetRemainingCurrentChunkSubStream());
            }

            if (type.FullName == typeof(byte[]).FullName)
            {
                return(reader.GetRemainingCurrentChunkBuffer());
            }

            if (type.IsChunk())
            {
                // should be handled outside the member writer
                throw new InvalidOperationException();
            }

            return(null);
        }
コード例 #2
0
        protected virtual object ReadValueForType(Type type, FileChunkReader reader)
        {
            Contract.Requires(type != null);
            Contract.Requires(reader != null);
            Check.IfArgumentNull(type, "type");
            Check.IfArgumentNull(reader, "reader");

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Byte:
                return(reader.ReadByte());

            case TypeCode.Char:
                return(reader.ReadChar());

            case TypeCode.Int16:
                return(reader.ReadInt16());

            case TypeCode.Int32:
                return(reader.ReadInt32());

            case TypeCode.Int64:
                return(reader.ReadInt64());

            case TypeCode.String:
                return(reader.ReadString());

            case TypeCode.UInt16:
                return(reader.ReadUInt16());

            case TypeCode.UInt32:
                return(reader.ReadUInt32());

            case TypeCode.UInt64:
                return(reader.ReadUInt64());

            case TypeCode.Object:
                // handled as custom object
                break;

            default:
                throw new NotSupportedException();
            }

            var value = this.ReadValueForCustomType(type, reader);

            return(value);
        }
コード例 #3
0
        /// <summary>
        /// Creates a new <see cref="FileChunkReader"/> instance.
        /// </summary>
        /// <param name="container">Must not be null.</param>
        /// <returns>Never returns null.</returns>
        public static FileChunkReader CreateFileChunkReader(this CompositionContainer container)
        {
            Contract.Requires(container != null);
            Contract.Ensures(Contract.Result <FileChunkReader>() != null);

            var context = container.GetService <ChunkFileContext>();

            if (context == null)
            {
                throw new InvalidOperationException("File Context export was not found in the Composition Container.");
            }

            if (context.CompositionContainer == null)
            {
                context.CompositionContainer = container;
            }

            var reader = new FileChunkReader(context);

            return(reader);
        }
コード例 #4
0
        public void ReadFields(FileChunkReader reader)
        {
            Contract.Requires(reader != null);
            Check.IfArgumentNull(reader, "reader");

            // keep processing native data type members
            foreach (var member in this.members)
            {
                if (!reader.CurrentStreamCanRead)
                {
                    break;
                }

                // this member represent a chunk
                if (member.ChunkIds != null)
                {
                    throw new NotSupportedException("This method does not support reading chunks. No mixed (chunks and data) types allowed.");
                }

                try
                {
                    object value = this.ReadValueForType(member.DataType, reader);

                    member.SetValue(this.Instance, value, false);
                }
                catch (EndOfStreamException eos)
                {
                    var fmt = "The end of the chunk was encountered while reading data for the '{1}' member on '{0}'." +
                              " Use the [Ignore] attribute to exclude members from serializing.";

                    var msg = String.Format(
                        CultureInfo.InvariantCulture,
                        fmt,
                        this.Instance.GetType().FullName,
                        member.GetMemberName());

                    throw new ChunkFileException(msg, eos);
                }
            }
        }