/// <summary>Retrieves the first item that matches the given filter (or null if there is no match).</summary> /// <param name="collection">The collection to examine.</param> /// <param name="predicate">The predicate to match.</param> public object First(IEnumerable collection, FuncBool predicate) { if (Script.IsNullOrUndefined(collection)) return null; foreach (object item in collection) { if (predicate(item)) return item; } return null; }
/// <summary>Constructs a subset of the collection based on the response of an include-filter.</summary> /// <param name="collection">The collection to filter.</param> /// <param name="predicate">The predicate to match.</param> public ArrayList Filter(IEnumerable collection, FuncBool predicate) { ArrayList list = new ArrayList(); foreach (object item in collection) { if (predicate(item)) list.Add(item); } return list; }
/// <summary>Gets the total number of items that match the given predicate.</summary> /// <param name="collection">The collection to examine.</param> /// <param name="predicate">The predicate to match.</param> public int Total(IEnumerable collection, FuncBool predicate) { if (Script.IsNullOrUndefined(collection)) return 0; int count = 0; foreach (object item in collection) { if (predicate(item)) count++; } return count; }
/// <summary> /// A general HTTP PUT method, with delegates for progress and cancelling. May throw various exceptions. /// </summary> /// <param name="progressDelegate">Delegate called periodically (500ms) with percent complete</param> /// <param name="cancellingDelegate">Delegate called periodically to see if need to cancel</param> /// <param name="uri">URI to PUT to</param> /// <param name="proxy">A proxy to handle the HTTP connection</param> /// <param name="path">Path to file to put</param> /// <param name="timeout_ms">Timeout for the connection in ms. 0 for no timeout.</param> public static void Put(UpdateProgressDelegate progressDelegate, FuncBool cancellingDelegate, Uri uri, IWebProxy proxy, string path, int timeout_ms) { using (Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read), requestStream = PUT(uri, proxy, fileStream.Length, timeout_ms)) { long len = fileStream.Length; DataCopiedDelegate dataCopiedDelegate = delegate(long bytes) { if (progressDelegate != null && len > 0) { progressDelegate((int)((bytes * 100) / len)); } }; CopyStream(fileStream, requestStream, dataCopiedDelegate, cancellingDelegate); } }
public static void Put(UpdateProgressDelegate progressDelegate, FuncBool cancellingDelegate, Uri uri, IWebProxy proxy, string path, int timeout_ms) { using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (Stream stream2 = PUT(uri, proxy, stream.Length, timeout_ms)) { long len = stream.Length; DataCopiedDelegate delegate2 = delegate(long bytes) { if ((progressDelegate != null) && (len > 0L)) { progressDelegate((int)((bytes * 100L) / len)); } }; CopyStream(stream, stream2, delegate2, cancellingDelegate); } } }
/// <summary>Gets the first descendent node that matches the given predicate.</summary> /// <param name="parent">The parent to look within.</param> /// <param name="predicate">The predicate used to match.</param> /// <returns></returns> public ITreeNode FirstDescendent(ITreeNode parent, FuncBool predicate) { // Setup initial conditions. if (parent == null || predicate == null) return null; // Look for item in direct children. object item = Helper.Collection.First(parent.Children, predicate); if (item != null) return item as ITreeNode; // Not found - recursively call back for each child. foreach (ITreeNode child in parent.Children) { ITreeNode descendent = FirstDescendent(child, predicate); if (descendent != null) return descendent; } // Finish up (not found). return null; }
/// <summary> /// A general HTTP GET method, with delegates for progress and cancelling. May throw various exceptions. /// </summary> /// <param name="dataRxDelegate">Delegate called periodically (500 ms) with the number of bytes transferred</param> /// <param name="cancellingDelegate">Delegate called periodically to see if need to cancel</param> /// <param name="uri">URI to GET from</param> /// <param name="proxy">A proxy to handle the HTTP connection</param> /// <param name="path">Path to file to receive the data</param> /// <param name="timeout_ms">Timeout for the connection in ms. 0 for no timeout.</param> public static void Get(DataCopiedDelegate dataCopiedDelegate, FuncBool cancellingDelegate, Uri uri, IWebProxy proxy, string path, int timeout_ms) { string tmpFile = Path.GetTempFileName(); try { using (Stream fileStream = new FileStream(tmpFile, FileMode.Create, FileAccess.Write, FileShare.None), downloadStream = GET(uri, proxy, timeout_ms)) { CopyStream(downloadStream, fileStream, dataCopiedDelegate, cancellingDelegate); fileStream.Flush(); } File.Delete(path); File.Move(tmpFile, path); } finally { File.Delete(tmpFile); } }
public static void Get(DataCopiedDelegate dataCopiedDelegate, FuncBool cancellingDelegate, Uri uri, IWebProxy proxy, string path, int timeout_ms) { string tempFileName = Path.GetTempFileName(); try { using (Stream stream = new FileStream(tempFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { using (Stream stream2 = GET(uri, proxy, timeout_ms)) { CopyStream(stream2, stream, dataCopiedDelegate, cancellingDelegate); stream.Flush(); } } System.IO.File.Delete(path); System.IO.File.Move(tempFileName, path); } finally { System.IO.File.Delete(tempFileName); } }
public static long CopyStream(Stream inStream, Stream outStream, DataCopiedDelegate progressDelegate, FuncBool cancellingDelegate) { long bytesWritten = 0; byte[] buffer = new byte[BUFFER_SIZE]; DateTime lastUpdate = DateTime.Now; while (cancellingDelegate == null || !cancellingDelegate()) { int bytesRead = inStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outStream.Write(buffer, 0, bytesRead); bytesWritten += bytesRead; if (progressDelegate != null && DateTime.Now - lastUpdate > TimeSpan.FromMilliseconds(500)) { progressDelegate(bytesWritten); lastUpdate = DateTime.Now; } } if (cancellingDelegate != null && cancellingDelegate()) { throw new CancelledException(); } if (progressDelegate != null) { progressDelegate(bytesWritten); } return(bytesWritten); }
/// <summary> /// A general HTTP PUT method, with delegates for progress and cancelling. May throw various exceptions. /// </summary> /// <param name="progressDelegate">Delegate called periodically (500ms) with percent complete</param> /// <param name="cancellingDelegate">Delegate called periodically to see if need to cancel</param> /// <param name="uri">URI to PUT to</param> /// <param name="proxy">A proxy to handle the HTTP connection</param> /// <param name="path">Path to file to put</param> /// <param name="timeout_ms">Timeout for the connection in ms. 0 for no timeout.</param> public static void Put(UpdateProgressDelegate progressDelegate, FuncBool cancellingDelegate, Uri uri, IWebProxy proxy, string path, int timeout_ms) { using (Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read), requestStream = PUT(uri, proxy, fileStream.Length, timeout_ms)) { long len = fileStream.Length; DataCopiedDelegate dataCopiedDelegate = delegate(long bytes) { if (progressDelegate != null && len > 0) progressDelegate((int)((bytes * 100) / len)); }; CopyStream(fileStream, requestStream, dataCopiedDelegate, cancellingDelegate); } }
public static long CopyStream(Stream inStream, Stream outStream, DataCopiedDelegate progressDelegate, FuncBool cancellingDelegate) { long bytesWritten = 0; byte[] buffer = new byte[BUFFER_SIZE]; DateTime lastUpdate = DateTime.Now; while (cancellingDelegate == null || !cancellingDelegate()) { int bytesRead = inStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; outStream.Write(buffer, 0, bytesRead); bytesWritten += bytesRead; if (progressDelegate != null && DateTime.Now - lastUpdate > TimeSpan.FromMilliseconds(500)) { progressDelegate(bytesWritten); lastUpdate = DateTime.Now; } } if (cancellingDelegate != null && cancellingDelegate()) throw new CancelledException(); if (progressDelegate != null) progressDelegate(bytesWritten); return bytesWritten; }
public static long CopyStream(Stream inStream, Stream outStream, DataCopiedDelegate progressDelegate, FuncBool cancellingDelegate) { long bytes = 0L; byte[] buffer = new byte[0x8000]; DateTime now = DateTime.Now; while ((cancellingDelegate == null) || !cancellingDelegate()) { int count = inStream.Read(buffer, 0, buffer.Length); if (count == 0) { break; } outStream.Write(buffer, 0, count); bytes += count; if ((progressDelegate != null) && ((DateTime.Now - now) > TimeSpan.FromMilliseconds(500.0))) { progressDelegate(bytes); now = DateTime.Now; } } if ((cancellingDelegate != null) && cancellingDelegate()) { throw new CancelledException(); } if (progressDelegate != null) { progressDelegate(bytes); } return(bytes); }