Example #1
0
        public override EsfNode CreateCopy()
        {
            RecordNode recordNode = new RecordNode(base.Codec, base.OriginalTypeCode);

            CopyMembers(recordNode);
            return(recordNode);
        }
        public override EsfNode CreateCopy()
        {
            RecordNode node = new RecordNode(Codec, OriginalTypeCode);

            CopyMembers(node);
            return(node);
        }
Example #3
0
        public virtual RecordNode ReadRecordNode(BinaryReader reader, byte typeCode, bool forceDecode = false)
        {
            RecordNode recordNode = (forceDecode || buffer == null)
                ? new RecordNode(this, typeCode)
                : new MemoryMappedRecordNode(this, buffer, (int)reader.BaseStream.Position);

            recordNode.Decode(reader, EsfType.RECORD);
            return(recordNode);
        }
        //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);
        }
Example #5
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);
        }
Example #6
0
//        protected byte[] EncodeArrayNode<T>(T[] array, ValueWriter<T> WriteItem) {
//            byte[] result;
//            MemoryStream bufferStream = new MemoryStream();
//            using (BinaryWriter writer = new BinaryWriter(bufferStream)) {
//                for (int i = 0; i < array.Length; i++) {
//                    WriteItem(writer, array[i]);
//                }
//                result = bufferStream.ToArray();
//            }
//            return result;
//        }
        #endregion

        #region Record Nodes
        // read an identified node from the reader at its current position
        public virtual RecordNode ReadRecordNode(BinaryReader reader, byte typeCode, bool forceDecode = false)
        {
            RecordNode node;

            if (!forceDecode && buffer != null)
            {
                node = new MemoryMappedRecordNode(this, buffer, (int)reader.BaseStream.Position);
            }
            else
            {
                node = new RecordNode(this, typeCode);
            }
            node.Decode(reader, EsfType.RECORD);
            return(node);
        }
Example #7
0
        public override RecordNode ReadRecordNode(BinaryReader reader, byte typeCode, bool forceDecode = false)
        {
            RecordNode recordNode = base.ReadRecordNode(reader, typeCode, forceDecode);

            if (forceDecode && recordNode.Name == CompressedNode.TAG_NAME)
            {
                recordNode = new CompressedNode(this, recordNode);
            }

            if (recordNode is MemoryMappedRecordNode)
            {
                (recordNode as MemoryMappedRecordNode).InvalidateSiblings = false;
            }

            return(recordNode);
        }
Example #8
0
        // Section can now be compressed
        public override RecordNode ReadRecordNode(BinaryReader reader, byte typeCode, bool forceDecode = false)
        {
            RecordNode node = base.ReadRecordNode(reader, typeCode, forceDecode) as RecordNode;

            if (forceDecode && node.Name == CompressedNode.TAG_NAME)
            {
                // decompress node
                // Console.WriteLine("reading compressed node");
                node = new CompressedNode(this, node);
            }
            if (node is MemoryMappedRecordNode)
            {
                // we don't need to invalidate following sections because
                // all the sizes are relative so we won't need to adjust them
                (node as MemoryMappedRecordNode).InvalidateSiblings = false;
            }
            return(node);
        }
Example #9
0
        protected virtual void WriteRecordNode(BinaryWriter writer, EsfNode node)
        {
            RecordNode recordNode = node as RecordNode;

            recordNode.Encode(writer);
        }
Example #10
0
 public CompressedNode(EsfCodec codec, RecordNode rootNode)
     : base(codec, 0)
 {
     base.Name      = TAG_NAME;
     compressedNode = rootNode;
 }