Example #1
0
        private void Get(int offset, int getLength, Bytes intoBytes)
        {
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(string.Format("offset {0} must be >= 0", offset));
            }

            if (getLength < 0)
            {
                throw new ArgumentOutOfRangeException(string.Format("getLength {0} must be >= 0", getLength));
            }

            if (offset >= Length)
            {
                throw new ArgumentOutOfRangeException(string.Format("offset {0} must be < Length {1}", offset, Length));
            }

            if ((getLength + offset) > Length)
            {
                throw new ArgumentOutOfRangeException(
                          string.Format("offset {0} + getLength {1} = {2} must be < Length {3}", offset, getLength, offset + getLength,
                                        Length));
            }

            if (IsArray)
            {
                if (offset == 0 && getLength == Length)
                {
                    intoBytes.Append(_byteArray);
                    return;
                }

                intoBytes.Append(MidByteArray(_byteArray, offset, getLength));
                return;
            }

            foreach (Bytes bytes in _bytesList)
            {
                if (bytes.Length <= offset)
                {
                    offset -= bytes.Length;
                    continue;
                }

                if (bytes.Length >= (offset + getLength))
                {
                    bytes.Get(offset, getLength, intoBytes);
                    return;
                }

                int lengthToGet = bytes.Length - offset;
                bytes.Get(offset, lengthToGet, intoBytes);
                getLength -= lengthToGet;
                offset     = 0;
            }
        }
Example #2
0
        public void GetByteArrayMultipleTimes()
        {
            byte[] a     = new byte[] { 0x01, 0x02 };
            Bytes  bytes = new Bytes(a);

            bytes.Append(new byte[] { 0x03, 0x04, 0x05, 0x06 });
            bytes.Append(new byte[] { 0x07 });
            Assert.AreEqual(7, bytes.ByteArray.Length);
            Assert.AreEqual(7, bytes.ByteArray.Length);
        }
Example #3
0
        public void AppendBytesObject()
        {
            Bytes bytes = new Bytes(new byte[] { sixteenBytes[0], sixteenBytes[1] });

            bytes.Append(new Bytes(new byte[] { sixteenBytes[2], sixteenBytes[3] }));
            Assert.AreEqual(4, bytes.Length);
            Assert.IsFalse(bytes.IsArray);
        }
Example #4
0
        public void ConvertArrayToList()
        {
            Bytes bytes = new Bytes(new byte[] { sixteenBytes[0], sixteenBytes[1] });

            Assert.IsTrue(bytes.IsArray);
            bytes.Append(new byte[] { sixteenBytes[2], sixteenBytes[3] });
            Assert.IsFalse(bytes.IsArray);
        }
Example #5
0
        private void TestPerformance(int sets, int setLength)
        {
            byte[] set = GetByteArray(setLength);

            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();

            long aTicks, bTicks;

            //test manual
            stopwatch.Start();
            byte[] aResultArray = new byte[0];
            for (int i = 0; i < sets; i++)
            {
                byte[] subResult = ManuallyAppend(set, new byte[1] {
                    (byte)i
                });
                aResultArray = ManuallyAppend(subResult, aResultArray);
            }
            stopwatch.Stop();
            aTicks = stopwatch.ElapsedTicks;
            System.Diagnostics.Debug.WriteLine(string.Format("ArMan: {0} ticks", aTicks));

            //test byteswise
            stopwatch.Reset();
            stopwatch.Start();
            Bytes bResult = new Bytes();

            for (int i = 0; i < sets; i++)
            {
                Bytes subResult = new Bytes();
                subResult.Append(new byte[] { (byte)i });
                subResult.Append(set);
                bResult.Append(subResult);
            }
            byte[] bResultArray = bResult.ByteArray;
            stopwatch.Stop();
            bTicks = stopwatch.ElapsedTicks;
            System.Diagnostics.Debug.WriteLine(string.Format("Bytes: {0} ticks", bTicks));
            System.Diagnostics.Debug.WriteLine(string.Format("Bytes advantage factor: {0}", bTicks == 0 ? aTicks == bTicks ? 0 : 1 : aTicks / ((decimal)bTicks)));
            AssertArraysAreEqual(aResultArray, bResultArray);
        }
Example #6
0
        public void Test_Get()
        {
            Bytes bytes = new Bytes();

            Bytes newBytes = new Bytes();

            newBytes.Append(new byte[] { sixteenBytes[0], sixteenBytes[1] });
            newBytes.Append(new byte[] { sixteenBytes[2], sixteenBytes[3] });
            bytes.Append(newBytes);

            bytes.Append(new Bytes(new byte[] { sixteenBytes[4], sixteenBytes[5] }));

            newBytes = new Bytes();
            newBytes.Append(new byte[] { sixteenBytes[6], sixteenBytes[7], sixteenBytes[8] });
            newBytes.Append(new byte[] { sixteenBytes[9] });
            bytes.Append(newBytes);

            newBytes = new Bytes();
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[10] }));
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[11] }));
            bytes.Append(newBytes);

            newBytes = new Bytes();
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[12] }));
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[13], sixteenBytes[14] }));
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[15] }));
            bytes.Append(newBytes);

            AssertArraysAreEqual(bytes.ByteArray, sixteenBytes);

            for (int offset = 0; offset < 16; offset++)
            {
                for (int length = 0; length <= (16 - offset); length++)
                {
                    AssertArraysAreEqual(Bytes.MidByteArray(sixteenBytes, offset, length), bytes.Get(offset, length).ByteArray);
                }
            }
        }
Example #7
0
        public void LittleEndian0x01()
        {
            byte  b     = 0x01;
            Bytes bytes = new Bytes();

            bytes.Append(b);
            bool[] bits = bytes.GetBits().Values;
            Assert.AreEqual(8, bits.Length, "Bits length");
            Assert.IsTrue(bits[0], "Bit 0 value");
            for (int i = 1; i < 8; i++)
            {
                Assert.IsFalse(bits[i], string.Format("Bit {0} value", i));
            }
        }
Example #8
0
        public void LittleEndian0x0101()
        {
            byte[] bs    = new byte[] { 0x01, 0x01 };
            Bytes  bytes = new Bytes();

            bytes.Append(bs);
            bool[] bits = bytes.GetBits().Values;
            Assert.AreEqual(16, bits.Length, "Bits length");
            Assert.IsTrue(bits[0], "Bit 0 value");
            Assert.IsTrue(bits[8], "Bit 8 value");
            for (int i = 1; i < 8; i++)
            {
                Assert.IsFalse(bits[i], string.Format("Bit {0} value", i));
            }
            for (int i = 9; i < 16; i++)
            {
                Assert.IsFalse(bits[i], string.Format("Bit {0} value", i));
            }
        }