/// <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); } }
/// <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); }
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; }