有个ByteArrayOutputStream可能可以用,不断把byte[]通过write写入缓冲区,最后调用toByteArray获取全部byte[]。
缺点是不能取出指定的字节数。
不过我觉得自己写一个也不麻烦,没有几行代码。
缺点是不能取出指定的字节数。
不过我觉得自己写一个也不麻烦,没有几行代码。
解决方案 »
- 两台android手机,能直接通过ip地址,进行通讯吗?
- DatePickerDialog在2.2和4.1的android版本中怎么执行结果不一样?
- 急急急。想做一个android的crm管理系统,谁能给点建议呢?
- MOTO ME501 刷机问题
- GPS定位的神奇问题
- Android下, 3G模块的驱动 是不是 很难调试?
- android来调用http生成的json数据
- 请教,如何在谷歌上面发布应用?
- android音乐播放器歌曲收藏怎么实现?
- 写了个android键盘,请问怎么实现backspace的功能
- android开发:把一个byte数组转换成wav音频文件,并且播放
- eclipse下调试android程序问题
private byte[] buffer = new byte[4096];
private int head;
private int tail; public void push(byte[] data, int start, int length) {
int free = buffer.length - tail;
if (free < data.length) {
int used = tail - head;
if (head + free >= data.length) {
System.arraycopy(buffer, head, buffer, 0, used);
} else {
int size = buffer.length * 2;
int want = used + data.length;
while (size < want) {
size += size;
}
byte[] block = new byte[size];
System.arraycopy(buffer, head, block, 0, used);
buffer = block;
}
tail = used;
head = 0;
}
System.arraycopy(data, start, buffer, tail, length);
tail += data.length;
}
public void push(byte[] data) {
push(data, 0, data.length);
}
public int pop(byte[] data, int start, int length) {
int used = tail - head;
if (used == 0)
return 0;
int size = used;
if (size > length) {
size = length;
}
System.arraycopy(buffer, head, data, start, size);
head += size;
if (head == tail) {
head = 0;
tail = 0;
}
return size;
}
public int pop(byte[] data) {
return pop(data, 0, data.length);
}
}
private byte[] buffer = new byte[4096];
private int head;
private int tail; public void push(byte[] data, int start, int length) {
int free = buffer.length - tail;
if (free < length) {
int used = tail - head;
if (head + free >= length) {
System.arraycopy(buffer, head, buffer, 0, used);
} else {
int size = buffer.length * 2;
int want = used + length;
while (size < want) {
size += size;
}
byte[] block = new byte[size];
System.arraycopy(buffer, head, block, 0, used);
buffer = block;
}
tail = used;
head = 0;
}
System.arraycopy(data, start, buffer, tail, length);
tail += data.length;
}
public void push(byte[] data) {
push(data, 0, data.length);
}
public int pop(byte[] data, int start, int length) {
int used = tail - head;
if (used == 0)
return 0;
int size = used;
if (size > length) {
size = length;
}
System.arraycopy(buffer, head, data, start, size);
head += size;
if (head == tail) {
head = 0;
tail = 0;
}
return size;
}
public int pop(byte[] data) {
return pop(data, 0, data.length);
}
}
private byte[] buffer = new byte[4096];
private int head;
private int tail; public void push(byte[] data, int start, int length) {
int free = buffer.length - tail;
if (free < length) {
int used = tail - head;
if (head + free >= length) {
System.arraycopy(buffer, head, buffer, 0, used);
} else {
int size = buffer.length * 2;
int want = used + length;
while (size < want) {
size += size;
}
byte[] block = new byte[size];
System.arraycopy(buffer, head, block, 0, used);
buffer = block;
}
tail = used;
head = 0;
}
System.arraycopy(data, start, buffer, tail, length);
tail += length;
}
public void push(byte[] data) {
push(data, 0, data.length);
}
public int pop(byte[] data, int start, int length) {
int used = tail - head;
if (used == 0)
return 0;
int size = used;
if (size > length) {
size = length;
}
System.arraycopy(buffer, head, data, start, size);
head += size;
if (head == tail) {
head = 0;
tail = 0;
}
return size;
}
public int pop(byte[] data) {
return pop(data, 0, data.length);
}
}
无法比较值。有什么好的办法呢? //验证字节头 必须是 55 AA 81 01才为有效返回
mBytesBuffer.pop(bHead, 0, 1);
if(bHead[0] != 0x55)
continue;
mBytesBuffer.pop(bHead, 1, 1);
if(bHead[1] != 0xAA)
continue;
mBytesBuffer.pop(bHead, 2, 1);
if(bHead[1] != 0x81)
continue;
mBytesBuffer.pop(bHead, 3, 1);
if(bHead[1] != 0x01)
continue;
//获取其他数据
mBytesBuffer.pop(bCheck, 4, 1);
mBytesBuffer.pop(bLen, 5, 2);
mBytesBuffer.pop(bCmd, 7, 4);
mBytesBuffer.pop(bDsID, 11, 4);
mBytesBuffer.pop(bDsValueType, 15, 1);
mBytesBuffer.pop(bDsValueLen, 16, 2);
iValueDataLen = bDsValueLen[0]*0x100 + bDsValueLen[1];
bDsValueData = new byte[iValueDataLen];
mBytesBuffer.pop(bDsValueData, 18, iValueDataLen);
无法比较值。有什么好的办法呢? //验证字节头 必须是 55 AA 81 01才为有效返回
mBytesBuffer.pop(bHead, 0, 1);
if(bHead[0] != 0x55)
continue;
mBytesBuffer.pop(bHead, 1, 1);
if(bHead[1] != 0xAA)
continue;
mBytesBuffer.pop(bHead, 2, 1);
if(bHead[1] != 0x81)
continue;
mBytesBuffer.pop(bHead, 3, 1);
if(bHead[1] != 0x01)
continue;
//获取其他数据
mBytesBuffer.pop(bCheck, 4, 1);
mBytesBuffer.pop(bLen, 5, 2);
mBytesBuffer.pop(bCmd, 7, 4);
mBytesBuffer.pop(bDsID, 11, 4);
mBytesBuffer.pop(bDsValueType, 15, 1);
mBytesBuffer.pop(bDsValueLen, 16, 2);
iValueDataLen = bDsValueLen[0]*0x100 + bDsValueLen[1];
bDsValueData = new byte[iValueDataLen];
mBytesBuffer.pop(bDsValueData, 18, iValueDataLen);
java的byte是有符号的(我觉得是很糟糕的设计),所以只好先转成int再比较:if ((bHead[1] & 0xff) != 0x81)
continue;
无法比较值。有什么好的办法呢? //验证字节头 必须是 55 AA 81 01才为有效返回
mBytesBuffer.pop(bHead, 0, 1);
if(bHead[0] != 0x55)
continue;
mBytesBuffer.pop(bHead, 1, 1);
if(bHead[1] != 0xAA)
continue;
mBytesBuffer.pop(bHead, 2, 1);
if(bHead[1] != 0x81)
continue;
mBytesBuffer.pop(bHead, 3, 1);
if(bHead[1] != 0x01)
continue;
//获取其他数据
mBytesBuffer.pop(bCheck, 4, 1);
mBytesBuffer.pop(bLen, 5, 2);
mBytesBuffer.pop(bCmd, 7, 4);
mBytesBuffer.pop(bDsID, 11, 4);
mBytesBuffer.pop(bDsValueType, 15, 1);
mBytesBuffer.pop(bDsValueLen, 16, 2);
iValueDataLen = bDsValueLen[0]*0x100 + bDsValueLen[1];
bDsValueData = new byte[iValueDataLen];
mBytesBuffer.pop(bDsValueData, 18, iValueDataLen);
java的byte是有符号的(我觉得是很糟糕的设计),所以只好先转成int再比较:if ((bHead[1] & 0xff) != 0x81)
continue;
有效,谢了。我继续研究,这个先给分。