Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
ObjectWriter.cs232 linesDownload Raw Back to utils
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4namespace System.Management.Automation.Internal5{6    using System;7    using System.Threading;8    using System.Runtime.InteropServices;9    using System.Management.Automation.Runspaces;10 11    /// <summary>12    /// A ObjectWriter for an ObjectStream.13    /// </summary>14    /// <remarks>15    /// This class is not safe for multi-threaded operations.16    /// </remarks>17    internal class ObjectWriter : PipelineWriter18    {19        /// <summary>20        /// Construct with an existing ObjectStream.21        /// </summary>22        /// <param name="stream">The stream to write.</param>23        /// <exception cref="ArgumentNullException">Thrown if the specified stream is null.</exception>24        public ObjectWriter([In, Out] ObjectStreamBase stream)25        {26            ArgumentNullException.ThrowIfNull(stream);27 28            _stream = stream;29#if (false)30            stream.WriteReady += new EventHandler (this.OnWriteReady);31#endif32        }33 34        #region Properties35 36        /// <summary>37        /// Waitable handle for caller's to block until buffer space is available in the underlying stream.38        /// </summary>39        public override WaitHandle WaitHandle40        {41            get42            {43                return _stream.WriteHandle;44            }45        }46 47        /// <summary>48        /// Check if the stream is open for further writes.49        /// </summary>50        /// <value>true if the underlying stream is open, otherwise; false.</value>51        /// <remarks>52        /// Attempting to write to the underlying stream if IsOpen is false throws53        /// a <see cref="PipelineClosedException"/>.54        /// </remarks>55        public override bool IsOpen56        {57            get58            {59                return _stream.IsOpen;60            }61        }62 63        /// <summary>64        /// Returns the number of objects in the underlying stream.65        /// </summary>66        public override int Count67        {68            get69            {70                return _stream.Count;71            }72        }73 74        /// <summary>75        /// Get the capacity of the stream.76        /// </summary>77        /// <value>78        /// The capacity of the stream.79        /// </value>80        /// <remarks>81        /// The capacity is the number of objects that stream may contain at one time.  Once this82        /// limit is reached, attempts to write into the stream block until buffer space83        /// becomes available.84        /// </remarks>85        public override int MaxCapacity86        {87            get88            {89                return _stream.MaxCapacity;90            }91        }92 93        #endregion Properties94 95        #region Methods96 97        /// <summary>98        /// Close the stream.99        /// </summary>100        /// <remarks>101        /// Causes subsequent calls to IsOpen to return false and calls to102        /// a write operation to throw an ObjectDisposedException.103        /// All calls to Close() after the first call are silently ignored.104        /// </remarks>105        /// <exception cref="ObjectDisposedException">106        /// The stream is already disposed107        /// </exception>108        public override void Close()109        {110            _stream.Close();111            // 2003/09/02-JonN I removed setting _stream112            // to null, now all of the tests for null can come out.113        }114 115        /// <summary>116        /// Flush the data from the stream.  Closed streams may be flushed,117        /// but disposed streams may not.118        /// </summary>119        /// <exception cref="ObjectDisposedException">120        /// The underlying stream is disposed121        /// </exception>122        public override void Flush()123        {124            _stream.Flush();125        }126 127        /// <summary>128        /// Write a single object into the underlying stream.129        /// </summary>130        /// <param name="obj">The object to add to the stream.</param>131        /// <returns>132        /// One, if the write was successful, otherwise;133        /// zero if the stream was closed before the object could be written,134        /// or if the object was AutomationNull.Value.135        /// </returns>136        /// <exception cref="PipelineClosedException">137        /// The underlying stream is closed138        /// </exception>139        /// <remarks>140        /// AutomationNull.Value is ignored141        /// </remarks>142        public override int Write(object obj)143        {144            return _stream.Write(obj);145        }146 147        /// <summary>148        /// Write objects to the underlying stream.149        /// </summary>150        /// <param name="obj">Object or enumeration to read from.</param>151        /// <param name="enumerateCollection">152        /// If enumerateCollection is true, and <paramref name="obj"/>153        /// is an enumeration according to LanguagePrimitives.GetEnumerable,154        /// the objects in the enumeration will be unrolled and155        /// written separately.  Otherwise, <paramref name="obj"/>156        /// will be written as a single object.157        /// </param>158        /// <returns>The number of objects written.</returns>159        /// <exception cref="PipelineClosedException">160        /// The underlying stream is closed161        /// </exception>162        /// <remarks>163        /// If the enumeration contains elements equal to164        /// AutomationNull.Value, they are ignored.165        /// This can cause the return value to be less than the size of166        /// the collection.167        /// </remarks>168        public override int Write(object obj, bool enumerateCollection)169        {170            return _stream.Write(obj, enumerateCollection);171        }172 173#if (false)174        /// <summary>175        /// Handle WriteReady events from the underlying stream.176        /// </summary>177        /// <param name="sender">The stream raising the event.</param>178        /// <param name="args">Standard event args.</param>179        private void OnWriteReady (object sender, EventArgs args)180        {181            if (WriteReady != null)182            {183                // call any event handlers on this, replacing the184                // ObjectStream sender with 'this' since receivers185                // are expecting an PipelineWriter186                WriteReady (this, args);187            }188        }189#endif190 191        #endregion Methods192 193        #region Private fields194 195        /// <summary>196        /// The underlying stream.197        /// </summary>198        /// <remarks>Can never be null</remarks>199        private readonly ObjectStreamBase _stream;200 201        #endregion Private Fields202    }203 204    /// <summary>205    /// A ObjectWriter for a PSDataCollection ObjectStream.206    /// </summary>207    /// <remarks>208    /// PSDataCollection is introduced after 1.0. PSDataCollection209    /// is used to store data from the last command in210    /// the pipeline and hence the writer will not211    /// support certain features like Flush().212    /// </remarks>213    internal class PSDataCollectionWriter<T> : ObjectWriter214    {215        #region Constructors216 217        /// <summary>218        /// Construct with an existing PSDataCollectionStream.219        /// </summary>220        /// <param name="stream">The stream to write.</param>221        /// <exception cref="ArgumentNullException">222        /// Thrown if the specified stream is null223        /// </exception>224        public PSDataCollectionWriter(PSDataCollectionStream<T> stream)225            : base(stream)226        {227        }228 229        #endregion230    }231}232