Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > convert byte[] to Byte[]

Reply
Thread Tools

convert byte[] to Byte[]

 
 
Shane Wealti
Guest
Posts: n/a
 
      06-13-2005
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[i]);
}

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?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-13-2005


Shane Wealti wrote:
> 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[i]);
> }
>
> 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[i]) 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Shane Wealti
Guest
Posts: n/a
 
      06-13-2005
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.

 
Reply With Quote
 
Shane Wealti
Guest
Posts: n/a
 
      06-13-2005
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);

 
Reply With Quote
 
Shane Wealti
Guest
Posts: n/a
 
      06-13-2005
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...

 
Reply With Quote
 
Brzezi
Guest
Posts: n/a
 
      06-13-2005
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/la...rrayUtils.html

there is method for you

Pozdrawiam
Brzezi
--
[ E-mail: (E-Mail 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 ]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Qestion about convert Object to byte[] and convert it back davidxiongcn@gmail.com Java 5 11-04-2006 04:11 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments