fCraft.BlockDB.TrimFile C# (CSharp) Method

TrimFile() private method

private TrimFile ( int maxCapacity ) : void
maxCapacity int
return void
        private void TrimFile( int maxCapacity )
        {
            if ( maxCapacity == 0 ) {
                using ( File.Create( FileName ) ) { }
                return;
            }
            if ( !File.Exists( FileName ) )
                return;

            string tempFileName = FileName + ".tmp";
            using ( FileStream source = File.OpenRead( FileName ) ) {
                int entries = ( int )( source.Length / sizeof( BlockDBEntry ) );
                if ( entries <= maxCapacity )
                    return;

                // skip beginning of the file (that's where old entries are)
                source.Seek( ( entries - maxCapacity ) * sizeof( BlockDBEntry ), SeekOrigin.Begin );

                // copy end of the existing file to a new one
                using ( FileStream destination = File.Create( tempFileName ) ) {
                    while ( source.Position < source.Length ) {
                        int bytesRead = source.Read( ioBuffer, 0, ioBuffer.Length );
                        destination.Write( ioBuffer, 0, bytesRead );
                    }
                }
            }
            Paths.MoveOrReplace( tempFileName, FileName );
        }