crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Readers/Types/Binary/AsyncBinaryFileReader.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 
       
    23 namespace SymbianUtils
       
    24 {
       
    25 	public abstract class AsyncBinaryFileReader : AsyncBinaryReaderBase
       
    26 	{
       
    27 		#region Construct & destruct
       
    28 		public AsyncBinaryFileReader( string aFileName )
       
    29 			: this( aFileName, KDefaultReadChunkSize )
       
    30 		{
       
    31 		}
       
    32 
       
    33 		public AsyncBinaryFileReader( string aFileName, int aReadChunkSize )
       
    34 		{
       
    35 			iSourceFileName = aFileName;
       
    36 			iReadChunkSize = aReadChunkSize;
       
    37 		}
       
    38 		#endregion
       
    39 
       
    40 		#region Constants
       
    41 		public const int KDefaultReadChunkSize = 512;
       
    42 		#endregion
       
    43 
       
    44 		#region Properties
       
    45 		public string FileName
       
    46 		{
       
    47 			get
       
    48 			{
       
    49 				string fileName = string.Empty;
       
    50 				//
       
    51 				lock( this )
       
    52 				{
       
    53 					fileName = iSourceFileName;
       
    54 				}
       
    55 				//
       
    56 				return fileName; 
       
    57 			}
       
    58 		}
       
    59 
       
    60 		protected BinaryReader Reader
       
    61 		{
       
    62 			get { return iReader; }
       
    63 		}
       
    64 		#endregion
       
    65 
       
    66 		#region New API
       
    67 		protected virtual void HandleReaderOpen()
       
    68 		{
       
    69 		}
       
    70 		#endregion
       
    71 
       
    72 		#region From DisposableObject - Cleanup Framework
       
    73 		protected override void CleanupManagedResources()
       
    74 		{
       
    75 			try
       
    76 			{
       
    77 				Cleanup();
       
    78 			}
       
    79 			finally
       
    80 			{
       
    81 				base.CleanupManagedResources();
       
    82 			}
       
    83 		}
       
    84 		#endregion
       
    85 
       
    86 		#region From AsyncReaderBase
       
    87 		protected override void HandleReadStarted()
       
    88 		{
       
    89 			iReader = new BinaryReader( new FileStream( FileName, FileMode.Open, FileAccess.Read ) );
       
    90 			HandleReaderOpen();
       
    91 			//
       
    92 			base.HandleReadStarted();
       
    93 		}
       
    94 
       
    95 		protected override void HandleReadCompleted()
       
    96 		{
       
    97 			try
       
    98 			{
       
    99 				Cleanup();
       
   100 			}
       
   101 			finally
       
   102 			{
       
   103 				base.HandleReadCompleted();
       
   104 			}
       
   105 		}
       
   106 		#endregion
       
   107 
       
   108 		#region Abstract reading framework
       
   109 		protected override byte[] ProvideReadBytes()
       
   110 		{
       
   111 			byte[] ret = iReader.ReadBytes( iReadChunkSize );
       
   112 			return ret;
       
   113 		}
       
   114 
       
   115 		protected override long Size
       
   116 		{
       
   117 			get
       
   118 			{
       
   119 				long size = 0;
       
   120 				//
       
   121 				lock( this )
       
   122 				{
       
   123 					if	( iReader != null && iReader.BaseStream != null )
       
   124 					{
       
   125 						size = iReader.BaseStream.Length;
       
   126 					}
       
   127 				}
       
   128 				//
       
   129 				return size;
       
   130 			}
       
   131 		}
       
   132 
       
   133 		protected override long Position
       
   134 		{
       
   135 			get
       
   136 			{
       
   137 				long position = 0;
       
   138 				//
       
   139 				lock( this )
       
   140 				{
       
   141 					if	( iReader != null && iReader.BaseStream != null )
       
   142 					{
       
   143 						position = iReader.BaseStream.Position;
       
   144 					}
       
   145 				}
       
   146 				//
       
   147 				return position;
       
   148 			}
       
   149 		}
       
   150 		#endregion
       
   151 
       
   152 		#region Internal methods
       
   153 		private void Cleanup()
       
   154 		{
       
   155 			lock( this )
       
   156 			{
       
   157 				if	( iReader != null )
       
   158 				{
       
   159 					iReader.Close();
       
   160 					iReader = null;
       
   161 				}
       
   162 			}
       
   163 		}
       
   164 		#endregion
       
   165 
       
   166 		#region Data members
       
   167 		private BinaryReader iReader;
       
   168 		private readonly int iReadChunkSize;
       
   169 		private readonly string iSourceFileName;
       
   170 		#endregion
       
   171 	}
       
   172 }