A common denominator for byte array and ByteChunk.
Ejemplo n.º 1
0
        private void WriteImpl(ref ByteSlice slice)
        {
            var offset       = 0;
            var bytesLeft    = _capacity - _position;
            var bytesToAlloc = slice.Count - bytesLeft;

            if (bytesToAlloc > 0)
            {
                var numberOfSegments = Calculator.GetSegmentIndex(bytesToAlloc) + 1;
                AddSegments(numberOfSegments);
            }

            // find initial segment to write
            var index = Calculator.GetSegmentIndex(_position);

            var currentSegment = FindSegment(index);

            // intial segment selected, do writing
            var toWrite = slice.Count;

            do
            {
                var currentSegmentIndex = Calculator.GetIndexInSegment(_position);
                var segmentBuffer       = currentSegment->Buffer;
                var spaceToWrite        = currentSegment->Length - currentSegmentIndex;

                spaceToWrite = spaceToWrite > toWrite ? toWrite : spaceToWrite;
                if (spaceToWrite > 0)
                {
                    slice.CopyTo(segmentBuffer, currentSegmentIndex, spaceToWrite, offset);
                }

                toWrite       -= spaceToWrite;
                _position     += spaceToWrite;
                offset        += spaceToWrite;
                currentSegment = currentSegment->Next;
            } while (toWrite > 0);

            if (_position > _length)
            {
                _length = _position;
            }
        }
Ejemplo n.º 2
0
 public void Write(ByteChunk chunk)
 {
     var slice = new ByteSlice(chunk);
     WriteImpl(ref slice);
 }
Ejemplo n.º 3
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     var slice = new ByteSlice(buffer, offset, count);
     WriteImpl(ref slice);
 }
Ejemplo n.º 4
0
        private void WriteImpl(ref ByteSlice slice)
        {
            var offset = 0;
            var bytesLeft = _capacity - _position;
            var bytesToAlloc = slice.Count - bytesLeft;
            if (bytesToAlloc > 0)
            {
                var numberOfSegments = Calculator.GetSegmentIndex(bytesToAlloc) + 1;
                AddSegments(numberOfSegments);
            }

            // find initial segment to write
            var index = Calculator.GetSegmentIndex(_position);

            var currentSegment = FindSegment(index);

            // intial segment selected, do writing
            var toWrite = slice.Count;
            do
            {
                var currentSegmentIndex = Calculator.GetIndexInSegment(_position);
                var segmentBuffer = currentSegment->Buffer;
                var spaceToWrite = currentSegment->Length - currentSegmentIndex;

                spaceToWrite = spaceToWrite > toWrite ? toWrite : spaceToWrite;
                if (spaceToWrite > 0)
                {
                    slice.CopyTo(segmentBuffer, currentSegmentIndex, spaceToWrite, offset);
                }

                toWrite -= spaceToWrite;
                _position += spaceToWrite;
                offset += spaceToWrite;
                currentSegment = currentSegment->Next;
            } while (toWrite > 0);

            if (_position > _length)
            {
                _length = _position;
            }
        }
Ejemplo n.º 5
0
 public int Read(ByteChunk chunk)
 {
     var slice = new ByteSlice(chunk);
     return ReadImpl(ref slice);
 }
Ejemplo n.º 6
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     var slice = new ByteSlice(buffer, offset, count);
     return ReadImpl(ref slice);
 }
Ejemplo n.º 7
0
        private int ReadImpl(ref ByteSlice slice)
        {
            if (_position >= _length)
                return 0;

            var alreadyCopied = 0;
            var toCopy = Math.Min(slice.Count, _length - _position);
            while (toCopy > 0)
            {
                var index = Calculator.GetSegmentIndex(_position);
                var indexInSegment = Calculator.GetIndexInSegment(_position);

                var segment = FindSegment(index);

                var bytesToRead = Math.Min(segment->Length - indexInSegment, toCopy);
                if (bytesToRead > 0)
                {
                    var segmentBuffer = segment->Buffer;
                    slice.CopyFrom(alreadyCopied, segmentBuffer, indexInSegment, bytesToRead);
                    alreadyCopied += bytesToRead;
                    toCopy -= bytesToRead;
                    _position += bytesToRead;
                }
            }

            return alreadyCopied;
        }
Ejemplo n.º 8
0
        public void Write(ByteChunk chunk)
        {
            var slice = new ByteSlice(chunk);

            WriteImpl(ref slice);
        }
Ejemplo n.º 9
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            var slice = new ByteSlice(buffer, offset, count);

            WriteImpl(ref slice);
        }
Ejemplo n.º 10
0
        public int Read(ByteChunk chunk)
        {
            var slice = new ByteSlice(chunk);

            return(ReadImpl(ref slice));
        }
Ejemplo n.º 11
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            var slice = new ByteSlice(buffer, offset, count);

            return(ReadImpl(ref slice));
        }