<commit_before>/*
  Copyright (c) 2012 - 2013 Ant Micro <www.antmicro.com>

  Authors:
   * <NAME> (<EMAIL>)
   * <NAME> (<EMAIL>)
   * <NAME> (<EMAIL>)

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Antmicro.Migrant.Hooks;
using Antmicro.Migrant.Utilities;
using System.Collections.ObjectModel;
using System.Reflection.Emit;
using Antmicro.Migrant.Generators;
using Antmicro.Migrant.VersionTolerance;
using Antmicro.Migrant.Customization;

namespace Antmicro.Migrant
{
    /// <summary>
    /// Reads the object previously written by <see cref="Antmicro.Migrant.ObjectWriter" />.
    /// </summary>
    public class ObjectReader : IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Antmicro.Migrant.ObjectReader" /> class.
        /// </summary>
        /// <param name='stream'>
        /// Stream from which objects will be read.
        /// </param>
        /// <param name='objectsForSurrogates'>
        /// Dictionary, containing callbacks that provide objects for given type of surrogate. Callbacks have to be of type Func&lt;T, object&gt; where
        /// typeof(T) is type of surrogate. They always have to be in sync with <paramref name="readMethods"/>.
        /// </param>
        /// <param name='postDeserializationCallback'>
        /// Callback which will be called after deserialization of every unique object. Deserialized
        /// object is given in the callback's only parameter.
        /// </param>
        /// <param name='readMethods'>
        /// Cache in which generated read methods are stored and reused between instances of <see cref="Antmicro.Migrant.ObjectReader" />.
        /// Can be null if one does not want to use the cache. For the life of the cache you always have to provide the same 
        /// <paramref name="objectsForSurrogates"/>.
        /// </param>
        /// <param name='isGenerating'>
        /// True if read methods are to be generated, false if one wants to use reflection.
        /// </param>
        /// <param name = "treatCollectionAsUserObject">
        /// True if collection objects are to be deserialized without optimization (treated as normal user objects).
        /// </param>
        /// <param name="versionToleranceLevel"> 
        /// Describes the tolerance level of this reader when handling discrepancies in type description (new or missing fields, etc.).
        /// </param> 
        /// <param name="useBuffering"> 
        /// True if buffering was used with the corresponding ObjectWriter or false otherwise - i.e. when no padding and buffering is used.
        /// </param>
        /// <param name="referencePreservation"> 
        /// Tells deserializer whether open stream serialization preserved objects identieties between serialization. Note that this option should
        /// be consistent with what was used during serialization.
        /// </param>
        public ObjectReader(Stream stream, InheritanceAwareList<Delegate> objectsForSurrogates = null, Action<object> postDeserializationCallback = null, 
                            IDictionary<Type, DynamicMethod> readMethods = null, bool isGenerating = false, bool treatCollectionAsUserObject = false, 
                            VersionToleranceLevel versionToleranceLevel = 0, bool useBuffering = true, 
                            ReferencePreservation referencePreservation = ReferencePreservation.Preserve)
        {
            if(objectsForSurrogates == null)
            {
                objectsForSurrogates = new InheritanceAwareList<Delegate>();
            }
            this.objectsForSurrogates = objectsForSurrogates;
            this.readMethodsCache = readMethods ?? new Dictionary<Type, DynamicMethod>();
            this.useGeneratedDeserialization = isGenerating;
            typeList = new List<Type>();
            methodList = new List<MethodInfo>();
            postDeserializationHooks = new List<Action>();
            this.postDeserializationCallback = postDeserializationCallback;
            this.treatCollectionAsUserObject = treatCollectionAsUserObject;
            delegatesCache = new Dictionary<Type, Func<int, object>>();
            reader = new PrimitiveReader(stream, useBuffering);
            stamper = new TypeStampReader(reader, versionToleranceLevel);
            this.referencePreservation = referencePreservation;
            this.versionToleranceLevel = versionToleranceLevel;
        }

        /// <summary>
        /// Reads the object with the expected formal type <typeparam name='T'/>.<commit_msg>Public properties exposing primitive reader and its configuration added to `ObjectReader`.<commit_after>/*
  Copyright (c) 2012 - 2013 Ant Micro <www.antmicro.com>

  Authors:
   * <NAME> (<EMAIL>)
   * <NAME> (<EMAIL>)
   * <NAME> (<EMAIL>)

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Antmicro.Migrant.Hooks;
using Antmicro.Migrant.Utilities;
using System.Collections.ObjectModel;
using System.Reflection.Emit;
using Antmicro.Migrant.Generators;
using Antmicro.Migrant.VersionTolerance;
using Antmicro.Migrant.Customization;

namespace Antmicro.Migrant
{
    /// <summary>
    /// Reads the object previously written by <see cref="Antmicro.Migrant.ObjectWriter" />.
    /// </summary>
    public class ObjectReader : IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Antmicro.Migrant.ObjectReader" /> class.
        /// </summary>
        /// <param name='stream'>
        /// Stream from which objects will be read.
        /// </param>
        /// <param name='objectsForSurrogates'>
        /// Dictionary, containing callbacks that provide objects for given type of surrogate. Callbacks have to be of type Func&lt;T, object&gt; where
        /// typeof(T) is type of surrogate. They always have to be in sync with <paramref name="readMethods"/>.
        /// </param>
        /// <param name='postDeserializationCallback'>
        /// Callback which will be called after deserialization of every unique object. Deserialized
        /// object is given in the callback's only parameter.
        /// </param>
        /// <param name='readMethods'>
        /// Cache in which generated read methods are stored and reused between instances of <see cref="Antmicro.Migrant.ObjectReader" />.
        /// Can be null if one does not want to use the cache. For the life of the cache you always have to provide the same 
        /// <paramref name="objectsForSurrogates"/>.
        /// </param>
        /// <param name='isGenerating'>
        /// True if read methods are to be generated, false if one wants to use reflection.
        /// </param>
        /// <param name = "treatCollectionAsUserObject">
        /// True if collection objects are to be deserialized without optimization (treated as normal user objects).
        /// </param>
        /// <param name="versionToleranceLevel"> 
        /// Describes the tolerance level of this reader when handling discrepancies in type description (new or missing fields, etc.).
        /// </param> 
        /// <param name="useBuffering"> 
        /// True if buffering was used with the corresponding ObjectWriter or false otherwise - i.e. when no padding and buffering is used.
        /// </param>
        /// <param name="referencePreservation"> 
        /// Tells deserializer whether open stream serialization preserved objects identieties between serialization. Note that this option should
        /// be consistent with what was used during serialization.
        /// </param>
        public ObjectReader(Stream stream, InheritanceAwareList<Delegate> objectsForSurrogates = null, Action<object> postDeserializationCallback = null, 
                            IDictionary<Type, DynamicMethod> readMethods = null, bool isGenerating = false, bool treatCollectionAsUserObject = false, 
                            VersionToleranceLevel versionToleranceLevel = 0, bool useBuffering = true, 
                            ReferencePreservation referencePreservation = ReferencePreservation.Preserve)
        {
            if(objectsForSurrogates == null)
            {
                objectsForSurrogates = new InheritanceAwareList<Delegate>();
            }
            this.objectsForSurrogates = objectsForSurrogates;
            this.readMethodsCache = readMethods ?? new Dictionary<Type, DynamicMethod>();
            this.useGeneratedDeserialization = isGenerating;
            typeList = new List<Type>();
            methodList = new List<MethodInfo>();
            postDeserializationHooks = new List<Action>();
            this.postDeserializationCallback = postDeserializationCallback;
            this.treatCollectionAsUserObject = treatCollectionAsUserObject;
            delegatesCache = new Dictionary<Type, Func<int, object>>();
            reader = new PrimitiveReader(stream, useBuffering);
            stamper = new TypeStampReader(reader, versionToleranceLevel);
            this.referencePreservation = referencePreservation;
            this.versionToleranceLevel = versionToleranceLevel;
        }

        public bool TreatCollectionAsUserObject { get { return treatCollectionAsUserObject; } }

        public PrimitiveReader PrimitiveReader { get { return reader; } }

        /// <summary>
        /// Reads the object with the expected formal type <typeparam name='T'/>.
{'fraction_non_alphanumeric': 0.08608480505076359, 'fraction_numerical': 0.0015357051446122344, 'mean_word_length': 2.805844155844156, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7283829', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2799, 'n_words': 1099}