xml/xmldomandxpath/src/xmlengineserializer/xmlenggzipfileoutputstream.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // GZIP output stream functions
       
    15 //
       
    16 
       
    17 #include "xmlenggzipfileoutputstream.h"
       
    18 #include <ezstream.h>
       
    19 #include <ezcompressor.h>
       
    20 
       
    21 /**
       
    22  * Default input buffers size.
       
    23  */
       
    24 const TInt CXmlEngGZIPFileOutputStream::KDefaultInputBufferSize = 4096;
       
    25 	    
       
    26 /**
       
    27  * Output buffer size. It determines, how often output data will be written
       
    28  * to disk. (it's written to disk, when buffer is full.
       
    29  *
       
    30  */
       
    31 const TInt CXmlEngGZIPFileOutputStream::KOutputBufferSize = 0x8000;
       
    32 
       
    33 CXmlEngGZIPFileOutputStream* CXmlEngGZIPFileOutputStream::NewLC( RFile& aOutputFile, 
       
    34                                                                 RFs& aRFs )
       
    35     {
       
    36     CXmlEngGZIPFileOutputStream* gzfos = new (ELeave) CXmlEngGZIPFileOutputStream( aOutputFile, 
       
    37 	                                                                                aRFs );
       
    38     CleanupStack::PushL( gzfos );
       
    39     gzfos->ConstructL();
       
    40     return gzfos;	
       
    41     }
       
    42 
       
    43 CXmlEngGZIPFileOutputStream* CXmlEngGZIPFileOutputStream::NewL( RFile& aOutputFile, 
       
    44                                                                 RFs& aRFs )
       
    45     {
       
    46     CXmlEngGZIPFileOutputStream* gzfos = CXmlEngGZIPFileOutputStream::NewLC( aOutputFile, 
       
    47 	                                                                                aRFs );
       
    48     CleanupStack::Pop( gzfos );
       
    49     return gzfos;	
       
    50     }
       
    51 
       
    52 CXmlEngGZIPFileOutputStream::CXmlEngGZIPFileOutputStream( RFile& aOutputFile, RFs& aRFs )
       
    53     {
       
    54     iKeepGoing = ETrue;
       
    55     iOutputFile = aOutputFile;
       
    56     iRFs = aRFs;
       
    57     iCrc = crc32( iCrc, NULL, 0 );
       
    58     }
       
    59 
       
    60 void CXmlEngGZIPFileOutputStream::ConstructL()
       
    61     {
       
    62     EZGZipFile::WriteHeaderL( iOutputFile, iHeader );
       
    63     //we can't create compressor here, because we heve no input at this moment
       
    64     iOutputDescriptor.CreateL( KOutputBufferSize );
       
    65     iInputDescriptor.CreateL( KDefaultInputBufferSize );
       
    66     iOldInputDescriptor.CreateL( KDefaultInputBufferSize );
       
    67     }
       
    68 
       
    69 CXmlEngGZIPFileOutputStream::~CXmlEngGZIPFileOutputStream()
       
    70     {
       
    71     delete iCompressor;
       
    72     iOutputDescriptor.Close();
       
    73     iInputDescriptor.Close();
       
    74     iOldInputDescriptor.Close();
       
    75     }
       
    76 
       
    77 TInt CXmlEngGZIPFileOutputStream::Write( const TDesC8& aBuffer )
       
    78     {
       
    79     TRAP ( iError, WriteL( aBuffer ) );
       
    80     if ( iError )
       
    81         {
       
    82         return KErrNotFound;
       
    83         }
       
    84     
       
    85     TInt bufferSize = aBuffer.Size();
       
    86     iUncompressedDataSize += bufferSize;
       
    87 						
       
    88     return bufferSize;
       
    89     }
       
    90 
       
    91 void CXmlEngGZIPFileOutputStream::WriteL( const TDesC8& aBuffer )
       
    92     {
       
    93     iNoInputNeeded = ETrue;
       
    94     if ( !iCompressor )
       
    95         {
       
    96         const TInt newLength = aBuffer.Length();
       
    97         if ( newLength > iInputDescriptor.MaxLength() )
       
    98             {
       
    99             iInputDescriptor.ReAllocL( newLength );
       
   100             }
       
   101         iInputDescriptor.Copy( aBuffer );
       
   102         iCompressor = CEZCompressor::NewL( *this, CEZCompressor::EDefaultCompression,
       
   103 		                                                             -CEZCompressor::EMaxWBits );    
       
   104         }
       
   105     else
       
   106         {
       
   107         iInputDescriptor.Swap( iOldInputDescriptor );
       
   108         const TInt newLength = aBuffer.Length();
       
   109         if ( newLength > iInputDescriptor.MaxLength() )
       
   110             {
       
   111             iInputDescriptor.ReAllocL( newLength );
       
   112             }
       
   113         iInputDescriptor.Copy( aBuffer );		    
       
   114 		    
       
   115         while ( iNoInputNeeded )
       
   116             {
       
   117             iCompressor->DeflateL();
       
   118             }		        
       
   119         }       		
       
   120     }
       
   121 	
       
   122 TInt CXmlEngGZIPFileOutputStream::Close()
       
   123     {
       
   124     iCloseInvoked = ETrue;
       
   125     if ( iError )
       
   126 		    {
       
   127 	        return -1;
       
   128 		    }
       
   129     TRAP ( iError, CloseL() );
       
   130     if ( iError )
       
   131         {
       
   132         return KErrNotFound;
       
   133         }		
       
   134     return KErrNone;
       
   135     }
       
   136 
       
   137 void CXmlEngGZIPFileOutputStream::CloseL()
       
   138     {
       
   139     while ( iKeepGoing )
       
   140         {
       
   141         iKeepGoing = iCompressor->DeflateL();
       
   142         }
       
   143     TEZGZipTrailer trailer( iCrc, iUncompressedDataSize );
       
   144     EZGZipFile::WriteTrailerL( iOutputFile, trailer );
       
   145     }
       
   146 		
       
   147 TInt CXmlEngGZIPFileOutputStream::CheckError()
       
   148     {
       
   149     return iError;
       
   150     }
       
   151     
       
   152 void CXmlEngGZIPFileOutputStream::InitializeL( CEZZStream& aZStream )
       
   153     {
       
   154     aZStream.SetInput(iInputDescriptor);
       
   155     aZStream.SetOutput(iOutputDescriptor);
       
   156     iCrc = crc32(iCrc,iInputDescriptor.Ptr(),iInputDescriptor.Size());
       
   157     iNoInputNeeded = EFalse;
       
   158     }
       
   159 
       
   160 void CXmlEngGZIPFileOutputStream::NeedInputL( CEZZStream& aZStream )
       
   161     {
       
   162     if ( iCloseInvoked )
       
   163         {
       
   164         aZStream.SetInput( KNullDesC8 );
       
   165         iCrc = crc32( iCrc, (unsigned char*)18, 0 );    
       
   166         //second argument can't be null, and third should be 0 !!!
       
   167         }
       
   168     else        
       
   169         {
       
   170         aZStream.SetInput( iInputDescriptor );
       
   171         iCrc = crc32( iCrc, iInputDescriptor.Ptr(), iInputDescriptor.Size() );
       
   172         iNoInputNeeded = EFalse;
       
   173         }
       
   174     }
       
   175 
       
   176 void CXmlEngGZIPFileOutputStream::NeedOutputL( CEZZStream& aZStream )
       
   177     {
       
   178     TPtrC8 od = aZStream.OutputDescriptor();    	
       
   179     User::LeaveIfError( iOutputFile.Write(od) );
       
   180     aZStream.SetOutput( iOutputDescriptor );
       
   181     }
       
   182 
       
   183 void CXmlEngGZIPFileOutputStream::FinalizeL( CEZZStream& aZStream )
       
   184     {
       
   185     TPtrC8 od = aZStream.OutputDescriptor();    	
       
   186     User::LeaveIfError( iOutputFile.Write(od) );    
       
   187     }
       
   188