activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Timothy Bish <tabish...@gmail.com>
Subject Re: ActiveMQ CPP: Getting text from a Text Message without making temporary
Date Mon, 13 Feb 2012 20:31:55 GMT
On Mon, 2012-02-13 at 23:57 +0400, Ivan Pechorin wrote: 
> 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,

Be careful though because if the message payload is compressed you won't
get a valid string, also the payload will be in modified UTF-8 format so
it could be different then what you'd expect also. 


-- 
Tim Bish
Sr Software Engineer | FuseSource Corp
tim.bish@fusesource.com | www.fusesource.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Mime
View raw message