?
Solved

Convert Vector to Byte[]

Posted on 2004-03-31
17
Medium Priority
?
2,252 Views
Last Modified: 2008-02-01
How would I convert a Vector to a Byte[].

I can see that Vector implements the Serializable interface, but since a Vector can hold any object, which may or may not be serializable, is a Vector really Serializable?
0
Comment
Question by:Moroni24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
  • +1
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10725946
You're right that its contents have also to be Serializable. What do you want to do? Do you really mean Byte[]?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10725985
If so, you can do this sort of thing:


            Vector v = new Vector();
            for(int i = 0;i < 10;i++) {
                  Byte b = new Byte((byte)i);
                  v.add(b);
            }
            Byte[] bytes = (Byte[])v.toArray(new Byte[10]);
            System.out.println(bytes);
0
 
LVL 2

Author Comment

by:Moroni24
ID: 10726031
What I have is a vector of ByteArrayOutputStreams
  These streams themsevles are compressed jms.TestMessages
Now I want to take the whole thing and convert it to a Byte array so I can assign it to a jms.BytesMessage.

I could also use an jms.ObjectMessagem but then I still have the same question. Is the vector going to be serializable.
0
Industry Leaders: 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 86

Expert Comment

by:CEHJ
ID: 10726073
When you take the wrapped byte arrays out of the streams it'll be Serializable, yes. It's just the streams that aren't
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10726110
Do something like:


            ByteArrayOutputStream master = new ByteArrayOutputStream();
            for(int i = 0;i < streamsVector.size();i++) {
                  ByteArrayOutputStream wrappedBytes = (ByteArrayOutputStream)streamsVector.get(i);
                  master.write(wrappedBytes.toByteArray());
            }
            byte[] allBytes = master.toByteArray();
            // now serialize 'allBytes'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10726123
That's assuming concatenation - you may not want to concatenate
0
 
LVL 2

Author Comment

by:Moroni24
ID: 10726148
So serializing the streams is not possible then. Which means I won't be able to serialize the vector.

Am I understanding you correctly?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10726173
You serialize what's in the streams, i.e. the byte arrays, not the streams themselves. Take my last code, you could then do


         byte[] allBytes = master.toByteArray();
         Vector v = new Vector();
         v.add(allBytes); // now serailize 'v'
0
 
LVL 2

Author Comment

by:Moroni24
ID: 10726663
I didn't think you would be able to add a byte[] to a vector since it's not an object.

I'll give it a shot
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 10727000
It is an object i assure you ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10727098
> What I have is a vector of ByteArrayOutputStreams

Question is if are looking to create a message from your Vector, then why does it contain streams.
Better off instead having it contain the bytes for the message in the first place or converting them:

Vector bytes = new Vector();
for(int i = 0;i < streamsVector.size();i++)
{
      ByteArrayOutputStream byteStream = (ByteArrayOutputStream)streamsVector.get(i);
      bytes.add(byteStream.toByteArray());
}

You can then serialize your vector.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10731526
Vector object = new Vector();

// add few objects to vector

ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(out);
outStream.writeObject(object);

byte[] data = out.toByteArray();


If any of the object that is in the vector is not serialiable, then it will throw the NotSerialableException.


Regards,
Muruga
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10897644
No objections...
0
 
LVL 2

Author Comment

by:Moroni24
ID: 10918454
opps... forgot this was still here... thanks for the reminder
0
 
LVL 92

Expert Comment

by:objects
ID: 10923008
Did my suggestion about how to serialize your Vector help you?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10927747
8-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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