convert byte[] to Byte[]

S

Shane Wealti

I have a byte array that I want to convert into a Byte array so that I
can insert the Byte[] object into a Queue collection. I can't find any
information on how to do this and it seems like doing something like

Byte[] BigByteArray = new Byte[bytearray.length];
for(int i = 0; i < bytearray.length; i++)
{
BigByteArray = new Byte(bytearray);
}

is just way too much effort to do something simple like that. How
should I be doing this?

Also, I'd rather just be able to make a Queue like this, but I can't
get it to compile:

Queue<byte[]> receivedPackets = new LinkedList<byte[]>();

Any ideas for what I should do?
 
E

Eric Sosman

Shane said:
I have a byte array that I want to convert into a Byte array so that I
can insert the Byte[] object into a Queue collection. I can't find any
information on how to do this and it seems like doing something like

Byte[] BigByteArray = new Byte[bytearray.length];
for(int i = 0; i < bytearray.length; i++)
{
BigByteArray = new Byte(bytearray);
}

is just way too much effort to do something simple like that. How
should I be doing this?


That's about the best you can do. Tou have an array of
byte and you want an array of references to Byte objects;
the two are not much alike, so *something* needs to happen.
In 1.5 you could use Byte.valueOf(bytearray) instead of
creating all those new Byte objects, but that's really just
an optimization.

Why do you specifically want an array of Byte references,
though? You say you want to "insert the Byte[] object into a
Queue collection," but why don't you just insert the original
byte[] object instead? An array is an object, even if the
elements of the array are primitives.
 
S

Shane Wealti

Wow, thanks for the speedy response. You are right, I really wanted to
insert the byte[] object into the array/queue but for some reason I was
getting compiler errors when trying to instantiate the queues but this
code seems to work:

private Queue sentPackets;
private Queue receivedPackets;

Queue<byte[]> sentPackets = new LinkedList<byte[]>();
Queue<byte[]> receivedPackets = new LinkedList<byte[]>();

I think I was trying to instantiate a Queue (which can't be
instantiated becaue it is abstract). Anyway, I didn't find a single
reference to convert byte[] to Byte[] when I googled it so hopefully
this might help someone else out in the future. Thanks again.
 
S

Shane Wealti

Now I have a new problem.

I'm using the code I posted in the previous post to instantiate the
Queues. I'm trying to add a byte[] object to a queue like this:

// Attempt to add the packet to the received packet queue
offerSuccess = receivedPackets.offer(currentPacket);

Where currentPacket is of type byte[].

When I compile I'm getting this error:
Note: BayController.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

It compiles with that option but gives this warning:

BayController.java:236: warning: [unchecked] unchecked call to offer(E)
as a member of the raw type java.util.Queue offerSuccess =
receivedPackets.offer(currentPacket);
 
S

Shane Wealti

I figured it out

I had the variables declared somewhere else as

Queue receivedPackets;
Queue sentPackets;

when it should have been

Queue<byte[]> receivedPackets;
Queue<byte[]> sentPackets;

I guess that's the whole point of generics...
 
B

Brzezi

pon, 13 cze 2005 o 19:06 GMT, Shane Wealti napisa³(a):
I have a byte array that I want to convert into a Byte array so that I
can insert the Byte[] object into a Queue collection. I can't find any
information on how to do this and it seems like doing something like

http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/ArrayUtils.html

there is method for you

Pozdrawiam
Brzezi
--
[ E-mail: (e-mail address removed) ][ I can write better than anybody who can ]
[ Ekg: #3781111 ][ write faster, and I can write ]
[ LinuxUser: #249916 ][ faster than anybody who can write ]
[ better. ]
[ -- A.J. Liebling ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top