static ArraySegment <byte> SerializeCore <T>(T obj, IFormatterResolver resolver)
        {
            var serializedData = MessagePackSerializer.SerializeUnsafe(obj, resolver);

            if (serializedData.Count < NotCompressionSize)
            {
                return(serializedData);
            }
            else
            {
                var offset      = 0;
                var buffer      = LZ4MemoryPool.GetBuffer();
                var maxOutCount = LZ4Codec.MaximumOutputLength(serializedData.Count);
                if (buffer.Length + 6 + 5 < maxOutCount) // (ext header size + fixed length size)
                {
                    buffer = new byte[6 + 5 + maxOutCount];
                }

                // acquire ext header position
                var extHeaderOffset = offset;
                offset += (6 + 5);

                // write body
                var lz4Length = LZ4Codec.Encode(serializedData.Array, serializedData.Offset, serializedData.Count, buffer, offset, buffer.Length - offset);

                // write extension header(always 6 bytes)
                extHeaderOffset += MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref buffer, extHeaderOffset, (sbyte)ExtensionTypeCode, lz4Length + 5);

                // write length(always 5 bytes)
                MessagePackBinary.WriteInt32ForceInt32Block(ref buffer, extHeaderOffset, serializedData.Count);

                return(new ArraySegment <byte>(buffer, 0, 6 + 5 + lz4Length));
            }
        }
Example #2
0
        public static void Serialize <T>(IArrayBufferWriter <byte> output, T obj, IFormatterResolver resolver)
        {
            using (var serializedData = MessagePackSerializer.SerializeUnsafe(obj, resolver))
            {
                var serializedDataLen = serializedData.Count;
                if ((uint)serializedDataLen < NotCompressionSize)
                {
                    // can't write direct, shoganai...
                    serializedData.Span.CopyTo(output.GetSpan(serializedDataLen));
                    output.Advance(serializedDataLen);
                    return;
                }

#if NET451
                var maxOutCount  = LZ4Codec.MaximumOutputLength(serializedDataLen);
                var outputBuffer = output.GetBuffer(c_lz4PackageHeaderSize + maxOutCount);
                // write body
                var serializedBuffer = serializedData.Buffer;
                var lz4Length        = LZ4Codec.Encode(serializedBuffer.Array, serializedBuffer.Offset, serializedBuffer.Count, outputBuffer.Array, outputBuffer.Offset + c_lz4PackageHeaderSize, outputBuffer.Count - c_lz4PackageHeaderSize);
#else
                var maxOutCount = LZ4Codec.MaximumOutputSize(serializedDataLen);
                var outputSpan  = output.GetSpan(c_lz4PackageHeaderSize + maxOutCount);
                // write body
                var lz4Length = LZ4Codec.Encode(serializedData.Span, outputSpan.Slice(c_lz4PackageHeaderSize));
#endif

                ref byte addr = ref output.Origin;
                MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref addr, 0, ExtensionTypeCode, lz4Length + 5);
                MessagePackBinary.WriteInt32ForceInt32Block(ref addr, 6, serializedDataLen);
                output.Advance(lz4Length + c_lz4PackageHeaderSize);
            }
        public static int SerializeToBlock <T>(ref byte[] bytes, int offset, T obj, IFormatterResolver resolver)
        {
            var serializedData = MessagePackSerializer.SerializeUnsafe(obj, resolver);

            if (serializedData.Count < NotCompressionSize)
            {
                // can't write direct, shoganai...
                MessagePackBinary.EnsureCapacity(ref bytes, offset, serializedData.Count);
                Buffer.BlockCopy(serializedData.Array, serializedData.Offset, bytes, offset, serializedData.Count);
                return(serializedData.Count);
            }
            else
            {
                var maxOutCount = LZ4Codec.MaximumOutputLength(serializedData.Count);

                MessagePackBinary.EnsureCapacity(ref bytes, offset, 6 + 5 + maxOutCount); // (ext header size + fixed length size)

                // acquire ext header position
                var extHeaderOffset = offset;
                offset += (6 + 5);

                // write body
                var lz4Length = LZ4Codec.Encode(serializedData.Array, serializedData.Offset, serializedData.Count, bytes, offset, bytes.Length - offset);

                // write extension header(always 6 bytes)
                extHeaderOffset += MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref bytes, extHeaderOffset, (sbyte)ExtensionTypeCode, lz4Length + 5);

                // write length(always 5 bytes)
                MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, extHeaderOffset, serializedData.Count);

                return(6 + 5 + lz4Length);
            }
        }