Ejemplo n.º 1
0
        void Save(SaveHexOptionsVM hex, ref byte[] buffer)
        {
            var progress = new HexFileProgress(GetSize(hex.Document.StartOffset, hex.Document.EndOffset));

            fileProgress = progress;
            if (buffer == null)
            {
                buffer = new byte[64 * 1024];
            }

            try {
                if (File.Exists(hex.FileName))
                {
                    File.Delete(hex.FileName);
                }
                using (var stream = File.OpenWrite(hex.FileName)) {
                    ulong offs = hex.Document.StartOffset;
                    ulong end  = hex.Document.EndOffset;
                    while (offs <= end)
                    {
                        ThrowIfCanceled();

                        ulong bytesLeft    = GetSize(offs, end);
                        int   bytesToWrite = bytesLeft > (ulong)buffer.Length ? buffer.Length : (int)bytesLeft;
                        hex.Document.Read(offs, buffer, 0, bytesToWrite);
                        stream.Write(buffer, 0, bytesToWrite);
                        progress.BytesWritten += (ulong)bytesToWrite;
                        NotifyProgressUpdated();

                        ulong nextOffs = offs + (ulong)bytesToWrite;
                        if (nextOffs < offs)
                        {
                            break;
                        }
                        offs = nextOffs;
                    }
                }
            }
            catch {
                DeleteFile(hex.FileName);
                throw;
            }
        }
Ejemplo n.º 2
0
        void Save(SaveHexOptionsVM hex, ref byte[] buffer)
        {
            var progress = new HexFileProgress(GetSize(hex.Buffer.Span.Start, hex.Buffer.Span.End));

            fileProgress = progress;
            if (buffer == null)
            {
                buffer = new byte[64 * 1024];
            }

            try {
                if (File.Exists(hex.FileName))
                {
                    File.Delete(hex.FileName);
                }
                using (var stream = File.OpenWrite(hex.FileName)) {
                    var pos = hex.Buffer.Span.Start;
                    var end = hex.Buffer.Span.End;
                    while (pos < end)
                    {
                        ThrowIfCanceled();

                        ulong bytesLeft    = GetSize(pos, end);
                        int   bytesToWrite = bytesLeft > (ulong)buffer.Length ? buffer.Length : (int)bytesLeft;
                        hex.Buffer.ReadBytes(pos, buffer, 0, bytesToWrite);
                        stream.Write(buffer, 0, bytesToWrite);
                        progress.BytesWritten += (ulong)bytesToWrite;
                        NotifyProgressUpdated();

                        pos = pos + (ulong)bytesToWrite;
                    }
                }
            }
            catch {
                DeleteFile(hex.FileName);
                throw;
            }
        }