コード例 #1
0
        public static string ToString(byte[] bytes, int index, int length, bool wrap)
        {
            ThrowHelper.ArgumentNull((bytes == null), nameof(bytes));
            ThrowHelper.ArgumentIndexOutOfRange((index < 0), nameof(index));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((index + length) > bytes.Length));

            string[] buffer = new string[length];
            for (int i = index; i < index + length; i++)
            {
                buffer[i - index] = string.Format(
                    System.Globalization.CultureInfo.InvariantCulture,
                    "{0:X2}",
                    bytes[i]);
            }

            if (wrap)
            {
                string[][]    temp    = Meision.Collections.CollectionManager.Divide <string>(buffer, 16);
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < temp.Length; i++)
                {
                    builder.AppendLine(string.Join(" ", temp[i]));
                }
                return(builder.ToString());
            }
            else
            {
                return(string.Join(" ", buffer));
            }
        }
コード例 #2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            this.EnsureNotClosed();

            ThrowHelper.ArgumentNull((buffer == null), nameof(buffer));
            ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
            ThrowHelper.ArgumentLengthOutOfRange((count < 0), nameof(count));
            ThrowHelper.ArgumentIndexLengthOutOfArray((offset + count > buffer.Length));

            int n = Math.Min(this.Remaining, count);

            if (n <= 0)
            {
                return(0);
            }

            Debug.Assert(_index + n >= 0, "_position + n >= 0");  // len is less than 2^31 -1.

            if (n <= 8)
            {
                int byteCount = n;
                while (--byteCount >= 0)
                {
                    buffer[offset + byteCount] = this._buffer[this._index + byteCount];
                }
            }
            else
            {
                System.Buffer.BlockCopy(_buffer, _index, buffer, offset, n);
            }
            _index += n;
            return(n);
        }
コード例 #3
0
        public BufferStream(byte[] buffer, int offset, int count, bool writable, bool isLittleEndian)
        {
            ThrowHelper.ArgumentNull((buffer == null), nameof(buffer));
            ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
            ThrowHelper.ArgumentLengthOutOfRange((count < 0), nameof(count));
            ThrowHelper.ArgumentIndexLengthOutOfArray((offset + count > buffer.Length));

            this.InitFromExternBuffer(buffer, offset, count, writable, isLittleEndian);
        }
コード例 #4
0
        public static T[] GetSegment <T>(this T[] array, long offset, long length)
        {
            ThrowHelper.ArgumentNull((array == null), nameof(array));
            ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((offset + length) > array.Length));

            T[] buffer = new T[length];
            Array.Copy(array, offset, buffer, 0, length);
            return(buffer);
        }
コード例 #5
0
        public void ReadBytes(byte[] buffer, int index, int count)
        {
            ThrowHelper.ArgumentNull((buffer == null), nameof(buffer));
            ThrowHelper.ArgumentIndexOutOfRange((index < 0), nameof(index));
            ThrowHelper.ArgumentLengthOutOfRange((count < 0), nameof(count));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((index + count) > buffer.Length));

            this.EnsureGetSpace(count);

            System.Buffer.BlockCopy(this._buffer, this._index, buffer, index, count);
            this._index += count;
        }
コード例 #6
0
        public void WriteBytes(byte[] values, int index, int count)
        {
            ThrowHelper.ArgumentNull((values == null), nameof(values));
            ThrowHelper.ArgumentIndexOutOfRange((index < 0), nameof(index));
            ThrowHelper.ArgumentLengthOutOfRange((count < 0), nameof(count));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((index + count) > values.Length));

            this.EnsureWriteSpace(count);

            System.Buffer.BlockCopy(values, index, this._buffer, this._index, count);
            this._index += count;
        }
コード例 #7
0
        public static byte[] GetSegment(this byte[] array, int offset, int length)
        {
            // Validate parameter(s).
            ThrowHelper.ArgumentNull((array == null), nameof(array));
            ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((offset + length) > array.Length));

            byte[] buffer = new byte[length];
            System.Buffer.BlockCopy(array, offset, buffer, 0, length);
            return(buffer);
        }
コード例 #8
0
        public static byte[] ToByteArray <T>(T[] array, int index, int length)
        {
            ThrowHelper.ArgumentNull((array == null), nameof(array));
            ThrowHelper.ArgumentIndexOutOfRange((index < 0), nameof(index));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((index + length) > array.Length));

            byte[] buffer = new byte[length * Marshal.SizeOf(typeof(T))];
            IntPtr point  = Marshal.UnsafeAddrOfPinnedArrayElement(array, index);

            Marshal.Copy(point, buffer, 0, buffer.Length);

            return(buffer);
        }
コード例 #9
0
        /// <summary>
        /// Find the index of pattern in origin array. BMH algorithm search
        /// </summary>
        /// <param name="origin">origin bytes.</param>
        /// <param name="index">start index.</param>
        /// <param name="length">searching length</param>
        /// <param name="pattern">Find bytes.</param>
        /// <returns>Index position.</returns>
        public static int Search(byte[] origin, int index, int length, byte[] pattern)
        {
            // Validate parameter(s).
            ThrowHelper.ArgumentNull((origin == null), nameof(origin));
            ThrowHelper.ArgumentIndexOutOfRange((index < 0), nameof(index));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((index + length) > origin.Length));
            ThrowHelper.ArgumentNull((pattern == null), nameof(pattern));

            if ((pattern.Length == 0) ||
                (length < pattern.Length))
            {
                return(-1);
            }

            if (pattern.Length > 1)
            {
                #region Binary Search
                unsafe
                {
                    fixed(byte *pOrigin = origin)
                    fixed(byte *pPattern = pattern)
                    {
                        byte *result = BMHSearch(pOrigin + index, length, pPattern, pattern.Length);

                        if (result == null)
                        {
                            return(-1);
                        }
                        return((int)(result - pOrigin));
                    }
                }
                #endregion Binary Search
            }
            else
            {
                #region Normal Search
                byte b = pattern[0];
                for (int i = index; i < index + length; i++)
                {
                    if (origin[i] == b)
                    {
                        return(i);
                    }
                }
                return(-1);

                #endregion Normal Search
            }
        }
コード例 #10
0
        public static bool Compare <T>(T[] source, int sourceIndex, T[] destination, int destinationIndex, int length)
        {
            ThrowHelper.ArgumentNull((source == null), nameof(source));
            ThrowHelper.ArgumentIndexOutOfRange((sourceIndex < 0), nameof(sourceIndex));
            ThrowHelper.ArgumentNull((destination == null), nameof(destination));
            ThrowHelper.ArgumentIndexOutOfRange((destinationIndex < 0), nameof(destinationIndex));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray((((sourceIndex + length) > source.Length) || ((destinationIndex + length) > destination.Length)));

            for (int i = 0; i < length; i++)
            {
                if (!source[sourceIndex + i].Equals(destination[destinationIndex + i]))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #11
0
    /// <summary>
    /// Receive data by size from server.
    /// </summary>
    /// <param name="instance">socket instance</param>
    /// <param name="buffer">data array to receive</param>
    /// <param name="offset">offset in data</param>
    /// <param name="size">ensure receiving size</param>
    public static void ReceiveBySize(this Socket instance, byte[] buffer, int offset, int size, SocketFlags socketFlags = SocketFlags.None)
    {
        ThrowHelper.ArgumentNull((buffer == null), nameof(buffer));
        ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
        ThrowHelper.ArgumentLengthOutOfRange((size < 0), nameof(size));
        ThrowHelper.ArgumentIndexLengthOutOfArray(((offset + size) > buffer.Length));

        int remaining = size;

        while (remaining > 0)
        {
            int received = instance.Receive(buffer, offset + (size - remaining), remaining, socketFlags);
            if (received == 0)
            {
                throw new InvalidOperationException(SR.Socket_Exception_ReceiveEmtpy);
            }
            remaining -= received;
        }
    }
コード例 #12
0
        public static unsafe int GetHashCode(byte[] array, int offset, int length)
        {
            ThrowHelper.ArgumentNull((array == null), nameof(array));
            ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
            ThrowHelper.ArgumentLengthOutOfRange((length < 0), nameof(length));
            ThrowHelper.ArgumentIndexLengthOutOfArray(((offset + length) > array.Length));

            fixed(byte *pArray = &array[offset])
            {
                int hash  = 0;
                int times = length / sizeof(int);
                int mod   = length % sizeof(int);

                int *pointer = (int *)pArray;

                // Run times
                for (int i = 0; i < times; i++)
                {
                    hash ^= *pointer;
                    pointer++;
                }
                // compute last bytes.
                switch (mod)
                {
                case 1:
                    hash ^= (array[offset + length - 1]);
                    break;

                case 2:
                    hash ^= ((array[offset + length - 2]) | (array[offset + length - 1] << 8));
                    break;

                case 3:
                    hash ^= ((array[offset + length - 3]) | (array[offset + length - 2] << 8) | (array[offset + length - 1] << 16));
                    break;

                default:
                    break;
                }

                return(hash);
            }
        }
コード例 #13
0
        public override void SetLength(long value)
        {
            this.EnsureNotClosed();
            this.EnsureWriteable();

            ThrowHelper.ArgumentLengthOutOfRange((value <0 || value> BufferStream.MAX_LENGTH), nameof(value));
            ThrowHelper.ArgumentOutOfRange((value > (BufferStream.MAX_LENGTH - this._start)), nameof(value), SR.Stream_Exception_StreamLength);

            int  newLength         = _start + (int)value;
            bool allocatedNewArray = EnsureCapacity(newLength);

            if (!allocatedNewArray && newLength > _end)
            {
                Array.Clear(_buffer, _end, newLength - _end);
            }
            _end = newLength;
            if (_index > newLength)
            {
                _index = newLength;
            }
        }
コード例 #14
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            ThrowHelper.ArgumentNull((buffer == null), nameof(buffer));
            ThrowHelper.ArgumentIndexOutOfRange((offset < 0), nameof(offset));
            ThrowHelper.ArgumentLengthOutOfRange((count < 0), nameof(count));
            ThrowHelper.ArgumentIndexLengthOutOfArray((offset + count > buffer.Length));

            this.EnsureWriteSpace(count);

            if ((count <= 8) && (buffer != _buffer))
            {
                int byteCount = count;
                while (--byteCount >= 0)
                {
                    _buffer[_index + byteCount] = buffer[offset + byteCount];
                }
            }
            else
            {
                System.Buffer.BlockCopy(buffer, offset, _buffer, _index, count);
            }
            this._index += count;
        }