private void DeserializeContents(Stream serializationStream, Package package, int count)
        {
            var bufferManager = this.BufferManager;

            if (bufferManager == null)
            {
                throw new InvalidOperationException("The value of BufferManager property is null.");
            }

            int headerCount;

            byte[] temp = new byte[4];

            for (int i = 0; i < count; i++)
            {
                PackageContent content = new PackageContent();
                headerCount = serializationStream.ReadByte();
                this.DeserializeHeaders(serializationStream, content.Headers, headerCount);

                if (serializationStream.Read(temp, 0, 4) == 4)
                {
                    int contentLength = BitConverter.ToInt32(temp, 0);
                    int id            = bufferManager.Allocate(contentLength);
                    bufferManager.Write(id, serializationStream, contentLength);
                    content.ContentStream = bufferManager.GetStream(id);
                }

                package.Contents.Add(content);
            }
        }
        private void SerializeContent(Stream serializationStream, PackageContent content)
        {
            var headers = content.Headers;

            if (headers == null || headers.Count == 0)
            {
                serializationStream.WriteByte(0);
            }
            else
            {
                serializationStream.WriteByte((byte)headers.Count);
                this.SerializeHeaders(serializationStream, headers);
            }

            if (content.ContentBuffer != null)
            {
                //写入内容数组的实际长度
                serializationStream.Write(BitConverter.GetBytes(content.ContentLength), 0, 4);
                //将内容数组全部写入序列化流
                serializationStream.Write(content.ContentBuffer, 0, content.ContentLength);
            }
            else if (content.ContentStream != null)
            {
                var contentStream = content.ContentStream;

                //写入内容流的实际长度
                serializationStream.Write(BitConverter.GetBytes(content.ContentLength), 0, 4);

                byte[] buffer          = new byte[1024];
                int    bytesRead       = 0;
                int    availableLength = content.ContentLength;

                while (availableLength > 0 && (bytesRead = contentStream.Read(buffer, 0, Math.Min(buffer.Length, availableLength))) > 0)
                {
                    serializationStream.Write(buffer, 0, bytesRead);
                    availableLength -= bytesRead;
                }
            }
        }