为什么它会给我BufferUnderflowException

时间:2016-04-19 00:13:02

标签: java parsing

  

以下是代码:

byte[] topo1={2,1,1,6,6};
byte[] topo2={2,1,1,2,2};
byte[] topo3={2,5,5,4,4};
byte[] topo4={2,3,3,5,5};
byte[] topo5={2,4,4,3,3};
byte[][] topology = {topo1,topo2,topo3,topo4,topo5};
    writeToLog(String.format("%s receives INIT from nse", routerName));
writeToLog(" ");
parseCircuitDB(topology[routerId-1]);
  

这是parseCircuitDB方法,它显示错误:

 private void parseCircuitDB(byte[] string) throws Exception {
ByteBuffer buffer = ByteBuffer.wrap(string);
buffer.order(ByteOrder.LITTLE_ENDIAN);
//gettign the number of neighbor links
nbrLink = buffer.getInt();
System.out.println(nbrLink);
logMsg = String.format("%d neighbor links exist", nbrLink);
writeToLog(logMsg);
for( int i = 1; i <= nbrLink; i++ ) {
    //link id as integer
    int l = buffer.getInt();
   System.out.println(l); 
    //link cost as integer
    int c = buffer.getInt();
    link_cost a = new link_cost(l, c);
    topo_db[routerId].linkCost.put(l, a);
}   
}
  

我收到错误BufferUnderflowException。   我试过检查循环,但我没有看到任何问题。

1 个答案:

答案 0 :(得分:2)

输入参数mAy string的非常糟糕的名称)是一个5字节的数组,例如byte[](十六进制)。

然后使用ByteBuffer字节顺序将其包装在LITTLE_ENDIAN中。

当您再调用getInt()时,它将占用4个字节。引用javadoc:

  

在此缓冲区的当前位置读取下一个四个字节,根据当前字节顺序将它们组成一个int值,然后将位置增加四。

这当然是因为02 01 01 06 06是一个32位整数,需要四个8位字节进行存储。

因此,它将读取int字节02 01 01 06,其中little endian顺序表示06010102(十六进制),即100729090(十进制)。 您的println(nbrLink)应该已经告诉过您了。

然后进入循环并再次调用getInt(),尝试读取另外4个字节,但只剩下1个,因此它会抛出BufferUnderflowException

相关问题