This commit is contained in:
@@ -31,14 +31,20 @@ public final class ByteArray {
|
||||
content = new byte[Math.max(128, size)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空数据,将count置为0,并不清掉byte[]的内容
|
||||
*/
|
||||
public void clear() {
|
||||
this.count = 0;
|
||||
}
|
||||
|
||||
public int find(byte value) {
|
||||
return find(0, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较内容是否相同
|
||||
*
|
||||
* @param bytes 待比较内容
|
||||
*
|
||||
* @return 是否相同
|
||||
*/
|
||||
public boolean equal(final byte[] bytes) {
|
||||
if (bytes == null || count != bytes.length) return false;
|
||||
for (int i = 0; i < count; i++) {
|
||||
@@ -47,26 +53,58 @@ public final class ByteArray {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断内容是否为空
|
||||
*
|
||||
* @return 是否为空
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字节长度
|
||||
*
|
||||
* @return 长度
|
||||
*/
|
||||
public int size() {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定位置的byte值,须确保0 <= index < size
|
||||
*
|
||||
* @param index 位置
|
||||
*
|
||||
* @return byte值
|
||||
*/
|
||||
public byte get(int index) {
|
||||
return content[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后一个字节值,调用前须保证count大于0
|
||||
*
|
||||
* @return byte值
|
||||
*/
|
||||
public byte getLastByte() {
|
||||
return content[count - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将buf内容覆盖到本对象内容中
|
||||
*
|
||||
* @param buf
|
||||
*/
|
||||
public void copyTo(byte[] buf) {
|
||||
System.arraycopy(this.content, 0, buf, 0, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将array的内容引用复制给本对象
|
||||
*
|
||||
* @param array
|
||||
*/
|
||||
public void directFrom(ByteArray array) {
|
||||
if (array != null) {
|
||||
this.content = array.content;
|
||||
@@ -74,6 +112,11 @@ public final class ByteArray {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将本对象的内容引用复制给array
|
||||
*
|
||||
* @param array
|
||||
*/
|
||||
public void directTo(ByteArray array) {
|
||||
if (array != null) {
|
||||
array.content = this.content;
|
||||
@@ -81,32 +124,92 @@ public final class ByteArray {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接获取全部数据, 实际数据需要根据size长度来截取
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] directBytes() {
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] getBytes() {
|
||||
return Arrays.copyOf(content, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]并清空
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] getBytesAndClear() {
|
||||
byte[] bs = Arrays.copyOf(content, count);
|
||||
clear();
|
||||
return bs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定值第一次出现的位置,没有返回-1
|
||||
*
|
||||
* @param value 查询值
|
||||
*
|
||||
* @return 所在位置
|
||||
*/
|
||||
public int find(byte value) {
|
||||
return find(0, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定的起始位置查询value值出现的位置,没有返回-1
|
||||
*
|
||||
* @param offset 起始位置
|
||||
* @param value 查询值
|
||||
*
|
||||
* @return 所在位置
|
||||
*/
|
||||
public int find(int offset, char value) {
|
||||
return find(offset, (byte) value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定的起始位置查询value值出现的位置,没有返回-1
|
||||
*
|
||||
* @param offset 起始位置
|
||||
* @param value 查询值
|
||||
*
|
||||
* @return 所在位置
|
||||
*/
|
||||
public int find(int offset, byte value) {
|
||||
return find(offset, -1, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定的起始位置和长度查询value值出现的位置,没有返回-1
|
||||
*
|
||||
* @param offset 起始位置
|
||||
* @param limit 长度限制
|
||||
* @param value 查询值
|
||||
*
|
||||
* @return 所在位置
|
||||
*/
|
||||
public int find(int offset, int limit, char value) {
|
||||
return find(offset, limit, (byte) value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定的起始位置和长度查询value值出现的位置,没有返回-1
|
||||
*
|
||||
* @param offset 起始位置
|
||||
* @param limit 长度限制
|
||||
* @param value 查询值
|
||||
*
|
||||
* @return 所在位置
|
||||
*/
|
||||
public int find(int offset, int limit, byte value) {
|
||||
byte[] bytes = this.content;
|
||||
int end = limit > 0 ? limit : count;
|
||||
@@ -116,14 +219,30 @@ public final class ByteArray {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除最后一个字节
|
||||
*/
|
||||
public void removeLastByte() {
|
||||
if (count > 0) count--;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入一个int值
|
||||
*
|
||||
* @param value int值
|
||||
*/
|
||||
public void writeInt(int value) {
|
||||
write((byte) (value >> 24 & 0xFF), (byte) (value >> 16 & 0xFF), (byte) (value >> 8 & 0xFF), (byte) (value & 0xFF));
|
||||
write((byte) (value >> 24 & 0xFF),
|
||||
(byte) (value >> 16 & 0xFF),
|
||||
(byte) (value >> 8 & 0xFF),
|
||||
(byte) (value & 0xFF));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入一个byte值
|
||||
*
|
||||
* @param value byte值
|
||||
*/
|
||||
public void write(byte value) {
|
||||
if (count >= content.length - 1) {
|
||||
byte[] ns = new byte[content.length + 8];
|
||||
@@ -133,6 +252,11 @@ public final class ByteArray {
|
||||
content[count++] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入一组byte值
|
||||
*
|
||||
* @param values 一组byte值
|
||||
*/
|
||||
public void write(byte... values) {
|
||||
if (count >= content.length - values.length) {
|
||||
byte[] ns = new byte[content.length + values.length];
|
||||
@@ -143,6 +267,12 @@ public final class ByteArray {
|
||||
count += values.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入ByteBuffer指定长度的数据
|
||||
*
|
||||
* @param buffer
|
||||
* @param len
|
||||
*/
|
||||
public void write(ByteBuffer buffer, int len) {
|
||||
if (len < 1) return;
|
||||
if (count >= content.length - len) {
|
||||
@@ -159,21 +289,53 @@ public final class ByteArray {
|
||||
return new String(content, 0, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定字符集转成字符串
|
||||
*
|
||||
* @param charset 字符集
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toString(final Charset charset) {
|
||||
return toString(0, count, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定字符集转成字符串并清空数据
|
||||
*
|
||||
* @param charset 字符集
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toStringAndClear(final Charset charset) {
|
||||
String str = toString(0, count, charset);
|
||||
clear();
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的起始位置和长度按指定字符集转成字符串
|
||||
*
|
||||
* @param offset 起始位置
|
||||
* @param len 长度
|
||||
* @param charset 字符集
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toString(final int offset, int len, final Charset charset) {
|
||||
if (charset == null) return new String(Utility.decodeUTF8(content, offset, len));
|
||||
return new String(content, offset, len, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的起始位置和长度按指定字符集并转义后转成字符串
|
||||
*
|
||||
* @param offset 起始位置
|
||||
* @param len 长度
|
||||
* @param charset 字符集
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toDecodeString(final int offset, int len, final Charset charset) {
|
||||
int index = offset;
|
||||
for (int i = offset; i < (offset + len); i++) {
|
||||
|
||||
Reference in New Issue
Block a user