Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
IObjectWriter.cs194 linesDownload Raw Back to utils
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Threading;6 7namespace System.Management.Automation.Runspaces8{9    /// <summary>10    /// PipelineWriter allows the caller to provide an asynchronous stream of objects11    /// as input to a <see cref="System.Management.Automation.Runspaces.Pipeline"/>.12    /// </summary>13    /// <seealso cref="System.Management.Automation.Runspaces.Pipeline.Input"/>14    public abstract class PipelineWriter15    {16        /// <summary>17        /// Signaled when buffer space is available in the underlying stream.18        /// </summary>19        public abstract WaitHandle WaitHandle20        {21            get;22        }23 24        /// <summary>25        /// Check if the stream is open for further writes.26        /// </summary>27        /// <value>true if the underlying stream is open, otherwise false</value>28        /// <remarks>29        /// Attempting to write to the underlying stream if IsOpen is false throws30        /// a <see cref="PipelineClosedException"/>.31        /// </remarks>32        public abstract bool IsOpen33        {34            get;35        }36 37        /// <summary>38        /// Returns the number of objects currently in the underlying stream.39        /// </summary>40        public abstract int Count41        {42            get;43        }44 45        /// <summary>46        /// Get the capacity of the stream.47        /// </summary>48        /// <value>49        /// The capacity of the stream.50        /// </value>51        /// <remarks>52        /// The capacity is the number of objects that stream may contain at one time.  Once this53        /// limit is reached, attempts to write into the stream block until buffer space54        /// becomes available.55        /// </remarks>56        public abstract int MaxCapacity57        {58            get;59        }60 61        /// <summary>62        /// Close the stream.63        /// </summary>64        /// <remarks>65        /// Causes subsequent calls to IsOpen to return false and calls to66        /// a write operation to throw an ObjectDisposedException.67        /// All calls to Close() after the first call are silently ignored.68        /// </remarks>69        /// <exception cref="ObjectDisposedException">70        /// The stream is already disposed71        /// </exception>72        public abstract void Close();73 74        /// <summary>75        /// Flush the buffered data from the stream.  Closed streams may be flushed,76        /// but disposed streams may not.77        /// </summary>78        /// <exception cref="ObjectDisposedException">79        /// The stream is already disposed80        /// </exception>81        public abstract void Flush();82 83        /// <summary>84        /// Write a single object into the underlying stream.85        /// </summary>86        /// <param name="obj">The object to add to the stream.</param>87        /// <returns>88        /// One, if the write was successful, otherwise;89        /// zero if the stream was closed before the object could be written,90        /// or if the object was AutomationNull.Value.91        /// </returns>92        /// <exception cref="PipelineClosedException">93        /// The underlying stream is already closed94        /// </exception>95        /// <remarks>96        /// AutomationNull.Value is ignored97        /// </remarks>98        public abstract int Write(object obj);99 100        /// <summary>101        /// Write multiple objects to the underlying stream.102        /// </summary>103        /// <param name="obj">Object or enumeration to read from.</param>104        /// <param name="enumerateCollection">105        /// If enumerateCollection is true, and <paramref name="obj"/>106        /// is an enumeration according to LanguagePrimitives.GetEnumerable,107        /// the objects in the enumeration will be unrolled and108        /// written separately.  Otherwise, <paramref name="obj"/>109        /// will be written as a single object.110        /// </param>111        /// <returns>The number of objects written.</returns>112        /// <exception cref="PipelineClosedException">113        /// The underlying stream is already closed114        /// </exception>115        /// <remarks>116        /// If the enumeration contains elements equal to117        /// AutomationNull.Value, they are ignored.118        /// This can cause the return value to be less than the size of119        /// the collection.120        /// </remarks>121        public abstract int Write(object obj, bool enumerateCollection);122    }123 124    internal class DiscardingPipelineWriter : PipelineWriter125    {126        private readonly ManualResetEvent _waitHandle = new ManualResetEvent(true);127 128        public override WaitHandle WaitHandle129        {130            get { return _waitHandle; }131        }132 133        private bool _isOpen = true;134 135        public override bool IsOpen136        {137            get { return _isOpen; }138        }139 140        private int _count = 0;141 142        public override int Count143        {144            get { return _count; }145        }146 147        public override int MaxCapacity148        {149            get { return int.MaxValue; }150        }151 152        public override void Close()153        {154            _isOpen = false;155        }156 157        public override void Flush()158        {159        }160 161        public override int Write(object obj)162        {163            const int numberOfObjectsWritten = 1;164            _count += numberOfObjectsWritten;165            return numberOfObjectsWritten;166        }167 168        public override int Write(object obj, bool enumerateCollection)169        {170            if (!enumerateCollection)171            {172                return this.Write(obj);173            }174 175            int numberOfObjectsWritten = 0;176            IEnumerable enumerable = LanguagePrimitives.GetEnumerable(obj);177            if (enumerable != null)178            {179                foreach (object o in enumerable)180                {181                    numberOfObjectsWritten++;182                }183            }184            else185            {186                numberOfObjectsWritten++;187            }188 189            _count += numberOfObjectsWritten;190            return numberOfObjectsWritten;191        }192    }193}194