Exemplo n.º 1
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 };
        }
Exemplo n.º 2
0
 public static StreamHelperResult CopyStreamData(Object sender, Stream src, Stream trg, StreamHelperProgressCallback status, params Object[] data)
 {
     return(CopyStreamData(sender, src, trg, -1, status, data));
 }
Exemplo n.º 3
0
 public static StreamHelperResult CopyStreamData(Object sender, Stream src, Stream trg, StreamHelperProgressCallback status, params Object[] data)
 {
     return CopyStreamData(sender, src, trg, -1, status, data);
 }
Exemplo n.º 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
            });
        }