BytesBuilder工具

一个byte数组构建工具,支持Hex,int,byte,byte[],不依赖其他仓库,拷贝到本地即可使用。
其中ByteUtil在这:处理Byte字节的工具—ByteUtil

简单使用:

BytesBuilder builder = new BytesBuilder();
builder.append(0xff, 1); //添加byte类型,占1个长度
builder.append(data.getLength(), 2);//添加int类型,添加的数据需要占2个长度
builder.append(data.getContent());//添加byte数组类型
builder0.toArray();//转成byte[]类型。

工具类:

import java.io.Serializable;
import java.util.Arrays;

public class BytesBuilder implements Serializable {
	private static final long serialVersionUID = 1298478082864104805L;

	private byte[] bytes;

	private int capacity;
	private int count;

	public BytesBuilder() {
		this(64);
	}

	public BytesBuilder(int capacity) {
		this.bytes = new byte[capacity];
		this.capacity = capacity;
		this.count = 0;
	}

	public BytesBuilder append(byte[] bs) {
		if (bs == null || bs.length == 0) return this;

		// 扩容byte数组
		int len = bs.length;
		if (count + len > bytes.length) {
			int len2 = bytes.length + capacity + len;
			byte[] bs2 = Arrays.copyOf(bytes, len2);

			this.bytes = bs2;
		}

		// 将本次数据给存放进来
		System.arraycopy(bs, 0, bytes, count, len);

		this.count += bs.length;

		return this;
	}

	public BytesBuilder appendLE(byte[] bs) {
		return append(invert(bs));
	}

	public BytesBuilder append(String hex) {
		return append(ByteUtil.toByte(hex));
	}

	public BytesBuilder appendLE(String hex) {
		return appendLE(ByteUtil.toByte(hex));
	}

	public BytesBuilder append(String hex, int bs) {
		return append(ByteUtil.toByte(hex, bs));
	}

	public BytesBuilder appendLE(String hex, int bs) {
		return appendLE(ByteUtil.toByte(hex, bs));
	}

	public BytesBuilder append(int i, int bs) {
		return append(ByteUtil.toByte(i, bs));
	}

	public BytesBuilder appendLE(int i, int bs) {
		return appendLE(ByteUtil.toByte(i, bs));
	}

	public BytesBuilder append(byte b1) {
		return append(new byte[] { b1 });
	}

	public BytesBuilder appendLE(byte b1) {
		return appendLE(new byte[] { b1 });
	}

	public BytesBuilder append(byte b1, byte b2) {
		return append(new byte[] { b1, b2 });
	}

	public BytesBuilder appendLE(byte b1, byte b2) {
		return appendLE(new byte[] { b1, b2 });
	}

	public BytesBuilder append(byte b1, byte b2, byte b3) {
		return append(new byte[] { b1, b2, b3 });
	}

	public BytesBuilder appendLE(byte b1, byte b2, byte b3) {
		return appendLE(new byte[] { b1, b2, b3 });
	}

	public BytesBuilder append(byte b1, byte b2, byte b3, byte b4) {
		return append(new byte[] { b1, b2, b3, b4 });
	}

	public BytesBuilder appendLE(byte b1, byte b2, byte b3, byte b4) {
		return appendLE(new byte[] { b1, b2, b3, b4 });
	}

	public byte[] sub(int start, int end) {
		return Arrays.copyOfRange(bytes, start, end);
	}

	public int length() {
		return count;
	}

	public byte[] toArray() {
		return Arrays.copyOfRange(bytes, 0, count);
	}

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

	private static byte[] invert(byte[] bs) {
		if (bs == null || bs.length == 0) return bs;

		int length = bs.length;
		byte[] answer = new byte[length];
		for (int i = 0; i < answer.length; i++) {
			answer[i] = bs[length - i - 1];
		}

		return answer;
	}

}

为了向别人、向世界证明自己而努力拼搏,而一旦你真的取得了成绩,才会明白:人无须向别人证明什么,只要你能超越自己。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值