GME  13
XMLBuffer.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/framework/XMLBuffer.hpp>
00027 #include <xercesc/util/XMLString.hpp>
00028 #include <xercesc/util/RuntimeException.hpp>
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  XMLBuffer: Buffer management
00034 // ---------------------------------------------------------------------------
00035 
00036 void XMLBuffer::ensureCapacity(const XMLSize_t extraNeeded)
00037 {    
00038     // If we can't handle it, try doubling the buffer size.
00039     XMLSize_t newCap = (fIndex + extraNeeded) * 2;
00040 
00041     // If a maximum size is set, and double the current buffer size exceeds that
00042     // maximum, first check if the maximum size will accomodate the extra needed.
00043     if (fFullHandler && (newCap > fFullSize))
00044     {
00045         // If the maximum buffer size accomodates the extra needed, resize to
00046         // the maximum
00047         if (fIndex + extraNeeded <= fFullSize) 
00048         {
00049             newCap = fFullSize;
00050         }
00051 
00052         // Otherwise, allow the registered full-handler to try to empty the buffer.
00053         // If it claims success, and we can accommodate the extra needed in the buffer
00054         // to be expanded, resize to the maximum
00055         // Note the order of evaluation: bufferFull() has the intentional side-effect
00056         // of modifying fIndex.
00057         else if (fFullHandler->bufferFull(*this) && (fIndex + extraNeeded <= fFullSize))
00058         {
00059             newCap = fFullSize;
00060         }
00061 
00062         // Finally, if the full-handler failed, or the buffer (of maximum size) 
00063         // still can't accomodate the extra needed, we must fail.
00064         else
00065             ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Array_BadNewSize, fMemoryManager);
00066     }
00067 
00068     // Note the previous if block can modify newCap, so we may not need to allocate
00069     // at all.
00070     if (newCap > fCapacity)
00071     {
00072         // Allocate new buffer
00073         XMLCh* newBuf = (XMLCh*) fMemoryManager->allocate((newCap+1) * sizeof(XMLCh)); //new XMLCh[newCap+1];
00074  
00075         // Copy over the old stuff
00076         memcpy(newBuf, fBuffer, fIndex * sizeof(XMLCh));
00077 
00078         // Clean up old buffer and store new stuff
00079         fMemoryManager->deallocate(fBuffer); //delete [] fBuffer;
00080         fBuffer = newBuf;
00081         fCapacity = newCap;
00082     }
00083 }
00084 
00085 XERCES_CPP_NAMESPACE_END
00086