public static void runTest()
            {
                Stats stats = new Stats();

                BinaryMemoryStream buf = null;

                // populate stats to give an average
                stats.Update(10000);
                stats.Update(11000);
                stats.Update(12000);
                stats.Update(13000);
                stats.Update(14000);

                buf = stats.InstantiateBuffer();
                Assert.AreEqual(buf.Capacity, (14000));

                // now make a really big request
                stats.Update(1024*1024*4);
                buf = stats.InstantiateBuffer();
                Assert.AreEqual(buf.Capacity, (1024*1024*4));

                // request an average size buffer and check the size
                stats.Update(1000);
                buf = stats.InstantiateBuffer();
                Assert.AreEqual(buf.Capacity, (1000 + 8));
            }
        public void TestGetBuffer1()
        {
            BinaryMemoryStream stream = new BinaryMemoryStream();

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, 0);
            stream.GetBuffer();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Decorate the specified <see cref="Binary"/> with the specified integer decoration.
        /// </summary>
        /// <param name="binValue">The <see cref="Binary"/> to be decorated.</param>
        /// <param name="decoration">The integer decoration.</param>
        /// <returns>The decorated (with integer decoration) <see cref="Binary"/> object.</returns>
        /// <since>Coherence 3.7.1</since>
        public static Binary DecorateBinary(Binary binValue, Int32 decoration)
        {
            var stream = new BinaryMemoryStream(6 + binValue.Length);
            var writer = new DataWriter(stream);

            writer.Write((byte)SerializationFormat.FMT_IDO);
            writer.WritePackedInt32(decoration);
            binValue.WriteTo(writer);

            return(stream.ToBinary());
        }
        public void TestDefaultConstructor()
        {
            BinaryMemoryStream stream = new BinaryMemoryStream();

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, 0);
            Assert.IsTrue(stream.CanWrite);
            stream.WriteByte(1);
            Assert.AreEqual(stream.Length, 1);
            stream.ToBinary(); //this invokes streamGetInternalByteArray and makes stream immutable
            //byte[] arr = stream.GetInternalByteArray();
            //Assert.IsNotNull(arr);
            //Assert.AreEqual(arr.Length, 1);
            Assert.IsFalse(stream.CanWrite);
            Exception ex = TryWrite(stream);

            Assert.IsNotNull(ex);
            Assert.IsInstanceOf(typeof(NotSupportedException), ex);
        }
        public void TestToAndFromBinary()
        {
            ISerializer serializer = new BinarySerializer();
            string      original   = "hello";
            Binary      bin        = SerializationHelper.ToBinary(original, serializer);
            string      copy       = (string) SerializationHelper.FromBinary(bin, serializer);
            Assert.AreEqual(original, copy);

            var stream = new BinaryMemoryStream();
            serializer.Serialize(new DataWriter(stream), original);
            try
            {
                // throws IOException as type (fmt_ext) information is missing
                SerializationHelper.FromBinary(stream.ToBinary(), serializer);
                Assert.Fail();
            }
            catch (IOException)
            {
            }
        }
        /// <summary>
        /// Initializes a new Binary instance from
        /// <see cref="BinaryMemoryStream"/>.
        /// </summary>
        /// <param name="stream">
        /// A <b>BinaryMemoryStream</b> instance.
        /// </param>
        internal Binary(BinaryMemoryStream stream)
        {
            byte[] ab      = stream.GetInternalByteArray();
            int    cbData  = (int)stream.Length;
            int    cbTotal = ab.Length;
            int    cbWaste = cbTotal - cbData;

            // tolerate up to 12.5% waste
            if (cbWaste <= Math.Max(16, NumberUtils.URShift(cbTotal, 3)))
            {
                m_ab = ab;
            }
            else
            {
                byte[] abNew = new byte[cbData];
                Array.Copy(ab, 0, abNew, 0, cbData);
                m_ab = abNew;
            }
            m_cb = cbData;
        }
        public void TestConstructorWithCapacity()
        {
            BinaryMemoryStream stream = new BinaryMemoryStream(4);

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, 0);
            Assert.AreEqual(stream.Capacity, 4);
            Assert.IsTrue(stream.CanWrite);
            Exception ex = TryWrite(stream);

            Assert.IsNull(ex);
            Assert.AreEqual(stream.Length, 1);
            stream.ToBinary();
            //byte[] arr = stream.GetInternalByteArray();
            //Assert.IsNotNull(arr);
            //Assert.AreEqual(arr.Length, 1);
            Assert.IsFalse(stream.CanWrite);
            ex = TryWrite(stream);
            Assert.IsNotNull(ex);
            Assert.IsInstanceOf(typeof(NotSupportedException), ex);
        }