You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by am...@apache.org on 2013/11/06 11:14:34 UTC

svn commit: r1539293 - /xerces/c/trunk/src/xercesc/framework/LocalFileFormatTarget.cpp

Author: amassari
Date: Wed Nov  6 10:14:34 2013
New Revision: 1539293

URL: http://svn.apache.org/r1539293
Log:
Ensure that the file handle is closed even if the last flush failed (XERCESC-2024)

Modified:
    xerces/c/trunk/src/xercesc/framework/LocalFileFormatTarget.cpp

Modified: xerces/c/trunk/src/xercesc/framework/LocalFileFormatTarget.cpp
URL: http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/framework/LocalFileFormatTarget.cpp?rev=1539293&r1=1539292&r2=1539293&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/framework/LocalFileFormatTarget.cpp (original)
+++ xerces/c/trunk/src/xercesc/framework/LocalFileFormatTarget.cpp Wed Nov  6 10:14:34 2013
@@ -66,17 +66,18 @@ LocalFileFormatTarget::LocalFileFormatTa
 
 LocalFileFormatTarget::~LocalFileFormatTarget()
 {
-    try
+    if (fSource && fSource != (FileHandle) XERCES_Invalid_File_Handle)
     {
-        // flush remaining buffer before destroy
-        XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
-
-        if (fSource)
-          XMLPlatformUtils::closeFile(fSource, fMemoryManager);
-    }
-    catch (...)
-    {
-      // There is nothing we can do about it here.
+        try
+        {
+            // flush remaining buffer before destroy
+            flush();
+        }
+        catch (...)
+        {
+            // There is nothing we can do about it here.
+        }
+        XMLPlatformUtils::closeFile(fSource, fMemoryManager);
     }
 
     fMemoryManager->deallocate(fDataBuf);//delete [] fDataBuf;
@@ -84,47 +85,42 @@ LocalFileFormatTarget::~LocalFileFormatT
 
 void LocalFileFormatTarget::flush()
 {
-  XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
-  fIndex = 0;
+    XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
+    fIndex = 0;
 }
 
 void LocalFileFormatTarget::writeChars(const XMLByte* const toWrite
                                      , const XMLSize_t count
                                      , XMLFormatter * const)
 {
-    if (count)
+    if (count == 0)
+        return;
+    if (count < MAX_BUFFER_SIZE)
     {
-      if (count < MAX_BUFFER_SIZE)
-      {
         // If we don't have enough space, see if we can grow the buffer.
         //
         if (fIndex + count > fCapacity && fCapacity < MAX_BUFFER_SIZE)
-          ensureCapacity (count);
+            ensureCapacity (count);
 
         // If still not enough space, flush the buffer.
         //
         if (fIndex + count > fCapacity)
-        {
-          XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
-          fIndex = 0;
-        }
+            flush();
 
         memcpy(&fDataBuf[fIndex], toWrite, count * sizeof(XMLByte));
         fIndex += count;
-      }
-      else
-      {
+    }
+    else
+    {
+        // block is too big to cache, flush the current cache...
+        //
         if (fIndex)
-        {
-          XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
-          fIndex = 0;
-        }
+            flush();
 
+        //... then write the data directly to disk
+        //
         XMLPlatformUtils::writeBufferToFile(fSource, count, toWrite, fMemoryManager);
-      }
     }
-
-    return;
 }
 
 void LocalFileFormatTarget::ensureCapacity(const XMLSize_t extraNeeded)



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org