protected override async Task <ViewFileObjectInitUnderlayFileResultParam> CreateUnderlayFileImplAsync(FileParameters option, CancellationToken cancel = default)
        {
            checked
            {
                ViewFileObjectInitUnderlayFileResultParam underlayFileObjectResult = await base.CreateUnderlayFileImplAsync(option, cancel);

                try
                {
                    HasBom = false;

                    long fileSize = underlayFileObjectResult.InitialSize;
                    if (fileSize == 0)
                    {
                        if (option.Access.Bit(FileAccess.Write))
                        {
                            await underlayFileObjectResult.FileObject.WriteRandomAsync(0, Str.BOM_UTF_8, cancel);

                            HasBom   = true;
                            fileSize = 3;
                        }
                    }
                    else if (fileSize >= 3)
                    {
                        Memory <byte> tmp = new byte[3];
                        if (await underlayFileObjectResult.FileObject.ReadRandomAsync(0, tmp, cancel) == tmp.Length)
                        {
                            if (tmp.Span.SequenceEqual(Str.BOM_UTF_8.Span))
                            {
                                HasBom = true;
                            }
                        }
                    }

                    HeaderOffset = HasBom ? 3 : 0;

                    fileSize -= HeaderOffset;

                    long currentPosition = 0;
                    if (FileParams.Mode == FileMode.Append)
                    {
                        currentPosition = fileSize;
                    }

                    return(new ViewFileObjectInitUnderlayFileResultParam(underlayFileObjectResult.FileObject, currentPosition, fileSize));
                }
                catch
                {
                    await underlayFileObjectResult.FileObject.CloseAsync();

                    underlayFileObjectResult.FileObject._DisposeSafe();
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        protected virtual async Task <ViewFileObjectInitUnderlayFileResultParam> CreateUnderlayFileImplAsync(FileParameters option, CancellationToken cancel = default)
        {
            FileObject obj = await UnderlayFileSystem.CreateFileAsync(option, cancel);

            try
            {
                ViewFileObjectInitUnderlayFileResultParam result = new ViewFileObjectInitUnderlayFileResultParam(
                    obj,
                    obj.FileParams.Flags.Bit(FileFlags.RandomAccessOnly) ? 0 : obj.Position,
                    obj.Size);

                return(result);
            }
            catch
            {
                obj._DisposeSafe();
                throw;
            }
        }
Exemplo n.º 3
0
        internal async Task _InternalCreateFileAsync(CancellationToken cancel = default)
        {
            if (UnderlayFile != null)
            {
                throw new ApplicationException("Already inited.");
            }

            ViewFileObjectInitUnderlayFileResultParam createResult = await CreateUnderlayFileImplAsync(this.FileParams, cancel);

            try
            {
                InitAndCheckFileSizeAndPosition(createResult.InitialPositon, createResult.InitialSize, cancel);
            }
            catch
            {
                createResult.FileObject._DisposeSafe();
                throw;
            }

            this.UnderlayFile = createResult.FileObject;
        }