crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianImageLib/Common/Streams/SIStream.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.Collections.Generic;
       
    19 using System.Text;
       
    20 using System.IO;
       
    21 using SymbianUtils;
       
    22 using SymbianUtils.Streams;
       
    23 using SymbianImageLib.Common.Header;
       
    24 using SymbianStructuresLib.Compression.Common;
       
    25 
       
    26 namespace SymbianImageLib.Common.Streams
       
    27 {
       
    28     internal class SIStream : DisposableObject
       
    29     {
       
    30         #region Enumerations
       
    31         public enum TOwnershipType
       
    32         {
       
    33             EOwned = 0,
       
    34             EOwnedExternally
       
    35         }
       
    36         #endregion
       
    37 
       
    38         #region Constructors
       
    39         public SIStream()
       
    40             : this( null, TOwnershipType.EOwned )
       
    41         {
       
    42         }
       
    43 
       
    44         public SIStream( Stream aStream )
       
    45             : this( aStream, TOwnershipType.EOwned )
       
    46         {
       
    47         }
       
    48 
       
    49         public SIStream( Stream aStream, TOwnershipType aType )
       
    50         {
       
    51             SwitchStream( aStream, aType );
       
    52         }
       
    53         #endregion
       
    54 
       
    55         #region Framework API
       
    56         public virtual bool InRange( long aPosition )
       
    57         {
       
    58             bool ret = ( aPosition >= 0 && aPosition < this.Length );
       
    59             return ret;
       
    60         }
       
    61 
       
    62         public virtual void Seek( long aPosition, SeekOrigin aOrigin )
       
    63         {
       
    64             iStream.Seek( aPosition, aOrigin );
       
    65         }
       
    66 
       
    67         public virtual int Read( byte[] aBuffer, int aOffset, int aCount )
       
    68         {
       
    69             int ret = iStream.Read( aBuffer, aOffset, aCount );
       
    70             return ret;
       
    71         }
       
    72 
       
    73         public virtual void Write( byte[] aBuffer )
       
    74         {
       
    75             Write( aBuffer, 0, aBuffer.Length );
       
    76         }
       
    77 
       
    78         public virtual void Write( SIStream aFrom, int aCount )
       
    79         {
       
    80             Write( (Stream) aFrom, aCount );
       
    81         }
       
    82 
       
    83         public virtual void Write( Stream aFrom, int aCount )
       
    84         {
       
    85             byte[] temp = new byte[ aCount ];
       
    86             int ret = aFrom.Read( temp, 0, aCount );
       
    87             if ( ret != aCount )
       
    88             {
       
    89                 throw new Exception( "Unable to read required number of bytes from stream" );
       
    90             }
       
    91             Write( temp, 0, aCount );
       
    92         }
       
    93 
       
    94         public virtual void Write( byte[] aBuffer, int aOffset, int aCount )
       
    95         {
       
    96             iStream.Write( aBuffer, aOffset, aCount );
       
    97         }
       
    98         #endregion
       
    99 
       
   100         #region API
       
   101         public void SwitchStream( Stream aStream, TOwnershipType aType )
       
   102         {
       
   103             if ( iStream != null )
       
   104             {
       
   105                 if ( iOwnership == TOwnershipType.EOwned )
       
   106                 {
       
   107                     iStream.Dispose();
       
   108                     iStream = null;
       
   109                     iLength = 0;
       
   110                 }
       
   111             }
       
   112             //
       
   113             iStream = aStream;
       
   114             iOwnership = aType;
       
   115             //
       
   116             if ( iStream != null )
       
   117             {
       
   118                 // Cache this because calling it is very expensive.
       
   119                 iLength = iStream.Length;
       
   120             }
       
   121         }
       
   122 
       
   123         public void Close()
       
   124         {
       
   125             base.Dispose();
       
   126         }
       
   127 
       
   128         public void Seek( long aPosition )
       
   129         {
       
   130             Seek( aPosition, SeekOrigin.Begin );
       
   131         }
       
   132 
       
   133         public SymbianStreamReaderLE CreateReader()
       
   134         {
       
   135             return SymbianStreamReaderLE.New( iStream );
       
   136         }
       
   137 
       
   138         public SymbianStreamReaderLE CreateReader( SymbianStreamReaderLE.TCloseOperation aCloseOperation )
       
   139         {
       
   140             return SymbianStreamReaderLE.New( iStream, aCloseOperation );
       
   141         }
       
   142         #endregion
       
   143 
       
   144         #region Properties
       
   145         public long Length
       
   146         {
       
   147             get { return iLength; }
       
   148         }
       
   149         #endregion
       
   150 
       
   151         #region Operators
       
   152         public static explicit operator Stream( SIStream aStream )
       
   153         {
       
   154             return aStream.InternalStream;
       
   155         }
       
   156         #endregion
       
   157 
       
   158         #region Internal methods
       
   159         protected Stream InternalStream
       
   160         {
       
   161             get { return iStream; }
       
   162         }
       
   163         #endregion
       
   164 
       
   165         #region Data members
       
   166         private Stream iStream = null;
       
   167         private TOwnershipType iOwnership = TOwnershipType.EOwned;
       
   168         private long iLength = 0;
       
   169         #endregion
       
   170     }
       
   171 }