login about faq

I am trying to load a CkXml object from a CkByteData object. However, the only interfaces available for loading from memory are LoadXml and LoadXml2. Both of those only take a char * rather than a CkByteData.

I tried loading with a call to LoadXml(data.getBytes()) as well as LoadXml(data.getData()) and was getting erratic behavior with both. I found out it is because the underlying byte array in CkByteData is not NULL-terminated! There is no way to tell LoadXml how many bytes to read, and there is no way to pass a CkByteData object, and there is no way to even set a NULL terminator on a CkByteData!

The only way I got around this was with a little ugly hack:

CkByteData data;
// load some data (in my case, from a CkZipEntry)

// here is the hack to set a NULL term.  getData() returns a const char *
// that must be cast to a char * so we can modify it.
((char*)data.getData())[data.getSize() - 1] = 0;

CkXml xml;

Even with this hack, I had to be sure that there was enough room to hold the NULL term, so I had to pre-allocate my CkByteData with xml.get_UncompressedLength() + 1.

Is there any other way to get around this? If not, please consider adding a LoadXml interface that accepts a CkByteData object.

asked Mar 18 '15 at 16:28

nsmith22's gravatar image


edited Mar 18 '15 at 16:30

Append a null byte to the CkByteData:


CkXml xml;
xml.LoadXml((const char *)data.getData());

answered Mar 18 '15 at 16:56

chilkat's gravatar image

chilkat ♦♦

That will do it, although I think it is an unobvious way to use LoadXml with a CkByteData object. Also, I still need to pre-allocate my data object to avoid an internal re-alloc during this appendChar() call.

(Mar 19 '15 at 13:49) nsmith22
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: Mar 18 '15 at 16:28

Seen: 1,488 times

Last updated: Mar 19 '15 at 13:49

powered by OSQA