convert byte[] to Byte[]

Discussion in 'Java' started by Shane Wealti, Jun 13, 2005.

  1. Shane Wealti

    Shane Wealti Guest

    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?
    Shane Wealti, Jun 13, 2005
    #1
    1. Advertising

  2. Shane Wealti

    Eric Sosman Guest

    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);
    > }
    >
    > 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.

    --
    Eric Sosman, Jun 13, 2005
    #2
    1. Advertising

  3. Shane Wealti

    Shane Wealti Guest

    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.
    Shane Wealti, Jun 13, 2005
    #3
  4. Shane Wealti

    Shane Wealti Guest

    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);
    Shane Wealti, Jun 13, 2005
    #4
  5. Shane Wealti

    Shane Wealti Guest

    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...
    Shane Wealti, Jun 13, 2005
    #5
  6. Shane Wealti

    Brzezi Guest

    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: ][ 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 ]
    Brzezi, Jun 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas
    Replies:
    1
    Views:
    829
    Jonathan Bromley
    May 4, 2004
  2. Replies:
    20
    Views:
    9,698
    licebmi
    Sep 7, 2009
  3. Replies:
    5
    Views:
    1,909
    Wesley Hall
    Nov 4, 2006
  4. Polaris431
    Replies:
    8
    Views:
    738
    SM Ryan
    Dec 4, 2006
  5. M D

    convert 2 byte space to one byte

    M D, Oct 6, 2006, in forum: Javascript
    Replies:
    3
    Views:
    182
    Dr John Stockton
    Oct 6, 2006
Loading...

Share This Page