/// <summary> /// Copy the contents of one <see cref="Stream"/> to another. /// </summary> /// <param name="source">The stream to source data from.</param> /// <param name="destination">The stream to write data to.</param> /// <param name="buffer">The buffer to use during copying.</param> /// <remarks>This is taken from SharpZipLib.Core.StreamUtils and modified for progress callbacks.</remarks> protected void Copy(Stream source, Stream destination, byte[] buffer) { PscxArgumentException.ThrowIfIsNull(source, "source"); PscxArgumentException.ThrowIfIsNull(destination, "destination"); PscxArgumentException.ThrowIfIsNull(buffer, "buffer"); PscxArgumentException.ThrowIf(buffer.Length < 128, "Buffer is too small"); long sourceLength = source.Length; long bytesWritten = 0; bool copying = true; Debug.WriteLine("StartCopy", "WriterBase.Copy"); while (copying) { int bytesRead = source.Read(buffer, 0, buffer.Length); if (bytesRead > 0) { destination.Write(buffer, 0, bytesRead); bytesWritten += bytesRead; OnWriteProgress(sourceLength, bytesWritten); } else { destination.Flush(); copying = false; } } OnWriteComplete(); Debug.WriteLine("EndCopy", "WriterBase.Copy"); }
/// <summary> /// /// </summary> /// <param name="literalPath"></param> /// <param name="session"></param> internal UnresolvedPscxPathImpl(string literalPath, SessionState session) { PscxArgumentException.ThrowIfIsNullOrEmpty(literalPath); PscxArgumentException.ThrowIfIsNull(session); _providerPath = session.Path.GetUnresolvedProviderPathFromPSPath(literalPath, out _providerInfo, out _driveInfo); _isUnresolved = true; _sourcePath = literalPath; }
protected BinaryParser OpenSection(PESection section) { PscxArgumentException.ThrowIfIsNull(section); uint index = (section.PointerToRawData); uint count = (section.SizeOfRawData); return(new BinaryParser(OpenImage(index))); }
public static Cookie Enter(object key, object value) { PscxArgumentException.ThrowIfIsNull(key); Cookie cookie = new Cookie(key, Current(key)); _context[key] = value; return(cookie); }
public void Remove(string[] values) { PscxArgumentException.ThrowIfIsNull(values); EnsureValuesLoaded(); for (int i = 0; i < values.Length; i++) { Remove(values[i]); } }
public void Prepend(string[] values) { PscxArgumentException.ThrowIfIsNull(values); EnsureValuesLoaded(); for (int i = values.Length - 1; i >= 0; i--) { Prepend(values[i]); } }
public RecordParser(Type recordType) { var ctor = recordType.GetConstructor(new Type[0]); var attr = BinaryParser.GetAttribute <BinaryRecordAttribute>(recordType); PscxArgumentException.ThrowIfIsNull(ctor, "type {0} does not have default constructor", recordType); PscxArgumentException.ThrowIfIsNull(attr, "type {0} does not have BinaryRecordAttribute", recordType); _type = recordType; _pack = attr.Pack; }
public static DirectoryEntry NewItem(DirectoryEntry parent, string name, string classNamePrefix) { PscxArgumentException.ThrowIfIsNull(parent, "parent"); DirectoryEntryType entryType = FindByPrefix(classNamePrefix); if (entryType == null) { return(null); } return(entryType.NewItem(parent, name)); }
protected static string GetProviderFullName(ProviderInfo provider) { PscxArgumentException.ThrowIfIsNull(provider); string name = provider.Name; if (provider.PSSnapIn != null) { string snapInName = provider.PSSnapIn.Name; if (!string.IsNullOrEmpty(snapInName)) { name = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", snapInName, provider.Name); } } return(name); }
/// <summary> /// Registers a delegate called when specified type of object /// is read from the pipeline. /// </summary> /// <remarks> /// You may register an interface type too, but they are processed only when /// there is no handler available for the actual type. /// </remarks> /// <example> /// <para>For example, if you register the following types, the IDisposable handler /// will be executed only on objects which do not inherit from Stream.</para> /// <code> /// RegisterInputType<IDisposable>(/* ... */); /// RegisterInputType<FileStream>(/* ... */); /// RegisterInputType<Stream>(/* ... */); /// </code> /// </example> /// <typeparam name="T">The type which should be processed. </typeparam> /// <param name="action"> /// The delegate that is called when the object is available. /// </param> protected void RegisterInputType <T>(Action <T> action) { PscxArgumentException.ThrowIfIsNull(action, "To ignore an input type, use IgnoreInputType<T>()."); PscxArgumentException.ThrowIf(typeof(T) == typeof(Object), "You cannot register an action for Object. Override the ProcessInputObject method instead."); PscxArgumentException.ThrowIf(typeof(T) == typeof(PSObject), "You cannot register an action for PSObject. Override the ProcessInputObject method instead."); if (_recurseIEnumerable) { PscxArgumentException.ThrowIf(typeof(T) == typeof(IEnumerable), "You cannot register an action for IEnumerable when ProcessIEnumerableRecursively is true."); } SetTypeAction(typeof(T), action); }
public static DosHeader Parse(BinaryReader br) { PscxArgumentException.ThrowIfIsNull(br); ushort signature = br.ReadUInt16(); if (DosSignature != signature) { InvalidPEFileException.ThrowInvalidDosHeader(); } DosHeader hdr = new DosHeader(); hdr.BytesOnLastPage = br.ReadUInt16(); hdr.PageCount = br.ReadUInt16(); hdr.RelocationCount = br.ReadUInt16(); hdr.HeaderSize = br.ReadUInt16(); hdr.MinExtraParagraphs = br.ReadUInt16(); hdr.MaxExtraParagraphs = br.ReadUInt16(); hdr.InitialSS = br.ReadUInt16(); hdr.InitialSP = br.ReadUInt16(); hdr.Checksum = br.ReadUInt16(); hdr.InitialIP = br.ReadUInt16(); hdr.InitialCS = br.ReadUInt16(); hdr.RelocationTableOffset = br.ReadUInt16(); hdr.OverlayNumber = br.ReadUInt16(); // reserved words for (int i = 0; i < 4; i++) { br.ReadUInt16(); } hdr.OemID = br.ReadUInt16(); hdr.OemInfo = br.ReadUInt16(); // reserved words for (int i = 0; i < 10; i++) { br.ReadUInt16(); } hdr.CoffHeaderOffset = br.ReadUInt32(); return(hdr); }
private Delegate CreateDelegate(Type type) { if (type.IsAbstract) { PscxArgumentOutOfRangeException.Throw("Type {0} is abstract.", type.Name); } // need a target for delegate PscxArgumentException.ThrowIf(this._arguments.Length == 0, "Please supply a ScriptBlock as an argument."); // must be a scriptblock ScriptBlock target = this._arguments[0] as ScriptBlock; PscxArgumentException.ThrowIfIsNull(target, "The only argument in ArgumentList must be a ScriptBlock."); Delegate invoker = GetScriptBlockDelegate(target, type); return(invoker); }
protected static string GetDriveQualifiedPath(string path, PSDriveInfo drive) { PscxArgumentException.ThrowIfIsNullOrEmpty(path); PscxArgumentException.ThrowIfIsNull(drive); string qualifiedPath = path; bool unqualified = true; int index = path.IndexOf(':'); if (index != -1) { if (string.Equals(path.Substring(0, index), drive.Name, StringComparison.OrdinalIgnoreCase)) { unqualified = false; } } if (unqualified) { const char separator = '\\'; string format = "{0}:" + separator + "{1}"; if (path.StartsWith(separator.ToString(), StringComparison.Ordinal)) { format = "{0}:{1}"; } // strip root if (!String.IsNullOrEmpty(drive.Root)) { if (path.StartsWith(drive.Root)) { path = path.Substring(drive.Root.Length + 1); // grab trailing slash } } qualifiedPath = string.Format(CultureInfo.InvariantCulture, format, drive.Name, path); } return(qualifiedPath); }
protected static string GetProviderQualifiedPath(string path, ProviderInfo provider) { PscxArgumentException.ThrowIfIsNullOrEmpty(path); PscxArgumentException.ThrowIfIsNull(provider); string qualifiedPath = path; bool isProviderQualified = false; int index = path.IndexOf("::", StringComparison.Ordinal); if (index != -1) { string providerName = path.Substring(0, index); if (CompareProviderNames(provider.Name, providerName) == true) { isProviderQualified = true; } } if (!isProviderQualified) { qualifiedPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", GetProviderFullName(provider), "::", path); } return(qualifiedPath); }
public static PscxThreadContext.Cookie Enter(TProvider provider) { PscxArgumentException.ThrowIfIsNull(provider); return(PscxThreadContext.Enter(typeof(TProvider), provider)); }
// this took blood, sweat and tears AND my first born. private static Delegate GetScriptBlockDelegate(ScriptBlock block, Type delegateType) { PscxArgumentException.ThrowIfIsNull(block, "block"); PscxArgumentException.ThrowIfIsNull(delegateType, "block"); bool isDelegate = typeof(Delegate).IsAssignableFrom(delegateType); PscxArgumentException.ThrowIf((!isDelegate) || (delegateType.IsAbstract), "Invalid delegateType: {0}", delegateType.Name); MethodInfo invoke = delegateType.GetMethod("Invoke"); Debug.Assert(invoke != null, "delegate invoke != null"); ParameterInfo[] parameters = invoke.GetParameters(); Type returnType = invoke.ReturnParameter.ParameterType; List <Type> args = new List <Type>(); args.Add(typeof(ScriptBlock)); // first argument is instance foreach (ParameterInfo parameter in parameters) { args.Add(parameter.ParameterType); } DynamicMethod method = new DynamicMethod(String.Empty, returnType, args.ToArray(), typeof(ScriptBlock).Module); ILGenerator ilgen = method.GetILGenerator(); LocalBuilder[] locals = new LocalBuilder[2] { ilgen.DeclareLocal(typeof(object[]), true), ilgen.DeclareLocal(typeof(object[]), true) }; ilgen.Emit(OpCodes.Ldc_I4, parameters.Length); ilgen.Emit(OpCodes.Newarr, typeof(object)); ilgen.Emit(OpCodes.Stloc, 1); for (int index = 1; index < args.Count; index++) { ilgen.Emit(OpCodes.Ldloc, 1); EmitFastPushInt(ilgen, (index - 1)); // e.g. Ldc_I4_1 ilgen.Emit(OpCodes.Ldarg, index); if (args[index].IsValueType) { ilgen.Emit(OpCodes.Box, args[index]); } ilgen.Emit(OpCodes.Stelem_Ref); } ilgen.Emit(OpCodes.Ldloc_1); ilgen.Emit(OpCodes.Stloc_0); ilgen.Emit(OpCodes.Ldarg_0); // this ilgen.Emit(OpCodes.Ldloc_0); // object[] args for script block ilgen.EmitCall(OpCodes.Callvirt, typeof(ScriptBlock).GetMethod("InvokeReturnAsIs"), null); if (invoke.ReturnType == typeof(void)) { ilgen.Emit(OpCodes.Pop); } else { // need to convert return type to the target delegate's return type // ... } ilgen.Emit(OpCodes.Ret); return(method.CreateDelegate(delegateType, block)); }