activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ivan Pechorin <ivan.pecho...@gmail.com>
Subject Re: ActiveMQ CPP: Getting text from a Text Message without making temporary
Date Mon, 13 Feb 2012 19:57:37 GMT
2012/2/13 stephenju <stephen@ju-ju.com>

> Is there a way to access the text bytes without creating a temporary
> std::string? I am dealing with some large text messages and try not to
> allocate memory when making copies. For example, when I get the message:
>
> std::string reply = myMessage->getText();
>
> getText() makes a copy of its data on return. And the assignment operator
> copies it again into my variable. If I can pass my std::string variable to
> the message and have it fill it out for me, or I can get the constant
> reference to the string data in the message, it will save considerable time
> and memory.
>
> There are also times that I don't need to make a copy of the text. Like
> when
> parsing the text as XML or JSON, the data is no longer needed after the
> parsed structure is created.
>
>
Technically, you can get text bytes without copying using something like
this:

using namespace cms;
using namespace activemq::commands;
...
const TextMessage* textMsg = ...
const ActiveMQTextMessage* msg =
dynamic_cast<ActiveMQTextMessage*>( textMsg );
assert(msg != NULL);

const std::vector<unsigned char>& data( msg->getContent() );
assert(data.size() >= 4);

unsigned int data_len = (data[0] << 24 | data[1] << 16 | data[2] <<  8 |
data[3] <<  0);
const char* data_ptr = (const char*) &(data[4]);

It's not pretty,

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message