Esempio n. 1
0
        /// <summary>
        /// Retrieves a PooledBitWriter
        /// </summary>
        /// <param name="stream">The stream the writer should write to</param>
        /// <returns>A PooledBitWriter</returns>
        public static PooledBitWriter GetWriter(Stream stream)
        {
            if (writers.Count == 0)
            {
                if (createdWriters == 254)
                {
                    if (LogHelper.CurrentLogLevel <= LogLevel.Normal)
                    {
                        LogHelper.LogWarning("255 writers have been created. Did you forget to dispose?");
                    }
                    else if (createdWriters < 255)
                    {
                        createdWriters++;
                    }
                }

                return(new PooledBitWriter(stream));
            }

            PooledBitWriter writer = writers.Dequeue();

            writer.SetStream(stream);

            return(writer);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets a PooledBitWriter from the static BitWriterPool
        /// </summary>
        /// <returns>PooledBitWriter</returns>
        public static PooledBitWriter Get(Stream stream)
        {
            PooledBitWriter writer = BitWriterPool.GetWriter(stream);

            writer.isDisposed = false;
            return(writer);
        }
Esempio n. 3
0
 /// <summary>
 /// Puts a PooledBitWriter back into the pool
 /// </summary>
 /// <param name="writer">The writer to put in the pool</param>
 public static void PutBackInPool(PooledBitWriter writer)
 {
     if (writers.Count < 64)
     {
         writers.Enqueue(writer);
     }
     else if (LogHelper.CurrentLogLevel <= LogLevel.Developer)
     {
         LogHelper.LogInfo("BitWriterPool already has 64 queued. Throwing to GC. Did you forget to dispose?");
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Writes the contents of the type instance to the stream
        /// </summary>
        /// <param name="stream">The stream to write to</param>
        public virtual void Write(Stream stream)
        {
            FieldInfo[] fields = SerializationManager.GetFieldsForType(GetType());

            using (PooledBitWriter writer = PooledBitWriter.Get(stream))
            {
                for (int i = 0; i < fields.Length; i++)
                {
                    writer.WriteObjectPacked(fields[i].GetValue(this));
                }
            }
        }