On Mon, 2012-02-13 at 23:57 +0400, Ivan Pechorin wrote: > 2012/2/13 stephenju <step...@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.b...@fusesource.com | www.fusesource.com skype: tabish121 | twitter: @tabish121 blog: http://timbish.blogspot.com/