Esempio n. 1
0
        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
        {
            Contract.Requires(dst != null);

            if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_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);
            }
        }
Esempio n. 2
0
        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 T Init <T>(
            AbstractByteBuffer unwrapped, IByteBuffer wrapped, int readerIndex, int writerIndex, int maxCapacity)
            where T : AbstractPooledDerivedByteBuffer
        {
            wrapped.Retain(); // Retain up front to ensure the parent is accessible before doing more work.
            this.parent     = wrapped;
            this.rootParent = unwrapped;

            try
            {
                this.SetMaxCapacity(maxCapacity);
                this.SetIndex0(readerIndex, writerIndex); // It is assumed the bounds checking is done by the caller.
                this.SetReferenceCount(1);

                wrapped = null;
                return((T)this);
            }
            finally
            {
                if (wrapped != null)
                {
                    this.parent = this.rootParent = null;
                    wrapped.Release();
                }
            }
        }
Esempio n. 4
0
        internal static PooledDuplicatedByteBuffer NewInstance(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int readerIndex, int writerIndex)
        {
            PooledDuplicatedByteBuffer duplicate = Recycler.Take();

            duplicate.Init <PooledDuplicatedByteBuffer>(unwrapped, wrapped, readerIndex, writerIndex, unwrapped.MaxCapacity);
            duplicate.MarkReaderIndex();
            duplicate.MarkWriterIndex();

            return(duplicate);
        }
 internal static int WriteAscii(AbstractByteBuffer buffer, int writerIndex, string value, int len)
 {
     // We can use the _set methods as these not need to do any index checks and reference checks.
     // This is possible as we called ensureWritable(...) before.
     for (int i = 0; i < len; i++)
     {
         buffer._SetByte(writerIndex++, (byte)value[i]);
     }
     return(len);
 }
        static PooledSlicedByteBuffer NewInstance0(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int adjustment, int length)
        {
            PooledSlicedByteBuffer slice = Recycler.Take();

            slice.Init <PooledSlicedByteBuffer>(unwrapped, wrapped, 0, length, length);
            slice.DiscardMarks();
            slice.adjustment = adjustment;

            return(slice);
        }
Esempio n. 7
0
        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);
            }
        }
Esempio n. 8
0
 internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream output, int length)
 {
     if (length != 0)
     {
         IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);
         try
         {
             byte[] tmp    = tmpBuf.Array;
             int    offset = tmpBuf.ArrayOffset;
             PlatformDependent.CopyMemory(addr, tmp, offset, length);
             output.Write(tmp, offset, length);
         }
         finally
         {
             tmpBuf.Release();
         }
     }
 }
        internal UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer, int readerIndex, int writerIndex)
            : base(buffer.MaxCapacity)
        {
            if (buffer is UnpooledDuplicatedByteBuffer duplicated)
            {
                this.buffer = duplicated.buffer;
            }
            else if (buffer is AbstractPooledDerivedByteBuffer)
            {
                this.buffer = (AbstractByteBuffer)buffer.Unwrap();
            }
            else
            {
                this.buffer = buffer;
            }

            this.SetIndex0(readerIndex, writerIndex);
            this.MarkIndex(); // Mark read and writer index
        }
Esempio n. 10
0
        internal static int SetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length)
        {
            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();
            }
        }
Esempio n. 11
0
        internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
        {
            Contract.Requires(src != null);

            if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_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);
                }
            }
        }
Esempio n. 12
0
        internal static void ThrowIndexOutOfRangeException_ReaderIndex(int minimumReadableBytes, int readerIndex, int writerIndex, AbstractByteBuffer buf)
        {
            throw GetIndexOutOfRangeException();

            IndexOutOfRangeException GetIndexOutOfRangeException()
            {
                return(new IndexOutOfRangeException(string.Format("readerIndex({0}) + length({1}) exceeds writerIndex({2}): {3}", readerIndex, minimumReadableBytes, writerIndex, buf)));
            }
        }
 public PooledNonRetainedSlicedByteBuffer(IReferenceCounted referenceCountDelegate, AbstractByteBuffer buffer, int index, int length)
     : base(buffer, index, length)
 {
     this.referenceCountDelegate = referenceCountDelegate;
 }
 internal PooledNonRetainedDuplicateByteBuffer(IReferenceCounted referenceCountDelegate, AbstractByteBuffer buffer)
     : base(buffer)
 {
     this.referenceCountDelegate = referenceCountDelegate;
 }
 public UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer)
     : this(buffer, buffer.ReaderIndex, buffer.WriterIndex)
 {
 }
        // Fast-Path implementation
        internal static int WriteUtf8(AbstractByteBuffer buffer, int writerIndex, string value, int len)
        {
            int oldWriterIndex = writerIndex;

            // We can use the _set methods as these not need to do any index checks and reference checks.
            // This is possible as we called ensureWritable(...) before.
            for (int i = 0; i < len; i++)
            {
                char c = value[i];
                if (c < 0x80)
                {
                    buffer._SetByte(writerIndex++, (byte)c);
                }
                else if (c < 0x800)
                {
                    buffer._SetByte(writerIndex++, (byte)(0xc0 | (c >> 6)));
                    buffer._SetByte(writerIndex++, (byte)(0x80 | (c & 0x3f)));
                }
                else if (char.IsSurrogate(c))
                {
                    if (!char.IsHighSurrogate(c))
                    {
                        buffer._SetByte(writerIndex++, WriteUtfUnknown);
                        continue;
                    }
                    char c2;
                    try
                    {
                        // Surrogate Pair consumes 2 characters. Optimistically try to get the next character to avoid
                        // duplicate bounds checking with charAt. If an IndexOutOfBoundsException is thrown we will
                        // re-throw a more informative exception describing the problem.
                        c2 = value[++i];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        buffer._SetByte(writerIndex++, WriteUtfUnknown);
                        break;
                    }
                    if (!char.IsLowSurrogate(c2))
                    {
                        buffer._SetByte(writerIndex++, WriteUtfUnknown);
                        buffer._SetByte(writerIndex++, char.IsHighSurrogate(c2) ? WriteUtfUnknown : c2);
                        continue;
                    }
                    int codePoint = CharUtil.ToCodePoint(c, c2);
                    // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630.
                    buffer._SetByte(writerIndex++, (byte)(0xf0 | (codePoint >> 18)));
                    buffer._SetByte(writerIndex++, (byte)(0x80 | ((codePoint >> 12) & 0x3f)));
                    buffer._SetByte(writerIndex++, (byte)(0x80 | ((codePoint >> 6) & 0x3f)));
                    buffer._SetByte(writerIndex++, (byte)(0x80 | (codePoint & 0x3f)));
                }
                else
                {
                    buffer._SetByte(writerIndex++, (byte)(0xe0 | (c >> 12)));
                    buffer._SetByte(writerIndex++, (byte)(0x80 | ((c >> 6) & 0x3f)));
                    buffer._SetByte(writerIndex++, (byte)(0x80 | (c & 0x3f)));
                }
            }

            return(writerIndex - oldWriterIndex);
        }
 internal static PooledSlicedByteBuffer NewInstance(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int index, int length)
 {
     CheckSliceOutOfBounds(index, length, unwrapped);
     return(NewInstance0(unwrapped, wrapped, index, length));
 }
Esempio n. 18
0
        internal static void ThrowIndexOutOfRangeException_WriterIndex(int minWritableBytes, int writerIndex, int maxCapacity, AbstractByteBuffer buf)
        {
            throw GetIndexOutOfRangeException();

            IndexOutOfRangeException GetIndexOutOfRangeException()
            {
                return(new IndexOutOfRangeException(string.Format($"writerIndex({0}) + minWritableBytes({1}) exceeds maxCapacity({2}): {3}", writerIndex, minWritableBytes, maxCapacity, buf)));
            }
        }
Esempio n. 19
0
 // No need to check length zero, the calling method already done it
 internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] src, int srcIndex, int length) =>
 PlatformDependent.CopyMemory(src, srcIndex, addr, length);