/// <inheriteddoc /> public HttpResponseBase Prefix(IEnumerable <byte> data) { lock (this._SYNC) { byte[] dataArray = CollectionHelper.AsArray(data); if (dataArray != null && dataArray.Length > 0) { using (MemoryStream backup = new MemoryStream()) { // backup this.Stream.Position = 0; IOHelper.CopyTo(this.Stream, backup); this.Clear(); this.Stream.Write(dataArray, 0, dataArray.Length); // restore backup.Position = 0; IOHelper.CopyTo(backup, this.Stream); } } } return(this); }
/// <inheriteddoc /> public ConsoleBase WriteLine(IEnumerable <char> format, params object[] args) { return(this.Write(string.Format("{0}{1}", string.Format(StringHelper.AsString(format) ?? string.Empty, CollectionHelper.AsArray(this.ToConsoleArguments(args))), this.GetNewLineForOutput()))); }
// Public Methods (16) /// <inheriteddoc /> public HttpResponseBase Append(IEnumerable <byte> data) { lock (this._SYNC) { byte[] dataArray = CollectionHelper.AsArray(data); if (dataArray != null && dataArray.Length > 0) { long lastPos = this.Stream.Position; try { // go to end this.Stream.Position = this.Stream.Length; this.Stream.Write(dataArray, 0, dataArray.Length); } finally { this.Stream.Position = lastPos; } } } return(this); }
/// <summary> /// Creates a new instance of the <see cref="DelegateLogCommand"/> class. /// </summary> /// <param name="executeLogCommand">The logic for <see cref="DelegateLogCommand.OnExecute(ILogCommandExecutionContext)" />.</param> /// <param name="canExecuteLogMessage">The logic for <see cref="DelegateLogCommand.CanExecute(ILogMessage)" />.</param> /// <param name="args">The execution arguments.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="executeLogCommand" /> is <see langword="null" />. /// </exception> public static DelegateLogCommand Create(ExecuteLogCommandHandler executeLogCommand, CanExecuteLogCommandHandler canExecuteLogMessage, IEnumerable <object> args) { return(Create(executeLogCommand, canExecuteLogMessage, CollectionHelper.AsArray(args))); }
/// <summary> /// Sets the data of this file. /// </summary> /// <param name="data">The data to set.</param> /// <returns>That instance.</returns> /// <remarks> /// If <paramref name="data" /> is <see langword="null" /> <see cref="SimpleFile.OpenStreamFunc" /> /// will also be set to <see langword="null" />. /// </remarks> public SimpleFile SetData(IEnumerable <byte> data) { this.OpenStreamFunc = data == null ? null : new OpenStreamHandler(delegate() { return(new MemoryStream(CollectionHelper.AsArray(data))); }); return(this); }
internal BinaryMessage(IEnumerable <byte> data) { this.Data = CollectionHelper.AsArray(data); this._HEADERS = new MessageHeaders(MessageVersion.None); this._PROPERTIES = new MessageProperties(); this.Properties .Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw)); }
/// <inheriteddoc /> public GeneralHasher(HashAlgorithm algo, IEnumerable <byte> salt) : base() { if (algo == null) { throw new ArgumentNullException("algo"); } this._ALGORITHM = algo; this._SALT = CollectionHelper.AsArray(salt); }
// Private Methods (1) private LogCommandExecutionContext CreateBasicExecutionContext(ILogMessage msg) { LogCommandExecutionContext result = new LogCommandExecutionContext(); result.Arguments = CollectionHelper.AsArray(this.GetExecutionArguments(msg)) ?? new object[0]; result.DoLogMessage = false; result.Command = this; result.Message = msg; result.MessageValueToLog = null; return(result); }
/// <inheriteddoc /> public HttpResponseBase Write(IEnumerable <byte> data) { byte[] dataArray = CollectionHelper.AsArray(data); if (dataArray != null) { lock (this._SYNC) { this.Stream .Write(dataArray, 0, dataArray.Length); } } return(this); }
public void AsArrayMethod() { int numberOfItems = this._RANDOM.Next(5000, 10000); IEnumerable <int> numbers = CollectionHelper.Range(0, numberOfItems); int[] numberArray = CollectionHelper.ToArray(numbers); Assert.AreNotSame(numbers, CollectionHelper.AsArray(numbers)); Assert.AreSame(numberArray, CollectionHelper.AsArray(numberArray)); }
// Public Methods (1) /// <summary> /// Creates a string list from a sequence of parameters. /// </summary> /// <param name="params">The parameter sequence.</param> /// <returns>The sequence as string.</returns> public static string CreateStringList(IEnumerable <ParameterInfo> @params) { var paramArray = CollectionHelper.AsArray(@params); if (paramArray != null) { if (paramArray.Length > 0) { return("(" + string.Join(",", paramArray.Select(p => TypeHelper.GetFullName(p.ParameterType))) + ")"); } } return(null); }
private void SaveResult() { try { var results = CollectionHelper.AsArray(this.Results); if (results.Length < 1) { return; } var dialog = new SaveFileDialog(); dialog.Filter = "XML files (*.xml)|*.xml|JSON files (*.json)|*.json|CSV files (*.csv; *.txt)|*.csv;*.txt|All files (*.*)|*.*"; dialog.OverwritePrompt = true; if (dialog.ShowDialog().IsNotTrue()) { return; } var outputFile = new FileInfo(dialog.FileName); Action <Stream, IList <ICompareResult> > actionToInvoke; switch ((Path.GetExtension(outputFile.Name) ?? string.Empty).ToLower().Trim()) { case ".json": actionToInvoke = this.SaveResult_Json; break; case ".csv": case ".txt": actionToInvoke = this.SaveResult_PlainText; break; default: actionToInvoke = this.SaveResult_Xml; break; } using (var stream = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.ReadWrite)) { actionToInvoke(stream, results); } } catch (Exception ex) { this.OnError(ex); } }
// Public Methods (3) /// <inheriteddoc /> public byte[] Hash(IEnumerable <byte> data) { if (data == null) { throw new ArgumentNullException("data"); } using (MemoryStream srcStream = new MemoryStream(CollectionHelper.AsArray(data), false)) { using (MemoryStream targetStream = new MemoryStream()) { this.OnHash(srcStream, targetStream); return(targetStream.ToArray()); } } }
/// <inheriteddoc /> public override void WriteLine(string format, params object[] arg) { IEnumerable <object> parsedArg = null; if (arg != null) { parsedArg = CollectionHelper.Select(arg, delegate(object i) { return(this.ParseObject(i)); }); } this.InvokeActionForWriter(delegate(TextWriter writer, string f, object[] a) { writer.WriteLine(f, a); }, format , CollectionHelper.AsArray(parsedArg) ?? new object[0]); }
/// <summary> /// /// </summary> /// <see href="http://msdn.microsoft.com/en-us/library/dd414746%28v=vs.110%29.aspx" /> public AggregateException(string message, IEnumerable <Exception> innerExceptions) : base(message) { if (innerExceptions == null) { throw new ArgumentNullException("innerExceptions"); } this._INNER_EXCEPTIONS = innerExceptions as ReadOnlyCollection <Exception>; if (this._INNER_EXCEPTIONS == null) { // needs to be converted IList <Exception> list = innerExceptions as IList <Exception>; if (list == null) { list = CollectionHelper.AsArray(innerExceptions); } this._INNER_EXCEPTIONS = new ReadOnlyCollection <Exception>(list); } }
// Public Methods (1) /// <summary> /// /// </summary> /// <see cref="ILogCommand.Execute(ILogMessage)"/> public new ILogCommandExecutionResult Execute(ILogMessage orgMsg) { if (!this.CanExecute(orgMsg)) { return(null); } LogCommandExecutionResult result = new LogCommandExecutionResult(); result.Command = this; result.Message = orgMsg; try { LogCommandExecutionContext ctx = this.CreateBasicExecutionContext(orgMsg); this.OnExecute(ctx); result.Errors = new Exception[0]; result.DoLogMessage = ctx.DoLogMessage; result.MessageValueToLog = ctx.MessageValueToLog; } catch (Exception ex) { AggregateException aggEx = ex as AggregateException; if (aggEx == null) { aggEx = new AggregateException(new Exception[] { ex }); } result.Errors = CollectionHelper.AsArray(aggEx.Flatten().InnerExceptions); result.DoLogMessage = false; result.MessageValueToLog = null; } return(result); }
private void GetRandomBytes(byte[] data) { if (data == null) { throw new ArgumentNullException("data"); } if (data.Length < 1) { return; } this._RNG.GetBytes(data); byte[] seed = CollectionHelper.AsArray(this._PROVIDER(this)); if ((seed == null) || (seed.Length < 1)) { return; } for (int i = 0; i < data.Length; i++) { try { unchecked { data[i] = (byte)(data[i] ^ seed[i % seed.Length]); } } catch { // ignore } } }
/// <summary> /// /// </summary> /// <see cref="CollectionHelper.AsArray(IEnumerable)" /> public static object[] AsArray(this IEnumerable seq) { return(CollectionHelper.AsArray(seq)); }
// Public Methods (2) /// <summary> /// /// </summary> /// <see cref="CollectionHelper.AsArray{T}(IEnumerable{T})" /> public static T[] AsArray <T>(this IEnumerable <T> seq) { return(CollectionHelper.AsArray <T>(seq)); }
// Public Methods (7) /// <inheriteddoc /> public object Execute(IEnumerable <object> args) { return(this.Execute(CollectionHelper.AsArray(args))); }
/// <summary> /// /// </summary> /// <see cref="IHttpModule.HandleRequest(IHttpRequestContext)" /> public IHttpModuleHandleRequestResult HandleRequest(IHttpRequestContext context) { if (context == null) { throw new ArgumentNullException("context"); } HttpModuleHandleRequestResult result = new HttpModuleHandleRequestResult(); result.RequestContext = context; try { BeforeHandleRequestContext beforeHandleCtx = new BeforeHandleRequestContext(); { beforeHandleCtx.Http = context; beforeHandleCtx.InvokeAfterHandleRequest = true; beforeHandleCtx.InvokeHandleRequest = true; this.OnBeforeHandleRequest(beforeHandleCtx); } HandleRequestContext handleCtx = new HandleRequestContext(); bool requestWasHandled = false; { handleCtx.Http = context; handleCtx.InvokeAfterHandleRequest = beforeHandleCtx.InvokeAfterHandleRequest; if (beforeHandleCtx.InvokeHandleRequest) { this.OnHandleRequest(handleCtx); requestWasHandled = true; } } if (handleCtx.InvokeAfterHandleRequest) { AfterHandleRequestContext afterHandleCtx = new AfterHandleRequestContext(); afterHandleCtx.RequestWasHandled = requestWasHandled; { afterHandleCtx.Http = context; this.OnAfterHandleRequest(afterHandleCtx); } } result.Errors = new Exception[0]; } catch (Exception ex) { AggregateException aggEx = ex as AggregateException; if (aggEx == null) { aggEx = new AggregateException(new Exception[] { ex }); } result.Errors = CollectionHelper.AsArray(aggEx.Flatten().InnerExceptions); } return(result); }
public void Start() { lock (this._SYNC) { this.IsCancelling = false; this.Failed = false; this.Canceled = false; this.Errors = null; List <Exception> occuredExceptions = new List <Exception>(); try { this.IsRunning = true; this.RaiseEventHandler(this.Started); this.OnProgress(new StreamCopyProgressEventArgs(this._COPIER, 0, 0)); byte[] buffer = new byte[this._COPIER._BUFFER_SIZE]; long totalCopied = 0; while (true) { int bytesRead = 0; try { bytesRead = this._COPIER.Source.Read(buffer, 0, buffer.Length); if (bytesRead < 1) { break; } if (this.IsCancelling) { break; } StreamCopyBeforeWriteEventArgs bwe = new StreamCopyBeforeWriteEventArgs(CollectionHelper.Take(buffer, bytesRead), this._COPIER, totalCopied); this.OnBeforeWrite(bwe); if (bwe.Cancel) { this.IsCancelling = true; break; } if (bwe.Skip) { // skip write operation continue; } int bytesCopied = 0; byte[] dataToWrite = CollectionHelper.AsArray(bwe.Data); if (dataToWrite != null) { if (dataToWrite.Length > 0) { bytesCopied = dataToWrite.Length; this._COPIER.Destination.Write(dataToWrite, 0, bytesCopied); totalCopied += bytesCopied; } } StreamCopyProgressEventArgs e = new StreamCopyProgressEventArgs(this._COPIER, bytesCopied, totalCopied); this.OnProgress(e); if (e.Cancel) { this.IsCancelling = true; break; } } catch (Exception ex) { StreamCopyProgressErrorEventArgs e = new StreamCopyProgressErrorEventArgs(ex, this._COPIER, bytesRead, totalCopied); e.Cancel = true; e.LogErrors = true; this.OnProgressError(e); if (e.LogErrors) { occuredExceptions.Add(e.Errors); } if (e.Handled == false) { // (re-)throw throw e.Errors; } if (e.Cancel) { this.IsCancelling = true; break; } } } } catch (Exception ex) { occuredExceptions.Add(ex); this.Failed = true; } finally { this.Errors = new ReadOnlyCollection <Exception>(occuredExceptions); if (this.IsCancelling) { this.Canceled = true; } this.IsCancelling = false; this.IsRunning = false; this.RaiseEventHandler(this.Completed); } } }