VOOVAN源码bug修复之一TByteBuffer

源码以下:git

/**
 * 查找特定 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/Voovanip

相关文章
相关标签/搜索