XmlDictionaryReader读取固定大小的归零流

时间:2013-12-06 17:00:31

标签: c# serialization xml-serialization datacontractserializer .net-4.5

有人可以给我很好的解释,为什么这会失败?

const int bufferSize = 2 * 1024, testValue = 123456;
var buffer = new byte[bufferSize];

var serializer = new DataContractSerializer(typeof(int));

//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
    serializer.WriteObject(writer, testValue);

//Deserialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
{
    object deserializedValue = serializer.ReadObject(reader); // <--- nope, this throws System.Runtime.Serialization.SerializationException: The input source is not correctly formatted.
    Console.WriteLine(deserializedValue);
}

我正在使用共享内存IPC,部分内容意味着您必须处理固定大小的进程间缓冲区。我正在将对象序列化到缓冲区中,我想要很酷,所以我尝试使用DataContractSerializer +二进制XmlDictionaryWriter组合,这是我所知道的最快的非自定义序列化技术之一。 问题是当反序列化时,似乎XmlDictionaryReader试图将整个内存流视为一个大的xml文档并读取它自己的流末尾/块标记,遇到大量的零并简单地自我掠夺。 BinaryFormatter没有这个问题,因为它逐块读取流。 我不得不想出一个实现自定义流的相当蹩脚的解决方案,它在到达第一个0之后“伪造”流的结尾(假设是XmlDictionaryWriter的eof标记)。

完整演示:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace SerializationTest
{
    public static class Program
    {
        public static void Main()
        {
            const int bufferSize = 2 * 1024, testValue = 123456;
            var buffer = new byte[bufferSize];

            var serializer = new DataContractSerializer(typeof(int));

            //Serialize value
            using (var memStream = new MemoryStream(buffer))
            using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
                serializer.WriteObject(writer, testValue);

            //Deserialize value
            using (var memStream = new MemoryStream(buffer))
            using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
            {
                object deserializedValue = serializer.ReadObject(reader); // <--- nope, this throws System.Runtime.Serialization.SerializationException: The input source is not correctly formatted.
                Console.WriteLine(deserializedValue);
            }

            //Deserialize value via FakeEndStream
            using (var memStream = new FakeEndStream(new MemoryStream(buffer)))
            using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
            {
                object deserializedValue = serializer.ReadObject(reader);
                Console.WriteLine(deserializedValue);
            }
        }

        private sealed class FakeEndStream : Stream
        {
            private readonly Stream _source;
            private bool _endOfStream;

            public FakeEndStream(Stream source)
            {
                _source = source;
            }

            #region The workaround

            public override int Read(byte[] buffer, int offset, int count)
            {
                int i = 0;

                for (int position = offset; i < count; i++, position++)
                {
                    int value = ReadByte();
                    if (value < 0)
                        return i;

                    buffer[position] = (byte)value;
                }

                return i;
            }

            public override int ReadByte()
            {
                if (_endOfStream)
                    return -1;

                int value = _source.ReadByte();

                if (value <= 0)
                    _endOfStream = true;

                return value;
            }

            #endregion

            #region Boilerplate overrides of Stream

            protected override void Dispose(bool disposing)
            {
                _source.Dispose();
            }

            public override void Flush()
            {
                _source.Flush();
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                return _source.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                _source.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                _source.Write(buffer, offset, count);
            }

            public override bool CanRead
            {
                get { return _source.CanRead; }
            }

            public override bool CanSeek
            {
                get { return _source.CanSeek; }
            }

            public override bool CanWrite
            {
                get { return _source.CanWrite; }
            }

            public override long Length
            {
                get { return _source.Length; }
            }

            public override long Position
            {
                get { return _source.Position; }
                set { _source.Position = value; }
            }

            #endregion
        }
    }
}

2 个答案:

答案 0 :(得分:3)

当然,我会帮助你解决这个问题(因为没有其他人可以打扰甚至确认/否认这些症状)。

我发现问题确实是阅读无效数据的读者。但是你可以通过在流的末尾写一个空格来帮助它,如下所示:

//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
{
    serializer.WriteObject(writer, testValue);
    writer.WriteWhitespace(" ");
}

//Deserialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
{
    object deserializedValue = serializer.ReadObject(reader); // \o/
    Console.WriteLine(deserializedValue);
}

无法相信任何人都不喜欢这个问题,因为它可能导致在运行时难以调试异常,因此使用XmlDictionaryWriter序列化对象有点不可靠。

连接报告:https://connect.microsoft.com/VisualStudio/feedback/details/811170/xmlbinaryreader-not-able-to-read-from-fixed-size-buffer

答案 1 :(得分:0)

您需要添加Flush()命令:

//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
{
    serializer.WriteObject(writer, testValue);
    writer.Flush();
}

来源:https://msdn.microsoft.com/ru-ru/library/ms752244(v=vs.110).aspx?f=255&MSPPError=-2147217396