Example #1
0
        public void ShiftRight(TypeCode typeCode, IConvertible value)
        {
            if (_rightOffsetWrite > _buffer.Length)
            {
                throw new IndexOutOfRangeException("右偏移溢出!");
            }

            var size = value.GetSize(typeCode);

            if (_rightOffsetWrite + size > _buffer.Length)
            {
                throw new IndexOutOfRangeException("右偏移溢出!");
            }

            if (WriteRemaining() < size)
            {
                throw new IndexOutOfRangeException("缓冲区溢出!");
            }

            _rightOffsetWrite += value.CopyTo(typeCode, _buffer, _rightOffsetWrite);
        }
Example #2
0
        public void ShiftLeft(TypeCode typeCode, IConvertible value)
        {
            if (_leftOffsetWrite < 0)
            {
                throw new IndexOutOfRangeException("左偏移溢出!");
            }

            var size = value.GetSize(typeCode);

            if (_leftOffsetWrite < size)
            {
                throw new IndexOutOfRangeException("左偏移溢出!");
            }

            if (WriteRemaining() < size)
            {
                throw new IndexOutOfRangeException("缓冲区溢出!");
            }

            _leftOffsetWrite -= size;
            value.CopyTo(typeCode, _buffer, _leftOffsetWrite);
        }
        public static int CopyTo(this IConvertible value, TypeCode typeCode, byte[] buffer, int offset)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset < 0 || offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            var size = value.GetSize(typeCode);

            if (buffer.Length - offset < size)
            {
                throw new ArgumentException(nameof(buffer) + "缓冲区太小!");
            }

            var bytes = value.ToBytes(typeCode);

            Buffer.BlockCopy(bytes, 0, buffer, offset, bytes.Length);
            return(bytes.Length);
        }