[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to convert BytesMessage to byte array and vice versa

Posted on 2007-10-16
9
Medium Priority
?
2,395 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:mmingfeilam
  • 6
  • 3
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20090763
think of it as a stream, so the following would read bytes into the byte array

bytesMessage.readBytes(byteArray);
0
 
LVL 92

Expert Comment

by:objects
ID: 20090770
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
 

Author Comment

by:mmingfeilam
ID: 20091112
what about the other way around?  thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 20091142
bytesMessage.write(bytes);

0
 

Author Comment

by:mmingfeilam
ID: 20091286
that's all you need to do?  no need for while loop?
0
 
LVL 92

Expert Comment

by:objects
ID: 20091946
thats all
0
 
LVL 92

Expert Comment

by:objects
ID: 20092696
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
 

Author Comment

by:mmingfeilam
ID: 20095551
shouldn't it be bytesMessage.readBytes as opposed to bytesMessage.read?
0
 
LVL 92

Accepted Solution

by:
objects earned 750 total points
ID: 20097405
yes sorry, that was a typo
*should also be writeBytes
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 4 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question