Beispiel #1
0
        public static void Deserialize(Stream stream, PiaFile piaFile)
        {
            if (stream == null)
                throw new ArgumentNullException("Stream");

            if (piaFile == null)
                throw new ArgumentNullException("PiaFile");

            try
            {
                // Header
                var headerBytes = new Byte[48]; // Ignore 12 byte checksum
                stream.Read(headerBytes, 0, headerBytes.Length);
                var headerString = Encoding.Default.GetString(headerBytes);
                piaFile.Header = new PiaHeader(headerString);

                // Inflation
                string inflatedString;
                stream.Seek(60, SeekOrigin.Begin);
                using (var zStream = new InflaterInputStream(stream))
                {
                    var sr = new StreamReader(zStream, Encoding.Default);
                    inflatedString = sr.ReadToEnd();
                }

                // Nodes
                piaFile.Owner = piaFile;
                _deserializeNode(piaFile, inflatedString);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #2
0
        public static void Serialize(Stream stream, PiaFile piaFile)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("Stream");
            }

            if (piaFile == null)
            {
                throw new ArgumentNullException("PiaFile");
            }

            try
            {
                // Header
                var headerString = piaFile.Header.ToString();
                var headerBytes  = Encoding.Default.GetBytes(headerString);
                stream.Write(headerBytes, 0, headerBytes.Length);

                // Nodes
                var nodeString = _serializeNode(piaFile);
                var nodeBytes  = Encoding.Default.GetBytes(nodeString);

                // Deflation
                byte[] deflatedBytes;
                var    deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
                using (var ms = new MemoryStream())
                {
                    var deflateStream = new DeflaterOutputStream(ms, deflater);
                    deflateStream.Write(nodeBytes, 0, nodeBytes.Length);
                    deflateStream.Finish();

                    deflatedBytes = ms.ToArray();
                }

                // Checksum
                var checkSum = new byte[12];
                BitConverter.GetBytes(deflater.Adler).CopyTo(checkSum, 0);       // Adler
                BitConverter.GetBytes(nodeBytes.Length).CopyTo(checkSum, 4);     // InflatedSize
                BitConverter.GetBytes(deflatedBytes.Length).CopyTo(checkSum, 8); // DeflatedSize
                stream.Write(checkSum, 0, checkSum.Length);

                // Final write
                stream.Write(deflatedBytes, 0, deflatedBytes.Length);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #3
0
        public static void Serialize(Stream stream, PiaFile piaFile)
        {
            if (stream == null)
                throw new ArgumentNullException("Stream");

            if (piaFile == null)
                throw new ArgumentNullException("PiaFile");

            try
            {
                // Header
                var headerString = piaFile.Header.ToString();
                var headerBytes = Encoding.Default.GetBytes(headerString);
                stream.Write(headerBytes, 0, headerBytes.Length);

                // Nodes
                var nodeString = _serializeNode(piaFile);
                var nodeBytes = Encoding.Default.GetBytes(nodeString);

                // Deflation
                byte[] deflatedBytes;
                var deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
                using (var ms = new MemoryStream())
                {
                    var deflateStream = new DeflaterOutputStream(ms, deflater);
                    deflateStream.Write(nodeBytes, 0, nodeBytes.Length);
                    deflateStream.Finish();

                    deflatedBytes = ms.ToArray();
                }

                // Checksum
                var checkSum = new byte[12];
                BitConverter.GetBytes(deflater.Adler).CopyTo(checkSum, 0); // Adler
                BitConverter.GetBytes(nodeBytes.Length).CopyTo(checkSum, 4); // InflatedSize
                BitConverter.GetBytes(deflatedBytes.Length).CopyTo(checkSum, 8); // DeflatedSize
                stream.Write(checkSum, 0, checkSum.Length);

                // Final write
                stream.Write(deflatedBytes, 0, deflatedBytes.Length);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #4
0
        public static void Deserialize(Stream stream, PiaFile piaFile)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("Stream");
            }

            if (piaFile == null)
            {
                throw new ArgumentNullException("PiaFile");
            }

            try
            {
                // Header
                var headerBytes = new Byte[48]; // Ignore 12 byte checksum
                stream.Read(headerBytes, 0, headerBytes.Length);
                var headerString = Encoding.Default.GetString(headerBytes);
                piaFile.Header = new PiaHeader(headerString);

                // Inflation
                string inflatedString;
                stream.Seek(60, SeekOrigin.Begin);

                using (var zStream = new InflaterInputStream(stream))
                {
                    var sr = new StreamReader(zStream, Encoding.Default);
                    inflatedString = sr.ReadToEnd();
                }

                // Nodes
                piaFile.Owner = piaFile;
                _deserializeNode(piaFile, inflatedString);
            }
            catch (Exception)
            {
                throw;
            }
        }