Exemplo n.º 1
0
        //通过文件创建流对象;
        public static Stream CreateStreamByFile(IFile file)
        {
            if (file != null)
            {
                try {
                    if (file.Type == FileType.BlockDeviceFile)                          //若为块文件,则文件为设备或分区;
                    {
                        if (file is Partition part)                                     //若为分区,则根据分区的起始,终止地址构造流;
                        {
                            var device = file.GetParent <Device>();
                            if (device != null)
                            {
                                return(InterceptStream.CreateFromStream(device.Stream, part.StartLBA, part.Size));
                            }
                        }
                        else if (file is Device device)                                            //若为设备,直接返回设备流;
                        {
                            return(device.Stream);
                        }
                    }
                    //否则直接根据StartLBA,EndLBA构建截取流;
                    else if (file is RegularFile regFile)
                    {
                        return(regFile.GetStream());
                    }
                    else if (file is IBlockGroupedFile)
                    {
                        var blockGroupedFile = file as IBlockGroupedFile;
                        var part             = file.GetParent <Partition>();
                        var device           = file.GetParent <Device>();
                        if (part != null && device != null)
                        {
                            var blockSize  = part.ClusterSize;
                            var partStream = InterceptStream.CreateFromStream(device.Stream, part.StartLBA, part.Size);
                            //若块组不为空,则遍历块组组成虚拟流;
                            if (blockGroupedFile.BlockGroups != null)
                            {
                                var ranges = blockGroupedFile.BlockGroups.Select(p =>
                                                                                 ValueTuple.Create(p.BlockAddress * blockSize + p.Offset, p.Count * blockSize)).ToArray();

                                var blockSub = ranges.Sum(p => p.Item2) - file.Size;
                                if (ranges?.Count() > 0 && 0 < blockSub && blockSub < blockSize)
                                {
                                    ranges[ranges.Count() - 1].Item2 -= blockSub;
                                }
                                var multiStream = MulPeriodsStream.CreateFromStream(partStream, ranges);
                                return(multiStream);
                            }
                        }
                    }
                }
                catch (Exception ex) {
                    Logger.WriteLine($"{nameof(StreamExtensions)}->{nameof(CreateStreamByFile)}:{ex.Message}");
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        }                                                           //文件系统类型;

        public virtual Stream GetStream(bool isReadOnly = true)
        {
            var device = this.GetParent <Device>();

            if (device != null)
            {
                return(InterceptStream.CreateFromStream(device.Stream, StartLBA, Size));
            }
            else
            {
                EventLogger.Logger.WriteCallerLine($"{nameof(device)} can't be null!");
                return(null);
            }
        }
Exemplo n.º 3
0
        }                                                           //创建时间;

        public virtual Stream GetStream(bool isReadOnly = true)
        {
            var part = this.GetParent <Partition>();

            if (part == null)
            {
                Logger.WriteCallerLine($"{nameof(part)} can't be null!");
            }

            //若块组不为空,则取所有的块字段流;
            if (BlockGroups != null)
            {
                if (part != null)
                {
                    var blockSize  = part.ClusterSize;
                    var partStream = part.GetStream();
                    //若块组不为空,则遍历块组组成虚拟流;

                    var ranges = BlockGroups.Select(p =>
                                                    ValueTuple.Create(
                                                        p.BlockAddress * blockSize,
                                                        p.Count * blockSize)).ToArray();

                    var blockSub = ranges.Sum(p => p.Item2) - Size;
                    if (ranges?.Count() > 0 && 0 < blockSub && blockSub < blockSize)
                    {
                        ranges[ranges.Count() - 1].Item2 -= blockSub;
                    }
                    var multiStream = MulPeriodsStream.CreateFromStream(partStream, ranges);
                    return(multiStream);
                }
            }
            //否则直接取连续的流;
            else
            {
                var blockSize = part.ClusterSize;

                var partStream = part.GetStream();
                if (partStream != null)
                {
                    var fiStream = InterceptStream.CreateFromStream(partStream, StartLBA, Size);
                    //var buffer = new byte[128];
                    //var read = fiStream.Read(buffer, 0, buffer.Length);
                    return(fiStream);
                }
            }
            return(null);
        }
        internal static IDelegateCommand CreateCopyToClipBoardCommand(IHexDataContext hexDataContext)
        {
            var copyToClipBoardCommand = CommandFactory.CreateDelegateCommand(
                () => {
                if (hexDataContext.SelectionStart == -1 || hexDataContext.SelectionLength == -1)
                {
                    return;
                }

                //若需剪切数据大于4GB,提示警告;
                if (hexDataContext.SelectionLength - hexDataContext.SelectionStart > MaxCopyToClipBoardSize)
                {
                    MsgBoxService.Current?.Show(LanguageService.Current?.FindResourceString(Constants.MsgText_TooLargeCopySize));
                    return;
                }

                hexDataContext.Stream.Position = hexDataContext.SelectionStart;

                InterceptStream sourceStream = null;
                StreamReader sr = null;

                try {
                    sourceStream = InterceptStream.CreateFromStream(
                        hexDataContext.Stream,
                        hexDataContext.SelectionStart,
                        hexDataContext.SelectionLength);

                    sr = new StreamReader(sourceStream);
                    ClipBoardService.SetDataObject(sr.ReadToEnd());
                }
                catch (Exception ex) {
                    LoggerService.WriteCallerLine(ex.Message);
                    MsgBoxService.Current?.Show($"{ex.Message}");
                }
                finally {
                    sourceStream?.Dispose();
                    sr?.Dispose();
                }
            });

            return(copyToClipBoardCommand);
        }
        internal static IDelegateCommand CreateCopyToNewFileCommand(IHexDataContext hexDataContext)
        {
            var copyToNewFileCommand = CommandFactory.CreateDelegateCommand(
                () => {
                if (hexDataContext.SelectionLength == -1 || hexDataContext.SelectionStart == -1)
                {
                    return;
                }

                var dialogService = ServiceProvider.Current.GetInstance <IDialogService>();
                if (dialogService == null)
                {
                    LoggerService.WriteCallerLine($"{nameof(dialogService)} can't be null.");
                    return;
                }

                var path = dialogService.GetSaveFilePath();
                if (path == null)
                {
                    return;
                }

                var dialog         = dialogService.CreateLoadingDialog();
                dialog.WindowTitle = LanguageService.FindResourceString(Constants.WindowTitle_DataCopying);
                dialog.DoWork     += (sender, e) => {
                    FileStream fs = null;
                    InterceptStream sourceStream = null;

                    try {
                        fs            = File.Create(path);
                        byte[] buffer = new byte[10485760];
                        int read;
                        long readSize = 0;
                        hexDataContext.Stream.Position = hexDataContext.SelectionStart;

                        sourceStream = InterceptStream.CreateFromStream(
                            hexDataContext.Stream,
                            hexDataContext.SelectionStart,
                            hexDataContext.SelectionLength);

                        while ((read = sourceStream.Read(buffer, 0, buffer.Length)) != 0 && !dialog.CancellationPending)
                        {
                            fs.Write(buffer, 0, read);
                            readSize += read;
                            dialog.ReportProgress((int)(readSize * 100 / sourceStream.Length));
                        }
                    }
                    catch (Exception ex) {
                        LoggerService.WriteCallerLine(ex.Message);
                    }
                    finally {
                        fs?.Dispose();
                        sourceStream?.Dispose();
                    }
                };

                dialog.RunWorkerCompleted += (sender, e) => {
                    if (e.Cancelled)
                    {
                        return;
                    }

                    LocalExplorerService.OpenFolderAndSelectFile(path);
                    MsgBoxService.Current?.Show(LanguageService.Current?.FindResourceString(Constants.WindowTitle_DataCopyDone));
                };

                dialog.ShowDialog();
            }, () => SelectionAvailable(hexDataContext));

            hexDataContext.SelectionStateChanged += delegate {
                copyToNewFileCommand.RaiseCanExecuteChanged();
            };
            return(copyToNewFileCommand);
        }