public static ArraySegment <byte>[] GetIoBuffers(this IByteBuffer buf)
 {
     if (buf is IByteBuffer2 buffer2)
     {
         return(buffer2.GetIoBuffers());
     }
     return(buf.GetIoBuffers(buf.ReaderIndex, buf.ReadableBytes));
 }
Exemple #2
0
        public override void CopyTo(Stream destination, int bufferSize)
        {
            EnsureNotClosed();

            var remaining = _buffer.ReadableBytes;

            if ((uint)(remaining - 1) > SharedConstants.TooBigOrNegative) // remaining <= 0
            {
                return;
            }

            ValidateCopyToArgs(destination);

            var ioBuffers = _buffer.GetIoBuffers();

            foreach (var ioBuffer in ioBuffers)
            {
                destination.Write(ioBuffer.Array, ioBuffer.Offset, ioBuffer.Count);
            }
        }
Exemple #3
0
        public void TestExtractNioBuffers()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);
            IByteBuffer buf2 = Unpooled.Buffer(10);
            IByteBuffer buf3 = Unpooled.DirectBuffer(10);

            buf1.WriteBytes(Encoding.ASCII.GetBytes("a"));
            buf2.WriteBytes(Encoding.ASCII.GetBytes("b"));
            buf3.WriteBytes(Encoding.ASCII.GetBytes("c"));
            IByteBuffer composite   = Unpooled.WrappedUnmodifiableBuffer(buf1, buf2, buf3);
            var         byteBuffers = composite.GetIoBuffers(0, 3);

            Assert.Equal(3, byteBuffers.Length);
            Assert.Single(byteBuffers[0]);
            Assert.Single(byteBuffers[1]);
            Assert.Single(byteBuffers[2]);
            composite.Release();
        }
        public override ArraySegment <byte>[] GetIoBuffers(int index, int length)
        {
            this.CheckIndex(index, length);
            if (length == 0)
            {
                return(new[] { EmptyNioBuffer });
            }

            var buffers = new List <ArraySegment <byte> >(this.components.Count);
            int i       = this.ToComponentIndex(index);

            while (length > 0)
            {
                ComponentEntry c           = this.components[i];
                IByteBuffer    s           = c.Buffer;
                int            adjustment  = c.Offset;
                int            localLength = Math.Min(length, s.Capacity - (index - adjustment));
                switch (s.IoBufferCount)
                {
                case 0:
                    throw new NotSupportedException();

                case 1:
                    buffers.Add(s.GetIoBuffer(index - adjustment, localLength));
                    break;

                default:
                    buffers.AddRange(s.GetIoBuffers(index - adjustment, localLength));
                    break;
                }

                index  += localLength;
                length -= localLength;
                i++;
            }

            return(buffers.ToArray());
        }
Exemple #5
0
 public virtual ArraySegment <byte>[] GetIoBuffers(int index, int length) => Buf.GetIoBuffers(index, length);
Exemple #6
0
 public ArraySegment <byte>[] GetIoBuffers(int index, int length)
 {
     CheckIndex(index, length);
     return(_buffer.GetIoBuffers(index, length));
 }
        bool Add(IByteBuffer buf)
        {
            if (this.count == MaximumLimit)
            {
                return(false);
            }

            int len = buf.ReadableBytes;

            if (len == 0)
            {
                return(true);
            }

            if (this.maxBytes - len < this.size && this.count > 0)
            {
                return(false);
            }

            IntPtr addr = IntPtr.Zero;

            if (buf.HasMemoryAddress)
            {
                addr = buf.AddressOfPinnedMemory();
            }

            if (addr != IntPtr.Zero)
            {
                this.Add(addr, buf.ReaderIndex, len);
            }
            else
            {
                int bufferCount = buf.IoBufferCount;
                if (MaximumLimit - bufferCount < this.count)
                {
                    return(false);
                }

                if (bufferCount == 1)
                {
                    ArraySegment <byte> arraySegment = buf.GetIoBuffer();

                    byte[]   array  = arraySegment.Array;
                    GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
                    this.handles.Add(handle);

                    addr = handle.AddrOfPinnedObject();
                    this.Add(addr, arraySegment.Offset, arraySegment.Count);
                }
                else
                {
                    ArraySegment <byte>[] segments = buf.GetIoBuffers();
                    for (int i = 0; i < segments.Length; i++)
                    {
                        GCHandle handle = GCHandle.Alloc(segments[i].Array, GCHandleType.Pinned);
                        this.handles.Add(handle);

                        addr = handle.AddrOfPinnedObject();
                        this.Add(addr, segments[i].Offset, segments[i].Count);
                    }
                }
            }
            return(true);
        }
Exemple #8
0
        internal void Prepare()
        {
            IByteBuffer byteBuffer = this.buffer;
            int         index      = byteBuffer.ReaderIndex;
            int         length     = byteBuffer.ReadableBytes;
            IntPtr      addr       = byteBuffer.AddressOfPinnedMemory();

            if (addr != IntPtr.Zero)
            {
                this.bufs[0] = new uv_buf_t(addr + index, length);
                this.size    = 1;
                return;
            }

            if (byteBuffer.HasArray)
            {
                byte[]   array  = byteBuffer.Array;
                GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
                this.handles.Add(handle);

                addr         = handle.AddrOfPinnedObject();
                this.bufs[0] = new uv_buf_t(addr + this.buffer.ArrayOffset + index, length);
                this.size    = 1;
                return;
            }

            if (byteBuffer.IoBufferCount == 1)
            {
                ArraySegment <byte> arraySegment = byteBuffer.GetIoBuffer(index, length);

                byte[]   array  = arraySegment.Array;
                GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
                this.handles.Add(handle);

                addr         = handle.AddrOfPinnedObject();
                this.bufs[0] = new uv_buf_t(addr + arraySegment.Offset, arraySegment.Count);
                this.size    = 1;
                return;
            }

            ArraySegment <byte>[] segments = byteBuffer.GetIoBuffers(index, length);
            if (segments.Length > this.bufs.Length)
            {
                if (this.pin.IsAllocated)
                {
                    this.pin.Free();
                }
                this.bufs = new uv_buf_t[segments.Length];
                this.pin  = GCHandle.Alloc(this.bufs, GCHandleType.Pinned);
            }

            for (int i = 0; i < segments.Length; i++)
            {
                GCHandle handle = GCHandle.Alloc(segments[i].Array, GCHandleType.Pinned);
                this.handles.Add(handle);

                addr         = handle.AddrOfPinnedObject();
                this.bufs[i] = new uv_buf_t(addr + segments[i].Offset, segments[i].Count);
            }

            this.size = segments.Length;
        }