In BinaryXMLElementReader, add a DynamicUCharArray to handle partial data
diff --git a/ndn-cpp/c/encoding/BinaryXMLElementReader.c b/ndn-cpp/c/encoding/BinaryXMLElementReader.c
index 9561026..169db20 100644
--- a/ndn-cpp/c/encoding/BinaryXMLElementReader.c
+++ b/ndn-cpp/c/encoding/BinaryXMLElementReader.c
@@ -21,9 +21,20 @@
#if 0 // TODO: implement saving data parts.
this.dataParts.push(data.subarray(0, this.structureDecoder.offset));
var element = DataUtils.concatArrays(this.dataParts);
- this.dataParts = [];
#endif
- (*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset);
+ if (self->usePartialData) {
+ // We have partial data from a previous call, so append this data and point to partialData.
+ if (error = ndn_DynamicUCharArray_set(&self->partialData, data, self->structureDecoder.offset, self->partialDataLength))
+ return error;
+ self->partialDataLength += dataLength;
+
+ (*self->elementListener->onReceivedElement)(self->elementListener, self->partialData.array, self->partialDataLength);
+ // Assume we don't need to use partialData anymore until needed.
+ self->usePartialData = 0;
+ }
+ else
+ // We are not using partialData, so just point to the input data buffer.
+ (*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset);
// Need to read a new object.
data += self->structureDecoder.offset;
@@ -36,12 +47,16 @@
// else loop back to decode.
}
else {
-#if 0 // TODO: implement saving data parts.
- // Save for a later call to concatArrays so that we only copy data once.
- this.dataParts.push(data);
-#else
- return -1; // TODO: implement saving data parts.
-#endif
+ // Save remaining data for a later call.
+ if (!self->usePartialData) {
+ self->usePartialData = 1;
+ self->partialDataLength = 0;
+ }
+
+ if (error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength))
+ return error;
+ self->partialDataLength += dataLength;
+
return 0;
}
}
diff --git a/ndn-cpp/c/encoding/BinaryXMLElementReader.h b/ndn-cpp/c/encoding/BinaryXMLElementReader.h
index 09aeb03..9a80deb 100644
--- a/ndn-cpp/c/encoding/BinaryXMLElementReader.h
+++ b/ndn-cpp/c/encoding/BinaryXMLElementReader.h
@@ -8,6 +8,7 @@
#include "../errors.h"
#include "BinaryXMLStructureDecoder.h"
+#include "../util/DynamicUCharArray.h"
#ifdef __cplusplus
extern "C" {
@@ -41,18 +42,27 @@
struct ndn_BinaryXMLElementReader {
struct ndn_ElementListener *elementListener;
struct ndn_BinaryXMLStructureDecoder structureDecoder;
+ int usePartialData;
+ struct ndn_DynamicUCharArray partialData;
+ unsigned int partialDataLength;
};
/**
- * Initialize an ndn_BinaryXMLElementReader struct with the elementListener.
+ * Initialize an ndn_BinaryXMLElementReader struct with the elementListener and a buffer for saving partial data.
* @param self pointer to the ndn_BinaryXMLElementReader struct
* @param elementListener pointer to the ndn_ElementListener used by ndn_BinaryXMLElementReader_onReceivedData.
+ * @param buffer the allocated buffer. If reallocFunction is null, this should be large enough to save a full element, perhaps 8000 bytes.
+ * @param bufferLength the length of the buffer
+ * @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0.
*/
static inline void ndn_BinaryXMLElementReader_init
- (struct ndn_BinaryXMLElementReader *self, struct ndn_ElementListener *elementListener)
+ (struct ndn_BinaryXMLElementReader *self, struct ndn_ElementListener *elementListener,
+ unsigned char *buffer, unsigned int bufferLength, unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
{
self->elementListener = elementListener;
ndn_BinaryXMLStructureDecoder_init(&self->structureDecoder);
+ self->usePartialData = 0;
+ ndn_DynamicUCharArray_init(&self->partialData, buffer, bufferLength, reallocFunction);
}
/**