Exemplo n.º 1
0
 private void InitBlockInfo()
 {
     if (BlockSum == 1)
     {
         FileBlock block = new FileBlock(0L, FileLength);
         block.IsLastBlock = true;
         BlockList.Add(block);
         return;
     }
     else
     {
         long blockSize     = FileLength / (long)BlockSum;
         long lastBlockSize = blockSize + (FileLength % (long)BlockSum);//余下部分由最后一个线程下载
         for (int i = 0; i < BlockSum; ++i)
         {
             FileBlock block;
             if (i == 0)
             {
                 block = new FileBlock(0L, blockSize);
             }
             else if (i == BlockSum - 1)
             {
                 block             = new FileBlock((long)i * blockSize, lastBlockSize);
                 block.IsLastBlock = true;
             }
             else
             {
                 block = new FileBlock((long)i * blockSize, blockSize);
             }
             BlockList.Add(block);
         }
     }
 }
Exemplo n.º 2
0
        public bool Load(string strPath)
        {
            Stream       readStream   = null;
            BinaryReader binaryReader = null;

            try
            {
                readStream   = new FileStream(strPath, FileMode.Open, FileAccess.Read);
                binaryReader = new BinaryReader(readStream);

                Link              = binaryReader.ReadString();
                FileName          = binaryReader.ReadString();
                FileLength        = binaryReader.ReadInt64();
                SumDownloadedSize = binaryReader.ReadInt64();
                BlockSum          = binaryReader.ReadInt32();
                SupportResume     = binaryReader.ReadBoolean();
                Scheme            = binaryReader.ReadString();
                UserName          = binaryReader.ReadString();
                Password          = binaryReader.ReadString();

                for (int i = 0; i < BlockSum; ++i)
                {
                    FileBlock block = new FileBlock();
                    block.StartIndex     = binaryReader.ReadInt64();
                    block.BlockSize      = binaryReader.ReadInt64();
                    block.DownloadedSize = binaryReader.ReadInt64();
                    block.IsLastBlock    = binaryReader.ReadBoolean();
                    block.Completed      = binaryReader.ReadBoolean();
                    BlockList.Add(block);
                }
            }
            catch (EndOfStreamException)
            {
#if DEBUG
                Debug.WriteLine("The end of the stream is reached.");
#endif
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (readStream != null && binaryReader != null)
                {
                    binaryReader.Close();
                }
                else if (readStream != null)
                {
                    readStream.Close();
                }
            }
#if DEBUG
            Debug.WriteLine("读取配置文件完毕!");
#endif
            return(true);
        }