how to convert BytesMessage to byte array and vice versa

can someone tell me how to convert BytesMessage to byte array and vice versa?  i tried to use SimpleMessageConverter102, but my class needs to be in the same package or be a subclass to use it's methods (my class is already implementing Thread):

C:\Projects\Leader\prototype\jms\openjms4\TNQueueReceiver.java:137: extractByteArrayFromMessage(javax.jms.BytesMessage) has protected access in org.springframework.jms.support.converter.SimpleMessageConverter102
mmingfeilamAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
yes sorry, that was a typo
*should also be writeBytes
0
 
objectsCommented:
think of it as a stream, so the following would read bytes into the byte array

bytesMessage.readBytes(byteArray);
0
 
objectsCommented:
or if you didn't know the size:


ByteArrayOutputStream out = new ByteArrayOutputStream();
int n = 0;
byte[] buf = new byte[256];
while ((n=bytesMessage.read(buf))>=0) {
   out.write(buf, 0, n);
}

byte[] bytes = out.toByteArray();
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mmingfeilamAuthor Commented:
what about the other way around?  thanks.
0
 
objectsCommented:
bytesMessage.write(bytes);

0
 
mmingfeilamAuthor Commented:
that's all you need to do?  no need for while loop?
0
 
objectsCommented:
thats all
0
 
objectsCommented:
you can get the size of the message using message.getBodyLength() if you need to know how big to create your buffer (instead of using a ByteArrayInputStream)
0
 
mmingfeilamAuthor Commented:
shouldn't it be bytesMessage.readBytes as opposed to bytesMessage.read?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.