示例#1
0
        protected override Result ReadImpl(long offset, Span <byte> destination)
        {
            long inPos     = offset;
            int  outPos    = 0;
            int  remaining = destination.Length;

            if (!IsRangeValid(offset, destination.Length, Length))
            {
                return(ResultFs.ValueOutOfRange.Log());
            }

            int sourceIndex = FindSource(inPos);

            while (remaining > 0)
            {
                ConcatSource entry       = Sources[sourceIndex];
                long         entryPos    = inPos - entry.StartOffset;
                long         entryRemain = entry.StartOffset + entry.Size - inPos;

                int bytesToRead = (int)Math.Min(entryRemain, remaining);

                Result rc = entry.Storage.Read(entryPos, destination.Slice(outPos, bytesToRead));
                if (rc.IsFailure())
                {
                    return(rc);
                }

                outPos    += bytesToRead;
                inPos     += bytesToRead;
                remaining -= bytesToRead;
                sourceIndex++;
            }

            return(Result.Success);
        }
示例#2
0
        protected override Result WriteImpl(long offset, ReadOnlySpan <byte> source)
        {
            long inPos     = offset;
            int  outPos    = 0;
            int  remaining = source.Length;

            if (!IsRangeValid(offset, source.Length, Length))
            {
                return(ResultFs.ValueOutOfRange.Log());
            }

            int sourceIndex = FindSource(inPos);

            while (remaining > 0)
            {
                ConcatSource entry       = Sources[sourceIndex];
                long         entryPos    = inPos - entry.StartOffset;
                long         entryRemain = entry.StartOffset + entry.Size - inPos;

                int bytesToWrite = (int)Math.Min(entryRemain, remaining);

                Result rc = entry.Storage.Write(entryPos, source.Slice(outPos, bytesToWrite));
                if (rc.IsFailure())
                {
                    return(rc);
                }

                outPos    += bytesToWrite;
                inPos     += bytesToWrite;
                remaining -= bytesToWrite;
                sourceIndex++;
            }

            return(Result.Success);
        }
示例#3
0
        protected override void WriteImpl(ReadOnlySpan <byte> source, long offset)
        {
            long inPos     = offset;
            int  outPos    = 0;
            int  remaining = source.Length;

            while (remaining > 0)
            {
                ConcatSource storage   = FindSource(inPos);
                long         sourcePos = inPos - storage.StartOffset;

                int bytesToWrite = (int)Math.Min(storage.EndOffset - inPos, remaining);
                storage.Storage.Write(source.Slice(outPos, bytesToWrite), sourcePos);

                outPos    += bytesToWrite;
                inPos     += bytesToWrite;
                remaining -= bytesToWrite;
            }
        }
示例#4
0
        protected override void ReadImpl(Span <byte> destination, long offset)
        {
            long inPos     = offset;
            int  outPos    = 0;
            int  remaining = destination.Length;

            while (remaining > 0)
            {
                ConcatSource entry     = FindSource(inPos);
                long         sourcePos = inPos - entry.StartOffset;

                int bytesToRead = (int)Math.Min(entry.EndOffset - inPos, remaining);
                entry.Storage.Read(destination.Slice(outPos, bytesToRead), sourcePos);

                outPos    += bytesToRead;
                inPos     += bytesToRead;
                remaining -= bytesToRead;
            }
        }
示例#5
0
        public override Storage Slice(long start, long length, bool leaveOpen)
        {
            ConcatSource startSource = FindSource(start);
            ConcatSource endSource   = FindSource(start + length - 1);

            if (startSource != endSource)
            {
                return(base.Slice(start, length, leaveOpen));
            }

            Storage storage = startSource.Storage.Slice(start - startSource.StartOffset, length, true);

            if (!leaveOpen)
            {
                storage.ToDispose.Add(this);
            }

            return(storage);
        }
示例#6
0
        public ConcatenationStorage(IList <IStorage> sources, bool leaveOpen)
        {
            Sources   = new ConcatSource[sources.Count];
            LeaveOpen = leaveOpen;

            long length = 0;

            for (int i = 0; i < sources.Count; i++)
            {
                sources[i].GetSize(out long sourceSize).ThrowIfFailure();

                if (sourceSize < 0)
                {
                    throw new ArgumentException("Sources must have an explicit length.");
                }
                Sources[i] = new ConcatSource(sources[i], length, sourceSize);
                length    += sourceSize;
            }

            Length = length;
        }
示例#7
0
        public ConcatenationStorage(IList <IStorage> sources, bool leaveOpen)
        {
            Sources = new ConcatSource[sources.Count];
            if (!leaveOpen)
            {
                ToDispose.AddRange(sources);
            }

            long length = 0;

            for (int i = 0; i < sources.Count; i++)
            {
                if (sources[i].Length < 0)
                {
                    throw new ArgumentException("Sources must have an explicit length.");
                }
                Sources[i] = new ConcatSource(sources[i], length, sources[i].Length);
                length    += sources[i].Length;
            }

            Length = length;
        }
示例#8
0
        protected override void WriteImpl(ReadOnlySpan <byte> source, long offset)
        {
            long inPos       = offset;
            int  outPos      = 0;
            int  remaining   = source.Length;
            int  sourceIndex = FindSource(inPos);

            while (remaining > 0)
            {
                ConcatSource entry       = Sources[sourceIndex];
                long         entryPos    = inPos - entry.StartOffset;
                long         entryRemain = entry.StartOffset + entry.Size - inPos;

                int bytesToWrite = (int)Math.Min(entryRemain, remaining);
                entry.Storage.Write(source.Slice(outPos, bytesToWrite), entryPos);

                outPos    += bytesToWrite;
                inPos     += bytesToWrite;
                remaining -= bytesToWrite;
                sourceIndex++;
            }
        }
示例#9
0
        protected override void ReadImpl(Span <byte> destination, long offset)
        {
            long inPos       = offset;
            int  outPos      = 0;
            int  remaining   = destination.Length;
            int  sourceIndex = FindSource(inPos);

            while (remaining > 0)
            {
                ConcatSource entry       = Sources[sourceIndex];
                long         entryPos    = inPos - entry.StartOffset;
                long         entryRemain = entry.StartOffset + entry.Size - inPos;

                int bytesToRead = (int)Math.Min(entryRemain, remaining);
                entry.Storage.Read(destination.Slice(outPos, bytesToRead), entryPos);

                outPos    += bytesToRead;
                inPos     += bytesToRead;
                remaining -= bytesToRead;
                sourceIndex++;
            }
        }