Пример #1
0
        public static TimeSpan CalculateOperationTransferTime(StreamHelperProgressEvent e)
        {
            // calc transfertime
            if (e.TransferRateTotal != -1 && e.TransferRateTotal > 0)
            {
                var bytesPerSecond = (e.TransferRateTotal / 8) * 1000;

                if (bytesPerSecond > 0)
                {
                    var neededSeconds = (e.TotalLength - e.ReadBytesTotal) / bytesPerSecond;
                    return(new TimeSpan(neededSeconds * TimeSpan.TicksPerSecond));
                }

                return(new TimeSpan(long.MaxValue));
            }

            return(new TimeSpan(long.MaxValue));
        }
Пример #2
0
        public static StreamHelperResult CopyStreamData(Object sender, Stream src, Stream trg, long MaxSize, StreamHelperProgressCallback status, params Object[] data)
        {
            // validate parameter
            if (src == null || trg == null)
                return new StreamHelperResult() { ResultCode = StreamHelperResultCodes.InvalidParameter };

            if (src.CanRead == false || trg.CanWrite == false)
                return new StreamHelperResult() { ResultCode = StreamHelperResultCodes.InvalidParameter };

            // build the buffer as configured
            byte[] buffer = new byte[_BufferSize];

            // set the real buffer size
            int RealBufferSize = _BufferSize;

            // build the event for the status callback
            StreamHelperProgressEvent e = new StreamHelperProgressEvent();

            // copy the stream data
            int readBytes = 0;
            int readBytesTotal = 0;
            int readBytes500msFrame = 0;

            DateTime dtStart = DateTime.Now;
            DateTime dt500MsWatch = DateTime.Now;
            TimeSpan ts500MsWatch = new TimeSpan();

            // set the total length if possible
            try
            {
                e.TotalLength = src.Length;
            }
            catch (Exception)
            {
                if (MaxSize != -1)
                    e.TotalLength = MaxSize;
                else
                    e.TotalLength = -1;
            }

            if (MaxSize != -1 && e.TotalLength > MaxSize)
                e.TotalLength = MaxSize;

            do
            {
                // Read the bytes
                readBytes = src.Read(buffer, 0, RealBufferSize);

                // add
                readBytesTotal += readBytes;
                readBytes500msFrame += readBytes;

                // check for interuption
                if (readBytes <= 0)
                    break;

                // Write the bytes
                trg.Write(buffer, 0, readBytes);
                
                // notify state
                if (status != null)
                {
                    StreamHelperResultCodes action = StreamHelperResultCodes.OK;

                    // upadte the event                    
                    e.ReadBytesTotal = readBytesTotal;
                    e.ReadBytesCurrentOperation = readBytes;                    
                    
                    // call the callback
                    action = status(sender, e, data);                        

                    // result
                    if (action == StreamHelperResultCodes.Aborted)
                        return new StreamHelperResult() { ResultCode = StreamHelperResultCodes.Aborted, TransferedBytes = readBytesTotal };
                }          
      
                // stop measurement
                DateTime dtLocalStop = DateTime.Now;
                
                // set the 500 ms span 
                ts500MsWatch = dtLocalStop - dt500MsWatch;

                // check if we achieved 500 ms
                if (ts500MsWatch.TotalMilliseconds >= 500)
                {                    
                    // update the current transfer rate                    
                    e.TransferRateCurrent = readBytes500msFrame / Convert.ToInt64(ts500MsWatch.TotalMilliseconds);
                    // bits per millisecond == kbits per second
                    e.TransferRateCurrent *= 8;

                    // reset the bytes
                    readBytes500msFrame = 0;

                    // reset the timespan
                    ts500MsWatch = new TimeSpan();

                    // reset the start timer
                    dt500MsWatch = DateTime.Now;                   
                }

                // recalc the overall transfer rate
                TimeSpan consumedTimeAllOver = dtLocalStop - dtStart;

                if (Convert.ToInt64(consumedTimeAllOver.TotalMilliseconds) > 0)
                {
                    // bytes per millisecond
                    e.TransferRateTotal = readBytesTotal / Convert.ToInt64(consumedTimeAllOver.TotalMilliseconds);

                    // bits per millisecond == kbits per second
                    e.TransferRateTotal *= 8;                    
                }
                else
                    e.TransferRateTotal = -1;

                // check the max size 
                if (MaxSize != -1)
                {
                    if (readBytesTotal >= MaxSize)
                        break;

                    // check if we have to asjust the buffer size
                    if (MaxSize - readBytesTotal < RealBufferSize)
                    {
                        RealBufferSize = Convert.ToInt32(MaxSize - readBytesTotal);
                    }
                }

                // check if we have 
            } while (readBytes > 0);

            return new StreamHelperResult() { ResultCode = StreamHelperResultCodes.OK, TransferedBytes = readBytesTotal };
        }
Пример #3
0
        public static TimeSpan CalculateOperationTransferTime(StreamHelperProgressEvent e)
        {            
            // calc transfertime            
            if (e.TransferRateTotal != -1 && e.TransferRateTotal > 0)
            {
                long bytesPerSecond = (e.TransferRateTotal / 8) * 1000;

                if (bytesPerSecond > 0)
                {
                    long neededSeconds = (e.TotalLength - e.ReadBytesTotal) / bytesPerSecond;
                    return new TimeSpan(neededSeconds * TimeSpan.TicksPerSecond);
                }
                else
                    return new TimeSpan(long.MaxValue);
            }
            else
                return new TimeSpan(long.MaxValue);                       
        }
Пример #4
0
        public static StreamHelperResult CopyStreamData(Object sender, Stream src, Stream trg, long MaxSize, StreamHelperProgressCallback status, params Object[] data)
        {
            // validate parameter
            if (src == null || trg == null)
            {
                return new StreamHelperResult {
                           ResultCode = StreamHelperResultCodes.InvalidParameter
                }
            }
            ;

            if (src.CanRead == false || trg.CanWrite == false)
            {
                return new StreamHelperResult {
                           ResultCode = StreamHelperResultCodes.InvalidParameter
                }
            }
            ;

            // build the buffer as configured
            var buffer = new byte[BufferSize];

            // set the real buffer size
            var RealBufferSize = BufferSize;

            // build the event for the status callback
            var e = new StreamHelperProgressEvent();

            // copy the stream data
            int readBytes;
            var readBytesTotal      = 0;
            var readBytes500msFrame = 0;

            var dtStart      = DateTime.Now;
            var dt500MsWatch = DateTime.Now;
            var ts500MsWatch = new TimeSpan();

            // set the total length if possible
            try
            {
                e.TotalLength = src.Length;
            }
            catch (Exception)
            {
                if (MaxSize != -1)
                {
                    e.TotalLength = MaxSize;
                }
                else
                {
                    e.TotalLength = -1;
                }
            }

            if (MaxSize != -1 && e.TotalLength > MaxSize)
            {
                e.TotalLength = MaxSize;
            }

            do
            {
                // Read the bytes
                readBytes = src.Read(buffer, 0, RealBufferSize);

                // add
                readBytesTotal      += readBytes;
                readBytes500msFrame += readBytes;

                // check for interuption
                if (readBytes <= 0)
                {
                    break;
                }

                // Write the bytes
                trg.Write(buffer, 0, readBytes);

                // notify state
                if (status != null)
                {
                    // upadte the event
                    e.ReadBytesTotal            = readBytesTotal;
                    e.ReadBytesCurrentOperation = readBytes;

                    // call the callback
                    var action = status(sender, e, data);

                    // result
                    if (action == StreamHelperResultCodes.Aborted)
                    {
                        return new StreamHelperResult {
                                   ResultCode = StreamHelperResultCodes.Aborted, TransferedBytes = readBytesTotal
                        }
                    }
                    ;
                }

                // stop measurement
                var dtLocalStop = DateTime.Now;

                // set the 500 ms span
                ts500MsWatch = dtLocalStop - dt500MsWatch;

                // check if we achieved 500 ms
                if (ts500MsWatch.TotalMilliseconds >= 500)
                {
                    // update the current transfer rate
                    e.TransferRateCurrent = readBytes500msFrame / Convert.ToInt64(ts500MsWatch.TotalMilliseconds);
                    // bits per millisecond == kbits per second
                    e.TransferRateCurrent *= 8;

                    // reset the bytes
                    readBytes500msFrame = 0;

                    // reset the timespan
                    ts500MsWatch = new TimeSpan();

                    // reset the start timer
                    dt500MsWatch = DateTime.Now;
                }

                // recalc the overall transfer rate
                var consumedTimeAllOver = dtLocalStop - dtStart;

                if (Convert.ToInt64(consumedTimeAllOver.TotalMilliseconds) > 0)
                {
                    // bytes per millisecond
                    e.TransferRateTotal = readBytesTotal / Convert.ToInt64(consumedTimeAllOver.TotalMilliseconds);

                    // bits per millisecond == kbits per second
                    e.TransferRateTotal *= 8;
                }
                else
                {
                    e.TransferRateTotal = -1;
                }

                // check the max size
                if (MaxSize != -1)
                {
                    if (readBytesTotal >= MaxSize)
                    {
                        break;
                    }

                    // check if we have to asjust the buffer size
                    if (MaxSize - readBytesTotal < RealBufferSize)
                    {
                        RealBufferSize = Convert.ToInt32(MaxSize - readBytesTotal);
                    }
                }

                // check if we have
            } while (readBytes > 0);

            return(new StreamHelperResult {
                ResultCode = StreamHelperResultCodes.OK, TransferedBytes = readBytesTotal
            });
        }
        /// <summary>
        /// Just a helper for the stream copy process
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pe"></param>        
        /// <param name="data"></param>
        /// <returns></returns>
        internal static StreamHelperResultCodes FileStreamCopyCallback(object sender, StreamHelperProgressEvent pe, params Object[] data)
        {
            // check array
            if (data.Length != 3)
                return StreamHelperResultCodes.OK;

            // get the progess delegate
            FileOperationProgressChanged pc = data[0] as FileOperationProgressChanged;
            if (pc == null)
                return StreamHelperResultCodes.OK;

            // get the file
            ICloudFileSystemEntry e = data[1] as ICloudFileSystemEntry;
            if (e == null)
                return StreamHelperResultCodes.OK;

            // get the progress context
            Object progressContext = data[2];
            
            // create the eventargs element
            FileDataTransferEventArgs arg = new FileDataTransferEventArgs();

            arg.FileSystemEntry = e;
            arg.CurrentBytes = pe.ReadBytesTotal;
            arg.CustomnContext = progressContext;

            if (pe.TotalLength == -1)
                arg.TotalBytes = e.Length;
            else
                arg.TotalBytes = pe.TotalLength;

            arg.PercentageProgress = pe.PercentageProgress;
            arg.TransferRateTotal = pe.TransferRateTotal;
            arg.TransferRateCurrent = pe.TransferRateCurrent;
            
            // calc transfertime            
            if (pe.TransferRateTotal != -1 && pe.TransferRateTotal > 0 )
            {
                long bytesPerSecond = (arg.TransferRateTotal / 8) * 1000;

                if (bytesPerSecond > 0)
                {
                    long neededSeconds = (arg.TotalBytes - arg.CurrentBytes) / bytesPerSecond;
                    arg.OpenTransferTime = new TimeSpan(neededSeconds * TimeSpan.TicksPerSecond);
                }
                else
                    arg.OpenTransferTime = new TimeSpan(long.MaxValue);                       
            }
            else
                arg.OpenTransferTime = new TimeSpan(long.MaxValue);                       

            // call it
            pc(sender, arg);
            
            // create the ret value
            if (arg.Cancel)
                return StreamHelperResultCodes.Aborted;
            else
                return StreamHelperResultCodes.OK;
        }