Esempio n. 1
0
        /// <summary>
        ///     Reads the header from the specified decoder.
        /// </summary>
        /// <param name="decoder">The decoder.</param>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when the object in the stream is not a valid Avro object container.</exception>
        /// <returns>The header.</returns>
        public static ObjectContainerHeader Read(IDecoder decoder)
        {
            if (decoder == null)
            {
                throw new ArgumentNullException("decoder");
            }

            byte[] magic = decoder.DecodeFixed(Magic.Length);
            if (!Magic.SequenceEqual(magic))
            {
                throw new SerializationException("Invalid Avro object container in a stream. The header cannot be recognized.");
            }

            var metadata = new Dictionary <string, byte[]>();

            for (long currentChunkSize = decoder.DecodeMapChunk(); currentChunkSize != 0; currentChunkSize = decoder.DecodeMapChunk())
            {
                for (int i = 0; i < currentChunkSize; ++i)
                {
                    string key   = decoder.DecodeString();
                    byte[] value = decoder.DecodeByteArray();
                    metadata.Add(key, value);
                }
            }

            byte[] syncMarker = decoder.DecodeFixed(16);
            return(new ObjectContainerHeader(metadata, syncMarker));
        }
 protected override object DeserializeSafe(IDecoder decoder)
 {
     return(decoder.DecodeFixed(this.Schema.Size));
 }
        /// <summary>
        ///     Reads the header from the specified decoder.
        /// </summary>
        /// <param name="decoder">The decoder.</param>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when the object in the stream is not a valid Avro object container.</exception>
        /// <returns>The header.</returns>
        public static ObjectContainerHeader Read(IDecoder decoder)
        {
            if (decoder == null)
            {
                throw new ArgumentNullException("decoder");
            }

            byte[] magic = decoder.DecodeFixed(Magic.Length);
            if (!Magic.SequenceEqual(magic))
            {
                throw new SerializationException("Invalid Avro object container in a stream. The header cannot be recognized.");
            }

            var metadata = new Dictionary<string, byte[]>();
            for (long currentChunkSize = decoder.DecodeMapChunk(); currentChunkSize != 0; currentChunkSize = decoder.DecodeMapChunk())
            {
                for (int i = 0; i < currentChunkSize; ++i)
                {
                    string key = decoder.DecodeString();
                    byte[] value = decoder.DecodeByteArray();
                    metadata.Add(key, value);
                }
            }

            byte[] syncMarker = decoder.DecodeFixed(16);
            return new ObjectContainerHeader(metadata, syncMarker);
        }