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