Ejemplo n.º 1
0
        public unsafe void Serialize(ref byte[] buffer, ref int offset, T[] value)
        {
            int count = value.Length;

            // Ensure capacity
            int size       = _itemSize;
            int neededSize = (count * size) + 5;

            SerializerBinary.EnsureCapacity(ref buffer, offset, neededSize);

            // Count
            SerializerBinary.WriteUInt32NoCheck(buffer, ref offset, (uint)count);

            int bytes = count * size;

            if (bytes == 0)
            {
                return;

                // Write
                fixed(T *srcAr = &value[0])
                fixed(byte *destAr = &buffer[0])
                {
                    byte *src  = (byte *)srcAr;
                    byte *dest = destAr + offset;

                    SerializerBinary.FastCopy(src, dest, (uint)bytes);
                }


                offset += bytes;
        }
Ejemplo n.º 2
0
        public void Deserialize(byte[] buffer, ref int offset, ref Bitmap img)
        {
            // Read data size
            int size = (int)SerializerBinary.ReadUInt32Fixed(buffer, ref offset);

            // Copy data into stream
            if (_sharedMemoryStream == null)
            {
                _sharedMemoryStream = new MemoryStream(size);
            }
            else if (_sharedMemoryStream.Capacity < size)
            {
                _sharedMemoryStream.Capacity = size;
            }

            var ms = _sharedMemoryStream;

            ms.Position = 0;
            var memoryStreamBuffer = ms.GetBuffer();

            if (size > 0)
            {
                SerializerBinary.FastCopy(buffer, offset, memoryStreamBuffer, 0, size);
            }

            // Now we can load the image back from the stream
            ms.Position = 0;

            img = new Bitmap(ms);

            offset += size;
        }
Ejemplo n.º 3
0
        public void FastCopy()
        {
#if NET45 || NET451 || NET452
            global::System.Console.WriteLine("Testing FastCopy on NET45.x");
#elif NET47 || NET471 || NET472
            global::System.Console.WriteLine("Testing FastCopy on NET47.x");
#elif NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
            global::System.Console.WriteLine("Testing FastCopy on NET STANDARD 2.0 / NETCOREAPP2_x");
#else
#error Unknown compiler version
#endif


            var sizes = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300, 400, 510, 511, 512, 513, 514, 1000, 5000, 10 * 1000, 100 * 1000 };

            List <byte[]> sourceArrays = new List <byte[]>();
            foreach (var s in sizes)
            {
                var ar = new byte[s];
                rng.NextBytes(ar);
                sourceArrays.Add(ar);
            }

            List <byte[]> targetArrays = new List <byte[]>();
            foreach (var s in sizes)
            {
                var ar = new byte[s];
                rng.NextBytes(ar);
                targetArrays.Add(ar);
            }


            for (int arIndex = 0; arIndex < sourceArrays.Count; arIndex++)
            {
                var size   = sizes[arIndex];
                var source = sourceArrays[arIndex];
                var target = targetArrays[arIndex];

                SerializerBinary.FastCopy(source, 0, target, 0, (uint)size);

                Assert.True(size == source.Length);
                Assert.True(size == target.Length);
                for (int i = 0; i < size; i++)
                {
                    Assert.True(source[i] == target[i]);
                }
            }
        }
Ejemplo n.º 4
0
        public void Serialize(ref byte[] buffer, ref int offset, Bitmap img)
        {
            // Let the image serialize itself to the memory stream
            // Its unfortunate that there's only a stream-based api...
            // The alternative would be manually locking the bits.
            // That would be easy, but we'd potentially lose some information (animation frames etc?)

            // Prepare buffer stream
            if (_sharedMemoryStream == null)
            {
                _sharedMemoryStream = new MemoryStream(200 * 1024);
            }

            var stream = _sharedMemoryStream;

            // Encode image into stream
            stream.Position = 0;
            var format = BitmapModeToImgFormat(BitmapMode);

            img.Save(stream, format);

            long sizeLong = stream.Length;

            if (sizeLong > int.MaxValue)
            {
                throw new InvalidOperationException("image too large");
            }
            int size = (int)sizeLong;

            stream.Position = 0;
            var streamBuffer = stream.GetBuffer();

            // Write Size
            SerializerBinary.WriteUInt32Fixed(ref buffer, ref offset, (uint)size);

            // Write stream data to serialization buffer
            if (size > 0)
            {
                SerializerBinary.EnsureCapacity(ref buffer, offset, size);
                SerializerBinary.FastCopy(streamBuffer, 0, buffer, (uint)offset, (uint)size);
            }

            offset += size;
        }
Ejemplo n.º 5
0
        public void Serialize(ref byte[] buffer, ref int offset, Bitmap img)
        {
            // Let the image serialize itself to the memory stream
            // Its unfortunate that there's only a stream-based api...
            // The alternative would be manually locking the bits.
            // That would be easy, but we'd potentially lose some information (animation frames etc?)

            var mode   = BitmapMode;
            var format = BitmapModeToImgFormat(mode);

            if (_sharedMemoryStream == null)
            {
                _sharedMemoryStream = new MemoryStream((int)(4 * (img.Width * img.Height) * 1.35));
            }
            var ms = _sharedMemoryStream;

            ms.Position = 0;
            img.Save(ms, format);

            long sizeLong = ms.Position;

            if (sizeLong > int.MaxValue)
            {
                throw new InvalidOperationException("image too large");
            }
            int size = (int)sizeLong;

            ms.Position = 0;
            var memoryStreamBuffer = ms.GetBuffer();

            // Write Size
            SerializerBinary.WriteUInt32Fixed(ref buffer, ref offset, (uint)size);

            // Write data into serialization buffer
            if (size > 0)
            {
                SerializerBinary.EnsureCapacity(ref buffer, offset, size);
                SerializerBinary.FastCopy(memoryStreamBuffer, 0, buffer, offset, size);
            }

            offset += size;
        }
Ejemplo n.º 6
0
        public void Deserialize(byte[] buffer, ref int offset, ref Bitmap img)
        {
            // Read data size
            int size = (int)SerializerBinary.ReadUInt32Fixed(buffer, ref offset);

            // Prepare memory stream
            if (_sharedMemoryStream == null)
            {
                _sharedMemoryStream = new MemoryStream(size);
            }
            else if (_sharedMemoryStream.Capacity < size)
            {
                _sharedMemoryStream.Capacity = size;
            }

            var stream = _sharedMemoryStream;


            if (size < 0)
            {
                throw new InvalidOperationException($"Invalid bitmap size: {size} bytes");
            }
            else if (size == 0)
            {
                img = null;
                return;
            }

            // Copy bitmap data into the stream-buffer
            stream.SetLength(size);
            stream.Position = 0;
            var streamBuffer = stream.GetBuffer();

            SerializerBinary.FastCopy(buffer, (uint)offset, streamBuffer, 0, (uint)size);

            stream.Position = 0;
            img             = new Bitmap(stream);

            offset += size;
        }