Ejemplo n.º 1
0
        /// <summary>
        /// Load a Cache File from the Disk
        /// </summary>
        /// <param name="flname">the name of the File</param>
        /// <param name="withprogress">true if you want  to set the Progress in the current Wait control</param>
        /// <exception cref="CacheException">Thrown if the File is not readable (ie, wrong Version or Signature)</exception>
        public void Load(string flname, bool withprogress)
        {
            this.filename = flname;
            containers.Clear();

            if (!System.IO.File.Exists(flname))
            {
                return;
            }

            StreamItem si = StreamFactory.UseStream(flname, FileAccess.Read, true);

            try
            {
                BinaryReader reader = new BinaryReader(si.FileStream);

                try
                {
                    ulong sig = reader.ReadUInt64();
                    if (sig != SIGNATURE)
                    {
                        throw new CacheException("Unknown Cache File Signature (" + Helper.HexString(sig) + ")", flname, 0);
                    }

                    version = reader.ReadByte();
                    if (version > VERSION)
                    {
                        throw new CacheException("Unable to read Cache", flname, version);
                    }

                    int count = reader.ReadInt32();
                    if (withprogress)
                    {
                        Wait.MaxProgress = count;
                    }
                    for (int i = 0; i < count; i++)
                    {
                        CacheContainer cc = new CacheContainer(DEFAULT_TYPE);
                        cc.Load(reader);
                        containers.Add(cc);
                        if (withprogress)
                        {
                            Wait.Progress = i;
                        }
                        if (i % 10 == 0)
                        {
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }
                }
                finally
                {
                    reader.Close();
                }
            }
            finally
            {
                si.Close();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Save a Cache File to the Disk
        /// </summary>
        /// <param name="flname">the name of the File</param>
        public void Save(string flname)
        {
            this.filename = flname;
            this.version  = VERSION;

            StreamItem si = StreamFactory.UseStream(flname, FileAccess.Write, true);

            try
            {
                CleanUp();

                si.FileStream.Seek(0, SeekOrigin.Begin);
                si.FileStream.SetLength(0);
                BinaryWriter writer = new BinaryWriter(si.FileStream);

                writer.Write(SIGNATURE);
                writer.Write(version);

                writer.Write((int)containers.Count);
                ArrayList offsets = new ArrayList();
                //prepare the Index
                for (int i = 0; i < containers.Count; i++)
                {
                    offsets.Add(writer.BaseStream.Position);
                    containers[i].Save(writer, -1);
                }

                //write the Data
                for (int i = 0; i < containers.Count; i++)
                {
                    long offset = writer.BaseStream.Position;
                    writer.BaseStream.Seek((long)offsets[i], SeekOrigin.Begin);
                    containers[i].Save(writer, (int)offset);
                }
            }
            finally
            {
                si.Close();
            }
        }