65
60
if (fSource == (FileHandle) XERCES_Invalid_File_Handle)
66
61
ThrowXMLwithMemMgr1(IOException, XMLExcepts::File_CouldNotOpenFile, fileName, fMemoryManager);
68
// Buffer is one larger than capacity, to allow for zero term
69
fDataBuf = (XMLByte*) fMemoryManager->allocate
71
(fCapacity+4) * sizeof(XMLByte)
72
);//new XMLByte[fCapacity+4];
74
// Keep it null terminated
75
fDataBuf[0] = XMLByte(0);
63
fDataBuf = (XMLByte*) fMemoryManager->allocate (
64
fCapacity * sizeof(XMLByte));
78
67
LocalFileFormatTarget::~LocalFileFormatTarget()
80
// flush remaining buffer before destroy
71
// flush remaining buffer before destroy
72
XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
84
XMLPlatformUtils::closeFile(fSource, fMemoryManager);
75
XMLPlatformUtils::closeFile(fSource, fMemoryManager);
79
// There is nothing we can do about it here.
86
82
fMemoryManager->deallocate(fDataBuf);//delete [] fDataBuf;
89
85
void LocalFileFormatTarget::flush()
87
XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
94
91
void LocalFileFormatTarget::writeChars(const XMLByte* const toWrite
95
, const XMLSize_t count
96
, XMLFormatter * const )
92
, const XMLSize_t count
93
, XMLFormatter * const)
99
if (insureCapacity(count))
101
memcpy(&fDataBuf[fIndex], toWrite, count * sizeof(XMLByte));
106
//flush whatever we have in the buffer and the incoming byte stream
108
XMLPlatformUtils::writeBufferToFile(fSource, count, toWrite, fMemoryManager);
97
if (count < MAX_BUFFER_SIZE)
99
// If we don't have enough space, see if we can grow the buffer.
101
if (fIndex + count > fCapacity && fCapacity < MAX_BUFFER_SIZE)
102
insureCapacity (count);
104
// If still not enough space, flush the buffer.
106
if (fIndex + count > fCapacity)
108
XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
112
memcpy(&fDataBuf[fIndex], toWrite, count * sizeof(XMLByte));
119
XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
123
XMLPlatformUtils::writeBufferToFile(fSource, count, toWrite, fMemoryManager);
115
void LocalFileFormatTarget::flushBuffer()
117
// Exception thrown in writeBufferToFile, if any, will be propagated to
118
// the XMLFormatter and then to the DOMLSSerializer, which may notify
119
// application through DOMErrorHandler, if any.
120
XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
123
fDataBuf[fIndex + 1] = 0;
124
fDataBuf[fIndex + 2] = 0;
125
fDataBuf[fIndex + 3] = 0;
130
* if the current capacity is not enough, and we can not have
131
* enough memory for the new buffer, we got to notify the caller
134
bool LocalFileFormatTarget::insureCapacity(const XMLSize_t extraNeeded)
136
// If we can handle it, do nothing yet
137
if (fIndex + extraNeeded < fCapacity)
140
// Oops, not enough room. Calc new capacity and allocate new buffer
141
const XMLSize_t newCap = ((fIndex + extraNeeded) * 2);
146
newBuf = (XMLByte*) fMemoryManager->allocate
148
(newCap+4) * sizeof(XMLByte)
149
);//new XMLByte[newCap+4];
151
catch(const OutOfMemoryException&)
130
void LocalFileFormatTarget::insureCapacity(const XMLSize_t extraNeeded)
132
XMLSize_t newCap = fCapacity * 2;
134
while (fIndex + extraNeeded > newCap)
137
XMLByte* newBuf = (XMLByte*) fMemoryManager->allocate (
138
newCap * sizeof(XMLByte));
158
140
// Copy over the old stuff
159
memcpy(newBuf, fDataBuf, fCapacity * sizeof(XMLByte) + 4);
141
memcpy(newBuf, fDataBuf, fIndex * sizeof(XMLByte));
161
143
// Clean up old buffer and store new stuff
162
fMemoryManager->deallocate(fDataBuf); //delete [] fDataBuf;
144
fMemoryManager->deallocate(fDataBuf);
163
145
fDataBuf = newBuf;
164
146
fCapacity = newCap;
166
// flush the buffer too
171
149
XERCES_CPP_NAMESPACE_END