ToByteArray() public method

Allocates a new byte array, copies the payload data into it and returns it.
public ToByteArray ( ) : byte[]
return byte[]
Example #1
0
        public virtual void  TestPayload()
        {
            rnd = NewRandom();
            byte[]  testData = System.Text.UTF8Encoding.UTF8.GetBytes("This is a test!");
            Payload payload  = new Payload(testData);

            Assert.AreEqual(testData.Length, payload.Length(), "Wrong payload length.");

            // test copyTo()
            byte[] target = new byte[testData.Length - 1];
            try
            {
                payload.CopyTo(target, 0);
                Assert.Fail("Expected exception not thrown");
            }
            catch (System.Exception expected)
            {
                // expected exception
            }

            target = new byte[testData.Length + 3];
            payload.CopyTo(target, 3);

            for (int i = 0; i < testData.Length; i++)
            {
                Assert.AreEqual(testData[i], target[i + 3]);
            }


            // test toByteArray()
            target = payload.ToByteArray();
            AssertByteArrayEquals(testData, target);

            // test byteAt()
            for (int i = 0; i < testData.Length; i++)
            {
                Assert.AreEqual(payload.ByteAt(i), testData[i]);
            }

            try
            {
                payload.ByteAt(testData.Length + 1);
                Assert.Fail("Expected exception not thrown");
            }
            catch (System.Exception expected)
            {
                // expected exception
            }

            Payload clone = (Payload)payload.Clone();

            Assert.AreEqual(payload.Length(), clone.Length());
            for (int i = 0; i < payload.Length(); i++)
            {
                Assert.AreEqual(payload.ByteAt(i), clone.ByteAt(i));
            }
        }
Example #2
0
		public virtual void  TestPayload()
		{
			rnd = NewRandom();
			byte[] testData = System.Text.UTF8Encoding.UTF8.GetBytes("This is a test!");
			Payload payload = new Payload(testData);
			Assert.AreEqual(testData.Length, payload.Length(), "Wrong payload length.");
			
			// test copyTo()
			byte[] target = new byte[testData.Length - 1];
			try
			{
				payload.CopyTo(target, 0);
				Assert.Fail("Expected exception not thrown");
			}
			catch (System.Exception expected)
			{
				// expected exception
			}
			
			target = new byte[testData.Length + 3];
			payload.CopyTo(target, 3);
			
			for (int i = 0; i < testData.Length; i++)
			{
				Assert.AreEqual(testData[i], target[i + 3]);
			}
			
			
			// test toByteArray()
			target = payload.ToByteArray();
			AssertByteArrayEquals(testData, target);
			
			// test byteAt()
			for (int i = 0; i < testData.Length; i++)
			{
				Assert.AreEqual(payload.ByteAt(i), testData[i]);
			}
			
			try
			{
				payload.ByteAt(testData.Length + 1);
				Assert.Fail("Expected exception not thrown");
			}
			catch (System.Exception expected)
			{
				// expected exception
			}
			
			Payload clone = (Payload) payload.Clone();
			Assert.AreEqual(payload.Length(), clone.Length());
			for (int i = 0; i < payload.Length(); i++)
			{
				Assert.AreEqual(payload.ByteAt(i), clone.ByteAt(i));
			}
		}