/// <summary> /// 构造函数 /// 用于从文件读入区块 /// </summary> /// <param name="transmissionTask"></param> /// <param name="blockIndex">分块位置</param> /// <param name="readOnCreated">是否立即从文件读取数据</param> public FileBlock(FileTransmission transmissionTask, int blockIndex, bool readOnCreated) { _fileStream = transmissionTask.FileStream; Index = blockIndex; if (readOnCreated) { Read(true); } }
/// <summary> /// 构造函数 /// 用于从二进制数据读入区块 /// </summary> /// <param name="transmissionTask"></param> /// <param name="receivedData">输入的二进制数据</param> public FileBlock(FileTransmission transmissionTask, byte[] receivedData) { _fileStream = transmissionTask.FileStream; if (receivedData[0] != Consts.FileBlockHeader) { throw new FileBlockException("Bad Header!", FileBlockException.ErrorCode.BadHeader); } Index = BitConverter.ToInt32(receivedData, 1); _dataLength = receivedData.Length - 9; if (_dataLength > Consts.BlockSize) { throw new FileBlockException("Illegal FileBlock Size!", FileBlockException.ErrorCode.IllegalFileBlockSize); } _data = new byte[_dataLength]; _dataHash = new byte[4]; Array.Copy(receivedData, 5, _dataHash, 0, 4); Array.Copy(receivedData, 9, _data, 0, _dataLength); if (!_dataHash.BytesEqual(_data.GetHash())) { throw new FileBlockException("Error Hash!", FileBlockException.ErrorCode.ChecksumError); } }
public FileBlockCollection(FileTransmission transmissionTask) { Task = transmissionTask; FileStream = Task.FileStream; IoBufferSize = Consts.DefaultIoBufferSize; }
public FileBlockCollection(FileTransmission transmissionTask) { Task = transmissionTask; FileStream = Task.FileStream; IoBufferSize = Consts.DefaultIoBufferSize; }