示例#1
0
        public static void SetBoolean(this IArrayBuffer <byte> buffer, int start, bool value)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start);
            buffer.Array[buffer.WriterIndex] = (byte)(value ? 1 : 0);
        }
示例#2
0
        public static string GetString(this IArrayBuffer <byte> buffer, int start, byte[] separator, Encoding encoding, out int count)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(encoding != null);
            Contract.Requires(separator != null && separator.Length > 0);

            buffer.CheckIndex(start);
            int index = buffer.ArrayOffset + start;

            if (buffer.ReadableCount < separator.Length)
            {
                count = buffer.ReadableCount;
                return(encoding.GetString(buffer.Array, index, count));
            }

            int frameLength = IndexOf(buffer, separator);

            if (frameLength == 0) // Leading separator
            {
                frameLength = separator.Length;
            }
            else if (frameLength < 0) // Not found
            {
                frameLength = buffer.ReadableCount;
            }

            count = frameLength;
            return(encoding.GetString(buffer.Array, index, count));
        }
示例#3
0
        public static void SetInt64(this IArrayBuffer <byte> buffer, int start, long value, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(long));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                buffer.Array[index]     = (byte)value;
                buffer.Array[index + 1] = (byte)(value >> 8);
                buffer.Array[index + 2] = (byte)(value >> 16);
                buffer.Array[index + 3] = (byte)(value >> 24);
                buffer.Array[index + 4] = (byte)(value >> 32);
                buffer.Array[index + 5] = (byte)(value >> 40);
                buffer.Array[index + 6] = (byte)(value >> 48);
                buffer.Array[index + 7] = (byte)(value >> 56);
            }
            else
            {
                buffer.Array[index]     = (byte)(value >> 56);
                buffer.Array[index + 1] = (byte)(value >> 48);
                buffer.Array[index + 2] = (byte)(value >> 40);
                buffer.Array[index + 3] = (byte)(value >> 32);
                buffer.Array[index + 4] = (byte)(value >> 24);
                buffer.Array[index + 5] = (byte)(value >> 16);
                buffer.Array[index + 6] = (byte)(value >> 8);
                buffer.Array[index + 7] = (byte)value;
            }
        }
示例#4
0
        public static long GetInt64(this IArrayBuffer <byte> buffer, int start, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(long));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                // ReSharper disable once RedundantCast
                return((long)buffer.Array[index]
                       | (long)buffer.Array[index + 1] << 8
                       | (long)buffer.Array[index + 2] << 16
                       | (long)buffer.Array[index + 3] << 24
                       | (long)buffer.Array[index + 4] << 32
                       | (long)buffer.Array[index + 5] << 40
                       | (long)buffer.Array[index + 6] << 48
                       | (long)buffer.Array[index + 7] << 56);
            }

            return(((long)buffer.Array[index] & 0xFF) << 56
                   | ((long)buffer.Array[index + 1] & 0xFF) << 48
                   | ((long)buffer.Array[index + 2] & 0xFF) << 40
                   | ((long)buffer.Array[index + 3] & 0xFF) << 32
                   | ((long)buffer.Array[index + 4] & 0xFF) << 24
                   | ((long)buffer.Array[index + 5] & 0xFF) << 16
                   | ((long)buffer.Array[index + 6] & 0xFF) << 8
                   | ((long)buffer.Array[index + 7] & 0xFF));
        }
示例#5
0
        public static double GetDouble(this IArrayBuffer <byte> buffer, int start, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(double));
            return(BitConverter.Int64BitsToDouble(
                       buffer.GetInt64(start, BitConverter.IsLittleEndian == isLittleEndian)));
        }
示例#6
0
        public static float GetFloat(this IArrayBuffer <byte> buffer, int start, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(float));
            return(ByteBufferUtil.Int32BitsToSingle(
                       buffer.GetInt32(start, BitConverter.IsLittleEndian == isLittleEndian)));
        }
示例#7
0
        public static void SetFloat(this IArrayBuffer <byte> buffer, int start, float value, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(float));
            buffer.SetInt32(start,
                            ByteBufferUtil.SingleToInt32Bits(value),
                            BitConverter.IsLittleEndian == isLittleEndian);
        }
示例#8
0
        public static bool GetBoolean(this IArrayBuffer <byte> buffer, int start)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start);
            int index = buffer.ArrayOffset + start;

            return(buffer.Array[index] != 0);
        }
示例#9
0
        public static ushort GetUInt16(this IArrayBuffer <byte> buffer, int start, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(ushort));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                return((ushort)(buffer.Array[index]
                                | buffer.Array[index + 1] << 8));
            }

            return((ushort)(buffer.Array[index] << 8
                            | buffer.Array[index + 1] & 0xFF));
        }
示例#10
0
        public static string GetString(this IArrayBuffer <byte> buffer, int start, int length, Encoding encoding)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(encoding != null);
            Contract.Requires(length >= 0);

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

            buffer.CheckIndex(start, length);
            int index = buffer.ArrayOffset + start;

            return(encoding.GetString(buffer.Array, index, length));
        }
示例#11
0
        public static int SetString(this IArrayBuffer <byte> buffer, int start, string value, Encoding encoding)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(encoding != null);

            if (string.IsNullOrEmpty(value))
            {
                return(0);
            }

            byte[] data = encoding.GetBytes(value);
            buffer.CheckIndex(start, data.Length);

            int index = buffer.ArrayOffset + start;

            buffer.Set(index, data);
            return(data.Length);
        }
示例#12
0
        public static void SetInt16(this IArrayBuffer <byte> buffer, int start, short value, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(ushort));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                buffer.Array[index]     = (byte)value;
                buffer.Array[index + 1] = ((byte)(value >> 8));
            }
            else
            {
                buffer.Array[index]     = (byte)(value >> 8);
                buffer.Array[index + 1] = (byte)value;
            }
        }
示例#13
0
        public static int GetInt32(this IArrayBuffer <byte> buffer, int start, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(int));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                return(buffer.Array[index]
                       | buffer.Array[index + 1] << 8
                       | buffer.Array[index + 2] << 16
                       | buffer.Array[index + 3] << 24);
            }

            return((buffer.Array[index] & 0xFF) << 24
                   | (buffer.Array[index + 1] & 0xFF) << 16
                   | (buffer.Array[index + 2] & 0xFF) << 8
                   | (buffer.Array[index + 3] & 0xFF));
        }