18

In some code I'm creating a List of Bytes, and want to insert an array of bytes into the list as I am building it. What is the cleanest way of doing this? See code below - thanks.

public class ListInsert {
    public static byte[] getData() {
        return new byte[]{0x01, 0x02, 0x03};
    }

    public static void main(String[] args) {
        final List<Byte> list = new ArrayList<Byte>();
        list.add((byte)0xaa);
        list.add(getData()); // I want to insert an array of bytes into the list here
        list.add((byte)0x55);
    }
}
3
  • 2
    Good answers below, but I'm intrigued what you're going to do with your list of bytes... Commented Aug 20, 2010 at 16:29
  • It's for a device I'm working with, need to send certain message packets out the serial port. Commented Aug 20, 2010 at 19:00
  • 1
    Using a List<Byte> is a really inefficient way to deal with serial coms. You should write your own list-like wrapper around a byte array, or use byte buffers from java.nio. Commented Aug 21, 2010 at 3:13

4 Answers 4

27

IF you have a Byte[] arr -- an array of reference types -- you can use Arrays.asList(arr) to get a List<Byte>.

IF you have a byte[] arr -- an array of primitives -- you can't use Arrays.asList(arr) to get a List<Byte>. Instead you'll get a one-element List<byte[]>.

That is, while a byte can be boxed to a Byte, a byte[] DOES NOT get autoboxed to Byte[]!
(also true for other primitives)

So you have two choices:

  • Just iterate over each byte in byte[] and add individually
  • Use libraries
    • With Apache Commons Lang, you can convert byte[] to Byte[]
      • You can then Arrays.asList and addAll
    • With Guava can convert byte[] immediatelly to List<Byte>

The first option looks like this:

byte[] arr = ...;
for (byte b : arr) {
    list.add(b);
}

The second option with Guava looks like this:

// requires Guava
byte[] arr = ...;
list.addAll(Bytes.asList(arr));

This uses Bytes.asList from package com.google.common.primitives. The package has other conversion utilities for other primitives too. The entire library is highly useful.

With Apache Commons Lang, you can use Byte[] toObject(byte[]) from ArrayUtils:

// requires Apache Commons Lang
byte[] arr = ...;
list.addAll(Arrays.asList(ArrayUtils.toObject(arr)));

Related questions

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I had not looked at the google primitives package before, will check it out now.
4

This might not answer your question but it should be a good practice. If you are heavily manipulating an array of bytes, use the ByteBuffer instead. This class have many types of implementation which can give you the best performance & memory usage. One of them is the Direct ByteBuffer which some operations can run natively.

To put a byte or an array of bytes is as simple as eating a candy:

ByteBuffer.put(byte src);
ByteBuffer.put(byte[] src);
ByteBuffer.put(byte[] src, int offset, int length);

And the best thing is when you trying to get the bytes out: directly, no array copy's needed (you need to check the size though) :)

byte[] data = ByteBuffer.array();

Hope you change your mind :)

Comments

1

There is the Arrays.asList() method which exactly do that:

Arrays.asList(getData());

So in your case :

list.addAll(Arrays.asList(getData()));

2 Comments

This doesn't work with byte[]. Arrays.asList doesn't "work" with array of primitives.
I thought that autoboxing would handle that, but I guess not for arrays then
1

Do you really need List<Byte>? I also thought so, but changed my mind. ByteArrayOutputStream was much better for me.

import java.io.ByteArrayOutputStream;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
// fill in buf or get a single byte b
bo.write(buf, 0, bytesRead);
bo.write(b);
byte[] resultArray = bo.toByteArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.