您的位置:首页 > Web前端

VOOVAN源码bug修复之一TByteBuffer

2017-09-14 11:41 162 查看
源码如下:

/**
* 查找特定 byte 标识的位置
*     byte 标识数组第一个字节的索引位置
* @param byteBuffer Bytebuffer 对象
* @param mark byte 标识数组
* @return 第一个字节的索引位置
*/
public static int indexOf(ByteBuffer byteBuffer, byte[] mark){

if(byteBuffer.remaining() == 0){
return -1;
}

int index = -1;
int position = byteBuffer.position();
byte[] tmp = new byte[mark.length];
for(int offset = 0; (offset + position <= byteBuffer.remaining() - mark.length); offset++){
byteBuffer.position(position + offset);
byteBuffer.get(tmp, 0, tmp.length);
if(Arrays.equals(mark, tmp)){
index = offset;
break;
}
}

byteBuffer.position(position);

return index;
}

测试如下:

private ByteBuffer b ;

public void setUp() throws IOException {
b = ByteBuffer.allocate(10);
b.put("helyho".getBytes());
}

public void testRun() {
b.flip();//b.put("helyho".getBytes());
int a = TByteBuffer.indexOf(b,"ho".getBytes());
System.out.println(a);
}




查找不到自定的字节,定位问题如下:


问题在于Buffer.remaining()方法是动态返回当前位置与限制之间的元素数。

bug修改如下:



打个广告:voovan地址:https://gitee.com/helyho/Voovan
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  VOOVAN