如果使用ArrayList管理byte,在add时只能添加byte,其他类型还需要先进行转化,参照ArrayList实现一个能添加各种数据类型的ByteArrayList,通过ByteArrayList可以快速构建所需结构的byte列表再转化为数组

ArrayList是使用数组保存数据,ArrayList的构造方法除了空构造有一个重载的传入参数的构造方法,这个参数就决定了其中数组初始化时的大小
每次add时,都会通过ensureCapacityInternal方法判断数组长度是否足够,不够时进行扩展

编写自己的ByteArrayList实际上的内容是对数据进行转化的封装,在添加非byte数据类型时,转化为byte数组,并对size进行对应的更新,不过转化时需要注意大小端的问题,利用之前的大小端转化工具类即可.

这里直接上代码:

public class ByteArrayList {

private int size = 0;
private byte[] elementData;

public ByteArrayList(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
}
elementData = new byte[initialCapacity];
}

public ByteArrayList() {
this(10);
}

public ByteArrayList(byte[] initData) {
if ((size = initData.length) != 0) {
elementData = initData;
}
}

public int size() {
return size;
}

public ByteArrayList add(byte element) {
ensureCapacityInternal(size + 1);
elementData[size++] = element;
return this;
}

public ByteArrayList add(int index, byte element) {
if (index < 0)
throw new IllegalArgumentException("Index Error:" + index + ", Size: " + size);

if (index > size) {
ensureCapacityInternal(index + 1);
elementData[index] = element;
size = index + 1;
} else {
ensureCapacityInternal(size + 1);
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}
return this;
}

public ByteArrayList add(byte[] src) {
int numNew = src.length;
ensureCapacityInternal(size + numNew);
System.arraycopy(src, 0, elementData, size, numNew);
size += numNew;
return this;
}

public ByteArrayList add(int index, byte[] src) {
if (index < 0)
throw new IllegalArgumentException("Index Error:" + index + ", Size: " + size);

int numNew = src.length;
if (index > size) {
ensureCapacityInternal(index + numNew);
System.arraycopy(src, 0, elementData, index, numNew);
size = index + src.length;
} else {
ensureCapacityInternal(size + numNew);
System.arraycopy(elementData, index, elementData, index + numNew, size - index);
System.arraycopy(src, 0, elementData, index, numNew);
size += numNew;
}
return this;
}

public ByteArrayList add(short s, boolean isLe) {
return add(isLe ? EndianUtils.toLe(s) : EndianUtils.toBe(s));
}

public ByteArrayList add(short s) {
return add(s, true);
}

public ByteArrayList add(int i, boolean isLe) {
return add(isLe ? EndianUtils.toLe(i) : EndianUtils.toBe(i));
}

public ByteArrayList add(int i) {
return add(i, true);
}

public ByteArrayList add(long l, boolean isLe) {
return add(isLe ? EndianUtils.toLe(l) : EndianUtils.toBe(l));
}

public ByteArrayList add(long l) {
return add(l, true);
}


public ByteArrayList add(String s, Charset charSet) {
if (!TextUtils.isEmpty(s) && charSet != null) {
return add(s.getBytes(charSet));
}
return this;
}

public ByteArrayList add(String s, String charSet) {
if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(charSet)) {
try {
return add(s.getBytes(charSet));
} catch (UnsupportedEncodingException ignored) {
}
}
return this;
}

public ByteArrayList add(String s) {
return add(s, "GBK");
}

public byte get(int index) {
if (index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

return elementData[index];
}

public byte remove(int index) {
if (index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

byte oldValue = elementData[index];

int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
elementData[--size] = 0;

return oldValue;
}

public void clear() {
for (int i = 0; i < size; i++) elementData[i] = 0;
size = 0;
}

/**
* 添加空数据
*/
public void addSize(int addSize) {
ensureCapacityInternal(size + addSize);
size += addSize;
}

/**
* 设置size的大小,若大于当前会扩充空数据 若小于当前会截取
*/
public void setSize(int newSize) {
if (newSize > size) {
ensureCapacityInternal(newSize);
} else {
for (int i = newSize; i < size; i++) {
elementData[i] = 0;
}
}
size = newSize;
}

public byte[] toArray() {
byte[] rst = new byte[size];
System.arraycopy(elementData, 0, rst, 0, size);
return rst;
}

private void ensureCapacityInternal(int minCapacity) {
if (minCapacity - elementData.length > 0) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = Arrays.copyOf(elementData, newCapacity);
}
}

@Override
public String toString() {
return BytesReader.toString(toArray());
}
}