xml/xmldomandxpath/src/xmlengineserializer/xmlenggzipoutputstream.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 "xmlenggzipoutputstream.h"
       
    18 #include <ezstream.h>
       
    19 #include <ezcompressor.h>
       
    20 
       
    21 /**
       
    22   * Default input buffers size.
       
    23   */
       
    24 static const TInt 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 static const TInt KOutputSize = 0x1000;
       
    32 
       
    33 
       
    34 CXmlEngGZIPOutputStream* CXmlEngGZIPOutputStream::NewLC(MXmlEngOutputStream& aOutputBuffer )
       
    35     {
       
    36     CXmlEngGZIPOutputStream* gzbos = new (ELeave) CXmlEngGZIPOutputStream( aOutputBuffer );
       
    37     CleanupStack::PushL( gzbos );
       
    38     gzbos->ConstructL();
       
    39     return gzbos;	
       
    40     }
       
    41 
       
    42 CXmlEngGZIPOutputStream* CXmlEngGZIPOutputStream::NewL( MXmlEngOutputStream& aOutputBuffer )
       
    43     {
       
    44     CXmlEngGZIPOutputStream* gzbos = CXmlEngGZIPOutputStream::NewLC( aOutputBuffer );
       
    45     CleanupStack::Pop( gzbos );
       
    46     return gzbos;	
       
    47     }
       
    48 
       
    49 CXmlEngGZIPOutputStream::CXmlEngGZIPOutputStream( MXmlEngOutputStream& aOutputBuffer )
       
    50     {
       
    51     iOutputString = &aOutputBuffer;
       
    52     iKeepGoing = ETrue;
       
    53     }
       
    54 
       
    55 void CXmlEngGZIPOutputStream::ConstructL()
       
    56     {
       
    57     //we can't create compressor here, because we heve no input at this moment
       
    58     iOutputDescriptor.CreateL( KOutputSize );
       
    59     iInputDescriptor.CreateL( KDefaultInputBufferSize );
       
    60     iOldInputDescriptor.CreateL( KDefaultInputBufferSize );
       
    61     }
       
    62 
       
    63 CXmlEngGZIPOutputStream::~CXmlEngGZIPOutputStream()
       
    64     {
       
    65     delete iCompressor;
       
    66     iOutputDescriptor.Close();
       
    67     iInputDescriptor.Close();
       
    68     iOldInputDescriptor.Close();
       
    69     }
       
    70 
       
    71 TInt CXmlEngGZIPOutputStream::Write( const TDesC8& aBuffer )
       
    72 	{
       
    73 	TRAP ( iError, WriteL( aBuffer ) );
       
    74 	if ( iError )
       
    75 	    {
       
    76         return -1;
       
    77 	    }
       
    78 	
       
    79 	return aBuffer.Size();
       
    80 	}
       
    81 
       
    82 void CXmlEngGZIPOutputStream::WriteL( const TDesC8& aBuffer )
       
    83 	{
       
    84 	iNoInputNeeded = ETrue;
       
    85 	if ( !iCompressor )
       
    86 	    {
       
    87 	    const TInt newLength = aBuffer.Size();
       
    88 	    if ( newLength > iInputDescriptor.Size() )
       
    89 	        {
       
    90 	        iInputDescriptor.ReAllocL( newLength );
       
    91 	        }
       
    92 	    iInputDescriptor.Copy( aBuffer );
       
    93 	    iCompressor = CEZCompressor::NewL( *this );
       
    94 	    }
       
    95 	else
       
    96 	    {
       
    97 	    iInputDescriptor.Swap( iOldInputDescriptor );
       
    98 	    const TInt newLength = aBuffer.Length();
       
    99 	    if ( newLength > iInputDescriptor.MaxLength() )
       
   100 	        {
       
   101 	        iInputDescriptor.ReAllocL( newLength );
       
   102 	        }
       
   103 	    iInputDescriptor.Copy( aBuffer );		    
       
   104 	    
       
   105 	    while ( iNoInputNeeded )
       
   106 	        {
       
   107 	        iCompressor->DeflateL();
       
   108 	        }		        
       
   109 	    }       		
       
   110 	}
       
   111 	
       
   112 TInt CXmlEngGZIPOutputStream::Close()
       
   113 	{
       
   114 	iCloseInvoked = ETrue;
       
   115 	TRAP ( iError, CloseL() );
       
   116 	if ( iError )
       
   117 	    {
       
   118         return -1;
       
   119 	    }		
       
   120 	return KErrNone;
       
   121 	}
       
   122 
       
   123 void CXmlEngGZIPOutputStream::CloseL()
       
   124 	{
       
   125 	while ( iKeepGoing )
       
   126 	    {
       
   127 	    iKeepGoing = iCompressor->DeflateL();
       
   128 	    }
       
   129 	}
       
   130 		
       
   131 TInt CXmlEngGZIPOutputStream::CheckError()
       
   132     {
       
   133     return iError;
       
   134     }
       
   135    
       
   136 void CXmlEngGZIPOutputStream::InitializeL( CEZZStream& aZStream )
       
   137    	{
       
   138    	aZStream.SetInput( iInputDescriptor );
       
   139    	aZStream.SetOutput( iOutputDescriptor );
       
   140    	iNoInputNeeded = EFalse;
       
   141    	}
       
   142 
       
   143 void CXmlEngGZIPOutputStream::NeedInputL( CEZZStream& aZStream )
       
   144    	{
       
   145     if ( iCloseInvoked )
       
   146         {
       
   147         aZStream.SetInput( KNullDesC8 );
       
   148         }
       
   149     else        
       
   150         {
       
   151        	aZStream.SetInput( iInputDescriptor );
       
   152        	iNoInputNeeded = EFalse;
       
   153        	}
       
   154    	}
       
   155 
       
   156 void CXmlEngGZIPOutputStream::NeedOutputL( CEZZStream& aZStream )
       
   157    	{
       
   158    	TPtrC8 od = aZStream.OutputDescriptor();
       
   159 	TInt res = iOutputString->Write(od);
       
   160    	if (res < 0)
       
   161    		{
       
   162 		User::Leave(res);
       
   163     	}
       
   164    	aZStream.SetOutput( iOutputDescriptor );
       
   165    	}
       
   166 
       
   167 void CXmlEngGZIPOutputStream::FinalizeL( CEZZStream& aZStream )
       
   168     {
       
   169     TPtrC8 od = aZStream.OutputDescriptor();
       
   170 	TInt res = iOutputString->Write(od);
       
   171    	if (res < 0)
       
   172    		{
       
   173 		User::Leave(res);
       
   174     	}
       
   175    	res = iOutputString->Close();
       
   176    	if (res < 0)
       
   177    		{
       
   178 		User::Leave(res);
       
   179     	}
       
   180    	}
       
   181