Convert Vector to Byte[]

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?
LVL 2
Moroni24Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
It is an object i assure you ;-)
0
 
CEHJCommented:
You're right that its contents have also to be Serializable. What do you want to do? Do you really mean Byte[]?
0
 
CEHJCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Moroni24Author Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
That's assuming concatenation - you may not want to concatenate
0
 
Moroni24Author Commented:
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
 
CEHJCommented:
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
 
Moroni24Author Commented:
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
 
objectsCommented:
> 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
 
mmuruganandamCommented:
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
 
mmuruganandamCommented:
No objections...
0
 
Moroni24Author Commented:
opps... forgot this was still here... thanks for the reminder
0
 
objectsCommented:
Did my suggestion about how to serialize your Vector help you?
0
 
CEHJCommented:
8-)
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.