crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Readers/Types/Binary/AsyncBinaryReaderBase.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.IO;
       
    19 using System.Text;
       
    20 using System.Threading;
       
    21 using System.Collections;
       
    22 using SymbianUtils.Tracer;
       
    23 
       
    24 namespace SymbianUtils
       
    25 {
       
    26 	public abstract class AsyncBinaryReaderBase : AsyncReaderBase
       
    27 	{
       
    28 		#region Construct & destruct
       
    29 		protected AsyncBinaryReaderBase()
       
    30             : this( null )
       
    31 		{
       
    32 		}
       
    33         
       
    34         protected AsyncBinaryReaderBase( ITracer aTracer )
       
    35             : base( aTracer )
       
    36         {
       
    37         }
       
    38         #endregion
       
    39 
       
    40 		#region Read handlers
       
    41 		protected virtual bool ContinueProcessing()
       
    42 		{
       
    43 			return false;
       
    44 		}
       
    45 
       
    46 		protected virtual bool ImmediateAbort()
       
    47 		{
       
    48 			return false;
       
    49 		}
       
    50 
       
    51 		protected virtual void HandleReadBytes( byte[] aData )
       
    52 		{
       
    53 		}
       
    54 		#endregion
       
    55 
       
    56 		#region Abstract reading framework
       
    57 		protected abstract byte[] ProvideReadBytes();
       
    58 		#endregion
       
    59 
       
    60 		#region Internal methods
       
    61 		protected override void PerformOperation()
       
    62 		{
       
    63 			bool forcedContinue = false;
       
    64 			bool immediateAbort = false;
       
    65 			byte[] bytes;
       
    66 			//
       
    67 			lock( this )
       
    68 			{
       
    69 				bytes = ProvideReadBytes();
       
    70 				forcedContinue = ContinueProcessing();
       
    71 				immediateAbort = ImmediateAbort();
       
    72 			}
       
    73 
       
    74             System.DateTime tenPercentTime = DateTime.Now;
       
    75 
       
    76 			int oldProgress = 0;
       
    77             int newProgress = 0;
       
    78 			while ( ( bytes != null && bytes.Length > 0 ) || forcedContinue && !immediateAbort )
       
    79 			{
       
    80 				lock( this )
       
    81 				{
       
    82 					HandleReadBytes( bytes );
       
    83 				}
       
    84 				//
       
    85 				lock( this )
       
    86 				{
       
    87 					newProgress = Progress;
       
    88 					if	( newProgress != oldProgress )
       
    89 					{
       
    90 #if DEBUG
       
    91                         if ( newProgress > 0 && ( newProgress % 10 ) == 0 )
       
    92                         {
       
    93                             System.DateTime now = DateTime.Now;
       
    94                             long intermediateTickDuration = ( ( now.Ticks - tenPercentTime.Ticks ) / 100 );
       
    95                             System.Diagnostics.Debug.WriteLine( newProgress.ToString( "d2" ) + " % COMPLETE - " + intermediateTickDuration.ToString( "d12" ) );
       
    96                             tenPercentTime = now;
       
    97                         }
       
    98 #endif
       
    99                         NotifyEvent( TEvent.EReadingProgress );
       
   100                         oldProgress = newProgress;
       
   101 					}
       
   102 					bytes = ProvideReadBytes();
       
   103 				}
       
   104 				//
       
   105 				lock( this )
       
   106 				{
       
   107 					forcedContinue = ContinueProcessing();
       
   108 				}
       
   109 			}
       
   110 
       
   111             System.DateTime endTime = DateTime.Now;
       
   112             long tickDuration = ( ( endTime.Ticks - iOperationStartTime.Ticks ) / 100 );
       
   113             System.Diagnostics.Debug.WriteLine( "BINARY READ COMPLETE - " + tickDuration.ToString( "d12" ) );
       
   114         }
       
   115 
       
   116 		#endregion
       
   117 	}
       
   118 }