Exemplo n.º 1
0
        public sealed override IByteBuffer AdjustCapacity(int newCapacity)
        {
            CheckNewCapacity(newCapacity);

            uint unewCapacity = (uint)newCapacity;
            uint oldCapacity  = (uint)_capacity;

            if (oldCapacity == unewCapacity)
            {
                return(this);
            }
            int bytesToCopy;

            if (unewCapacity > oldCapacity)
            {
                bytesToCopy = _capacity;
            }
            else
            {
                TrimIndicesToCapacity(newCapacity);
                bytesToCopy = newCapacity;
            }
            byte[] oldArray = Memory;
            byte[] newArray = AllocateArray(newCapacity);
            PlatformDependent.CopyMemory(oldArray, 0, newArray, 0, bytesToCopy);

            SetArray(newArray, MaxCapacity);
            FreeArray(oldArray);
            return(this);
        }
Exemplo n.º 2
0
        //internal static int SetBytes(AbstractByteBuffer buf, byte* addr, int index, Stream input, int length)
        //{
        //    IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);
        //    try
        //    {
        //        int readTotal = 0;
        //        int readBytes;
        //        byte[] tmp = tmpBuf.Array;
        //        int offset = tmpBuf.ArrayOffset;
        //        do
        //        {
        //            readBytes = input.Read(tmp, offset + readTotal, length - readTotal);
        //            readTotal += readBytes;
        //        }
        //        while (readBytes > 0 && readTotal < length);

        //        //if (readTotal > 0)
        //        //{
        //        PlatformDependent.CopyMemory(tmp, offset, addr, readTotal);
        //        //}

        //        return readTotal;
        //    }
        //    finally
        //    {
        //        tmpBuf.Release();
        //    }
        //}

        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
        {
            //if (dst is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst); }

            //if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity))
            //{
            //    ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex);
            //}
            if (0u >= (uint)length)
            {
                return;
            }

            if (dst.HasMemoryAddress)
            {
                IntPtr ptr = dst.AddressOfPinnedMemory();
                if (ptr != IntPtr.Zero)
                {
                    PlatformDependent.CopyMemory(addr, (byte *)(ptr + dstIndex), length);
                }
                else
                {
                    fixed(byte *destination = &dst.GetPinnableMemoryAddress())
                    {
                        PlatformDependent.CopyMemory(addr, destination + dstIndex, length);
                    }
                }
                return;
            }

            GetBytes0(buf, addr, index, dst, dstIndex, length);
        }
        public sealed override IByteBuffer AdjustCapacity(int newCapacity)
        {
            CheckNewCapacity(newCapacity);

            uint unewCapacity = (uint)newCapacity;
            uint oldCapacity  = (uint)_capacity;

            if (oldCapacity == unewCapacity)
            {
                return(this);
            }
            int bytesToCopy;

            if (unewCapacity > oldCapacity)
            {
                bytesToCopy = _capacity;
            }
            else
            {
                TrimIndicesToCapacity(newCapacity);
                bytesToCopy = newCapacity;
            }
            byte[] oldBuffer = _buffer;
            byte[] newBuffer = AllocateDirect(newCapacity);
            PlatformDependent.CopyMemory(oldBuffer, 0, newBuffer, 0, bytesToCopy);
            SetByteBuffer(newBuffer, true);
            return(this);
        }
Exemplo n.º 4
0
        internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
        {
            //if (src is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.src); }

            //if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity))
            //{
            //    ThrowHelper.ThrowIndexOutOfRangeException_SrcIndex(srcIndex);
            //}
            if (0u >= (uint)length)
            {
                return;
            }

            if (src.HasMemoryAddress)
            {
                IntPtr ptr = src.AddressOfPinnedMemory();
                if (ptr != IntPtr.Zero)
                {
                    PlatformDependent.CopyMemory((byte *)(ptr + srcIndex), addr, length);
                }
                else
                {
                    fixed(byte *source = &src.GetPinnableMemoryAddress())
                    {
                        PlatformDependent.CopyMemory(source + srcIndex, addr, length);
                    }
                }
                return;
            }

            SetBytes0(buf, addr, index, src, srcIndex, length);
        }
Exemplo n.º 5
0
        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
        {
            if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity))
            {
                throw new IndexOutOfRangeException($"dstIndex: {dstIndex}");
            }

            if (dst.HasMemoryAddress)
            {
                IntPtr ptr = dst.AddressOfPinnedMemory();
                if (ptr != IntPtr.Zero)
                {
                    PlatformDependent.CopyMemory(addr, (byte *)(ptr + dstIndex), length);
                }
                else
                {
                    fixed(byte *destination = &dst.GetPinnableMemoryAddress())
                    {
                        PlatformDependent.CopyMemory(addr, destination + dstIndex, length);
                    }
                }
            }
            else if (dst.HasArray)
            {
                PlatformDependent.CopyMemory(addr, dst.Array, dst.ArrayOffset + dstIndex, length);
            }
            else
            {
                dst.SetBytes(dstIndex, buf, index, length);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Return an array of the underlying storage from <paramref name="buf"/> into a byte array.
        /// The copy will start at {@code start} and copy {@code length} bytes.
        /// If <paramref name="copy"/> is true a copy will be made of the memory.
        /// If <paramref name="copy"/> is false the underlying storage will be shared, if possible.
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="copy"></param>
        /// <returns></returns>
        public static byte[] GetBytes(IByteBuffer buf, int start, int length, bool copy)
        {
            var capacity = buf.Capacity;

            if (MathUtil.IsOutOfBounds(start, length, capacity))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_Expected(start, length, capacity);
            }

            if (buf.HasArray)
            {
                if (copy || start != 0 || length != capacity)
                {
                    int baseOffset = buf.ArrayOffset + start;
                    var bytes      = new byte[length];
                    PlatformDependent.CopyMemory(buf.Array, baseOffset, bytes, 0, length);
                    return(bytes);
                }
                else
                {
                    return(buf.Array);
                }
            }

            byte[] v = new byte[length];
            _ = buf.GetBytes(start, v);
            return(v);
        }
        internal static IByteBuffer Copy(AbstractByteBuffer buf, byte *addr, int index, int length)
        {
            IByteBuffer copy = buf.Allocator.DirectBuffer(length, buf.MaxCapacity);

            if (length != 0)
            {
                if (copy.HasMemoryAddress)
                {
                    IntPtr ptr = copy.AddressOfPinnedMemory();
                    if (ptr != IntPtr.Zero)
                    {
                        PlatformDependent.CopyMemory(addr, (byte *)ptr, length);
                    }
                    else
                    {
                        fixed(byte *dst = &copy.GetPinnableMemoryAddress())
                        {
                            PlatformDependent.CopyMemory(addr, dst, length);
                        }
                    }
                    copy.SetIndex(0, length);
                }
                else
                {
                    copy.WriteBytes(buf, index, length);
                }
            }
            return(copy);
        }
        internal static Task <int> SetBytesAsync(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length, CancellationToken cancellationToken)
        {
            if (length == 0)
            {
                return(TaskEx.Zero);
            }

            IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);

            return(tmpBuf.SetBytesAsync(0, input, length, cancellationToken)
                   .ContinueWith(t => {
                try
                {
                    var read = t.Result;
                    if (read > 0)
                    {
                        PlatformDependent.CopyMemory(tmpBuf.Array, tmpBuf.ArrayOffset, addr, read);
                    }
                    return read;
                }
                finally
                {
                    tmpBuf.Release();
                }
            }));
        }
        internal static int SetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length)
        {
            if (length == 0)
            {
                return(0);
            }

            IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);

            try
            {
                byte[] tmp       = tmpBuf.Array;
                int    offset    = tmpBuf.ArrayOffset;
                int    readBytes = input.Read(tmp, offset, length);
                if (readBytes > 0)
                {
                    PlatformDependent.CopyMemory(tmp, offset, addr, readBytes);
                }

                return(readBytes);
            }
            finally
            {
                tmpBuf.Release();
            }
        }
Exemplo n.º 10
0
        internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
        {
            if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity))
            {
                throw new IndexOutOfRangeException($"srcIndex: {srcIndex}");
            }

            if (length != 0)
            {
                if (src.HasMemoryAddress)
                {
                    IntPtr ptr = src.AddressOfPinnedMemory();
                    if (ptr != IntPtr.Zero)
                    {
                        PlatformDependent.CopyMemory((byte *)(ptr + srcIndex), addr, length);
                    }
                    else
                    {
                        fixed(byte *source = &src.GetPinnableMemoryAddress())
                        {
                            PlatformDependent.CopyMemory(source + srcIndex, addr, length);
                        }
                    }
                }
                else if (src.HasArray)
                {
                    PlatformDependent.CopyMemory(src.Array, src.ArrayOffset + srcIndex, addr, length);
                }
                else
                {
                    src.GetBytes(srcIndex, buf, index, length);
                }
            }
        }
Exemplo n.º 11
0
        protected override void MemoryCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)
        {
            if (length == 0)
            {
                return;
            }

            PlatformDependent.CopyMemory(src, srcOffset, dst, dstOffset, length);
        }
Exemplo n.º 12
0
        public override IByteBuffer Copy(int index, int length)
        {
            this.CheckIndex(index, length);
            var copiedArray = new byte[length];

            PlatformDependent.CopyMemory(this.array, index, copiedArray, 0, length);

            return(new UnpooledHeapByteBuffer(this.Allocator, copiedArray, this.MaxCapacity));
        }
Exemplo n.º 13
0
        public byte[] ToByteArray(int start, int end)
        {
            int count = end - start;
            var bytes = new byte[count];

            PlatformDependent.CopyMemory(this.value, this.offset + start, bytes, 0, count);

            return(bytes);
        }
Exemplo n.º 14
0
 public sealed override IByteBuffer SetBytes(int index, byte[] src, int srcIndex, int length)
 {
     if (src is null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.src);
     }
     CheckSrcIndex(index, length, srcIndex, src.Length);
     PlatformDependent.CopyMemory(src, srcIndex, Memory, Idx(index), length);
     return(this);
 }
Exemplo n.º 15
0
 public sealed override IByteBuffer GetBytes(int index, byte[] dst, int dstIndex, int length)
 {
     if (dst is null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst);
     }
     CheckDstIndex(index, length, dstIndex, dst.Length);
     PlatformDependent.CopyMemory(Memory, Idx(index), dst, dstIndex, length);
     return(this);
 }
Exemplo n.º 16
0
 private static void GetBytes0(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
 {
     if (dst.HasArray)
     {
         PlatformDependent.CopyMemory(addr, dst.Array, dst.ArrayOffset + dstIndex, length);
     }
     else
     {
         _ = dst.SetBytes(dstIndex, buf, index, length);
     }
 }
Exemplo n.º 17
0
 private static void SetBytes0(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
 {
     if (src.HasArray)
     {
         PlatformDependent.CopyMemory(src.Array, src.ArrayOffset + srcIndex, addr, length);
     }
     else
     {
         _ = src.GetBytes(srcIndex, buf, index, length);
     }
 }
Exemplo n.º 18
0
 internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] dst, int dstIndex, int length)
 {
     if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length))
     {
         throw new IndexOutOfRangeException($"dstIndex: {dstIndex}");
     }
     if (length != 0)
     {
         PlatformDependent.CopyMemory(addr, dst, dstIndex, length);
     }
 }
Exemplo n.º 19
0
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a copy of the specified array.
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="array">A buffer we're going to copy.</param>
        /// <param name="offset">The index offset from which we're going to read array.</param>
        /// <param name="length">
        ///     The number of bytes we're going to read from array beginning from position offset.
        /// </param>
        /// <returns>The new buffer that copies the contents of array.</returns>
        public static IByteBuffer CopiedBuffer(byte[] array, int offset, int length)
        {
            if (length == 0)
            {
                return(Empty);
            }

            var copy = new byte[length];

            PlatformDependent.CopyMemory(array, offset, copy, 0, length);
            return(WrappedBuffer(copy));
        }
Exemplo n.º 20
0
        internal static void GetBytes(byte *addr, byte[] dst, int dstIndex, int length)
        {
            //if (dst is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst); }

            //if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length))
            //{
            //    ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex);
            //}
            //if (length != 0)
            //{
            PlatformDependent.CopyMemory(addr, dst, dstIndex, length);
            //}
        }
        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] dst, int dstIndex, int length)
        {
            Contract.Requires(dst != null);

            if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex);
            }
            if (length != 0)
            {
                PlatformDependent.CopyMemory(addr, dst, dstIndex, length);
            }
        }
Exemplo n.º 22
0
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a copy of the specified array
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="array">A buffer we're going to copy.</param>
        /// <returns>The new buffer that copies the contents of array.</returns>
        public static IByteBuffer CopiedBuffer(byte[] array)
        {
            if (array.Length == 0)
            {
                return(Empty);
            }

            var newArray = new byte[array.Length];

            PlatformDependent.CopyMemory(array, 0, newArray, 0, array.Length);

            return(WrappedBuffer(newArray));
        }
Exemplo n.º 23
0
        private AsciiString Concat0(ICharSequence charSequence)
        {
            int thisLen = this.length;
            int thatLen = charSequence.Count;

            var newValue = new byte[thisLen + thatLen];

            PlatformDependent.CopyMemory(this.value, this.offset, newValue, 0, thisLen);
            for (int i = thisLen, j = 0; i < newValue.Length; i++, j++)
            {
                newValue[i] = CharToByte(charSequence[j]);
            }

            return(new AsciiString(newValue, false));
        }
Exemplo n.º 24
0
        public AsciiString(string value, Encoding encoding, int start, int length)
        {
            int count = encoding.GetMaxByteCount(length);
            var bytes = new byte[count];

            count = encoding.GetBytes(value, start, length, bytes, 0);

            var thisVal = new byte[count];

            PlatformDependent.CopyMemory(bytes, 0, thisVal, 0, count);

            this.offset = 0;
            this.length = thisVal.Length;
            this.value  = thisVal;
        }
Exemplo n.º 25
0
        public void Copy(int srcIdx, byte[] dst, int dstIdx, int count)
        {
            if (dst is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst);
            }

            if (MathUtil.IsOutOfBounds(srcIdx, count, this.length))
            {
                ThrowIndexOutOfRangeException_SrcIndex(srcIdx, count, this.length);
            }
            if (0u >= (uint)count)
            {
                return;
            }

            PlatformDependent.CopyMemory(this.value, srcIdx + this.offset, dst, dstIdx, count);
        }
Exemplo n.º 26
0
 internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream output, int length)
 {
     if (length != 0)
     {
         IByteBuffer tmpBuf = buf.Allocator.Buffer(length);
         try
         {
             byte[] tmp    = tmpBuf.Array;
             int    offset = tmpBuf.ArrayOffset;
             PlatformDependent.CopyMemory(addr, tmp, offset, length);
             output.Write(tmp, offset, length);
         }
         finally
         {
             tmpBuf.Release();
         }
     }
 }
Exemplo n.º 27
0
        public AsciiString(byte[] value, int start, int length, bool copy)
        {
            if (copy)
            {
                this.value = new byte[length];
                PlatformDependent.CopyMemory(value, start, this.value, 0, length);
                this.offset = 0;
            }
            else
            {
                if (MathUtil.IsOutOfBounds(start, length, value.Length))
                {
                    ThrowIndexOutOfRangeException_Start(start, length, value.Length);
                }

                this.value  = value;
                this.offset = start;
            }

            this.length = length;
        }
Exemplo n.º 28
0
        public override IByteBuffer AdjustCapacity(int newCapacity)
        {
            this.CheckNewCapacity(newCapacity);

            int oldCapacity = this.array.Length;

            byte[] oldArray = this.array;
            if (newCapacity > oldCapacity)
            {
                byte[] newArray = this.AllocateArray(newCapacity);
                PlatformDependent.CopyMemory(this.array, 0, newArray, 0, this.array.Length);

                this.SetArray(newArray);
                this.FreeArray(oldArray);
            }
            else if (newCapacity < oldCapacity)
            {
                byte[] newArray    = this.AllocateArray(newCapacity);
                int    readerIndex = this.ReaderIndex;
                if (readerIndex < newCapacity)
                {
                    int writerIndex = this.WriterIndex;
                    if (writerIndex > newCapacity)
                    {
                        this.SetWriterIndex0(writerIndex = newCapacity);
                    }

                    PlatformDependent.CopyMemory(this.array, readerIndex, newArray, 0, writerIndex - readerIndex);
                }
                else
                {
                    this.SetIndex(newCapacity, newCapacity);
                }

                this.SetArray(newArray);
                this.FreeArray(oldArray);
            }
            return(this);
        }
Exemplo n.º 29
0
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a merged copy of of the specified arrays.
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="arrays"></param>
        /// <returns></returns>
        public static IByteBuffer CopiedBuffer(params byte[][] arrays)
        {
            switch (arrays.Length)
            {
            case 0:
                return(Empty);

            case 1:
                return(0u >= (uint)arrays[0].Length ? Empty : CopiedBuffer(arrays[0]));
            }

            // Merge the specified arrays into one array.
            int length = 0;

            foreach (byte[] a in arrays)
            {
                if (int.MaxValue - length < a.Length)
                {
                    ThrowHelper.ThrowArgumentException_CopyArray();
                }
                length += a.Length;
            }

            if (0u >= (uint)length)
            {
                return(Empty);
            }

            var mergedArray = new byte[length];

            for (int i = 0, j = 0; i < arrays.Length; i++)
            {
                byte[] a = arrays[i];
                PlatformDependent.CopyMemory(a, 0, mergedArray, j, a.Length);
                j += a.Length;
            }

            return(WrappedBuffer(mergedArray));
        }
Exemplo n.º 30
0
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a merged copy of of the specified arrays.
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="arrays"></param>
        /// <returns></returns>
        public static IByteBuffer CopiedBuffer(params byte[][] arrays)
        {
            switch (arrays.Length)
            {
            case 0:
                return(Empty);

            case 1:
                return(arrays[0].Length == 0 ? Empty : CopiedBuffer(arrays[0]));
            }

            // Merge the specified arrays into one array.
            int length = 0;

            foreach (byte[] a in arrays)
            {
                if (int.MaxValue - length < a.Length)
                {
                    throw new ArgumentException("The total length of the specified arrays is too big.");
                }
                length += a.Length;
            }

            if (length == 0)
            {
                return(Empty);
            }

            var mergedArray = new byte[length];

            for (int i = 0, j = 0; i < arrays.Length; i++)
            {
                byte[] a = arrays[i];
                PlatformDependent.CopyMemory(a, 0, mergedArray, j, a.Length);
                j += a.Length;
            }

            return(WrappedBuffer(mergedArray));
        }