Esempio n. 1
0
        public void Save()
        {
            DebugConsole.ShowInfo("Saving database to file: " + filename);
            try
            {
                IFormatter formatter = new BinaryFormatter();

                this.fileHandle.SetLength(0);
                this.fileHandle.Seek(0, SeekOrigin.Begin);
                this.fileHandle.Write(new byte[] { (byte) 'D', (byte) 'B', (byte) '2' }, 0 , 3);

                using (BZip2OutputStream os = new BZip2OutputStream(this.fileHandle))
                {
                    os.IsStreamOwner = false;

                    formatter.Serialize(os, ThreadDatabase.VERSION);
                    formatter.Serialize(os, this);
                }
            }
            catch (Exception e)
            {
                DebugConsole.ShowError("Exception thrown while saving database: " + e.ToString());
            }
            finally
            {
                this.fileHandle.Flush();
            }
            DebugConsole.ShowInfo("Serialized " + this.ThreadCount + " threads.");
        }
Esempio n. 2
0
        /// <summary>
        /// Compress <paramref name="inStream">input stream</paramref> sending 
        /// result to <paramref name="outStream">output stream</paramref>
        /// </summary>
        /// <param name="inStream">The stream to compress.</param>
        /// <param name="outStream">The stream to write compressed data to.</param>
        /// <param name="blockSize">The block size to use.</param>
        /// <remarks>Both streams are closed on completion</remarks>
        public static void Compress(Stream inStream, Stream outStream, int blockSize)
        {
            if ( inStream == null ) {
                throw new ArgumentNullException("inStream");
            }

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

            using ( inStream ) {
                using (BZip2OutputStream bzos = new BZip2OutputStream(outStream, blockSize)) {
                    int ch = inStream.ReadByte();
                    while (ch != -1) {
                        bzos.WriteByte((byte)ch);
                        ch = inStream.ReadByte();
                    }
                }
            }
        }