//re-compress node
        public override void Encode(BinaryWriter writer)
        {
            // encode the node into bytes
            byte[]       data;
            MemoryStream uncompressedStream = new MemoryStream();

            using (BinaryWriter w = new BinaryWriter(uncompressedStream)) {
                // use the node's own codec or we'll mess up the string lists
                Decoded.Codec.EncodeRootNode(w, Decoded);
                data = uncompressedStream.ToArray();
            }
            uint uncompressedSize = (uint)data.LongLength;

            // compress the encoded data
#if DEBUG
            Console.WriteLine("compressing...");
#endif
            MemoryStream outStream = new MemoryStream();
            LzmaEncoder  encoder   = new LzmaEncoder();
            using (uncompressedStream = new MemoryStream(data)) {
                encoder.Code(uncompressedStream, outStream, data.Length, long.MaxValue, null);
                data = outStream.ToArray();
            }
#if DEBUG
            Console.WriteLine("ok, compression done");
#endif

            // prepare decoding information
            List <EsfNode> infoItems = new List <EsfNode>();
            infoItems.Add(new UIntNode {
                Value = uncompressedSize, TypeCode = EsfType.UINT32, Codec = Codec
            });
            using (MemoryStream propertyStream = new MemoryStream()) {
                encoder.WriteCoderProperties(propertyStream);
                infoItems.Add(new RawDataNode(Codec)
                {
                    Value = propertyStream.ToArray()
                });
            }
            // put together the items expected by the unzipper
            List <EsfNode> dataItems = new List <EsfNode>();
            dataItems.Add(new RawDataNode(Codec)
            {
                Value = data
            });
            dataItems.Add(new RecordNode(Codec)
            {
                Name = CompressedNode.INFO_TAG, Value = infoItems
            });
            RecordNode compressedNode = new RecordNode(Codec)
            {
                Name = CompressedNode.TAG_NAME, Value = dataItems
            };

            // and finally encode
            compressedNode.Encode(writer);
        }
Ejemplo n.º 2
0
        public override void Encode(BinaryWriter writer)
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] array;
            using (BinaryWriter writer2 = new BinaryWriter(memoryStream))
            {
                base.Decoded.Codec.EncodeRootNode(writer2, base.Decoded);
                array = memoryStream.ToArray();
            }

            uint         value         = (uint)array.LongLength;
            MemoryStream memoryStream2 = new MemoryStream();
            Encoder      encoder       = new Encoder();

            using (memoryStream = new MemoryStream(array))
            {
                encoder.Code(memoryStream, memoryStream2, array.Length, long.MaxValue, null);
                array = memoryStream2.ToArray();
            }

            List <EsfNode> list = new List <EsfNode>();

            list.Add(new UIntNode
            {
                Value    = value,
                TypeCode = EsfType.UINT32,
                Codec    = base.Codec
            });
            using (MemoryStream memoryStream4 = new MemoryStream())
            {
                encoder.WriteCoderProperties(memoryStream4);
                list.Add(new RawDataNode(base.Codec)
                {
                    Value = memoryStream4.ToArray()
                });
            }

            List <EsfNode> list2 = new List <EsfNode>();

            list2.Add(new RawDataNode(base.Codec)
            {
                Value = array
            });
            list2.Add(new RecordNode(base.Codec, 0)
            {
                Name  = INFO_TAG,
                Value = list
            });
            RecordNode recordNode = new RecordNode(base.Codec, 0);

            recordNode.Name  = TAG_NAME;
            recordNode.Value = list2;
            recordNode.Encode(writer);
        }
Ejemplo n.º 3
0
        protected virtual void WriteRecordNode(BinaryWriter writer, EsfNode node)
        {
            RecordNode recordNode = node as RecordNode;

            recordNode.Encode(writer);
        }