当前位置: 代码迷 >> 综合 >> ByteBuffer写入数组BufferUnderflowException异常
  详细解决方案

ByteBuffer写入数组BufferUnderflowException异常

热度:79   发布时间:2024-01-19 01:12:33.0
chf是前文的一个FileChannel

ByteBuffer buf = ByteBuffer.allocate(50);
chf.read(buf);
buf.flip();
byte [] byt = new byte[100];
buf.get(byt);
然后就出来了 :
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.HeapByteBuffer.get(Unknown Source)
at java.nio.ByteBuffer.get(Unknown Source)
at Sample.main(Sample.java:22)

请问这怎么办
更多 0 分享到:
相关主题推荐:  exception  thread  异常
相关帖子推荐:
  • extjs treePanel抛出异常,高手看看。
  • c++调用cplex的问题
  • 谷歌小bug
  • 主线程被卡死,鼠标等无法动作!请教各位哪里出问题了?
  • socket
  • 接收IOS端发送的16进制图片data数据流 没法重新绘制成图片
  • 由于 AddressFilter 在 EndpointDispatcher 不匹配,To 为“”的消息无法在接收方处理。
  • try catch 使用问题
对我有用[0]  丢个板砖[0]  引用 |  举报 | 管理
回复次数:5
关注
ldh911
MiceRice
等级:
2
13
#1 得分:0回复于: 2012-01-20 11:42:11
你的ByteBuffer才50,但是你buf.get(byt)这里面的字节数组长度是100,ByteBuffer表示它搞不定了。
CSDN投诉事项说明
对我有用[0]  丢个板砖[0]  引用 |  举报 | 管理
关注
motLovejava
艾姆喔替
等级:
#2 得分:0回复于: 2012-01-20 11:56:34
我知道那个 改过 ByteBuffer 跟 byte的大小 
不管谁大谁小 都出那个异常
如果您对CSDN论坛有意见和建议 请直接在本帖指教
对我有用[0]  丢个板砖[0]  引用 |  举报 | 管理
关注
lost_guy_in_scut
lost_guy_in_scut
等级:
#3 得分:0回复于: 2012-01-20 12:50:57
Java code ?
1
2
3
4
5
ByteBuffer buf = ByteBuffer.allocate( 50 );  //这里要改大
chf.read(buf);       //这句话抛的异常
buf.flip();
byte  [] byt =  new  byte [ 100 ];
buf.get(byt);

参考如下代码
Java code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     public  static  void  main(String[] args)  throws  IOException{
         FileChannel channel =  new  FileOutputStream( "D:/a.txt" ).getChannel();  
         // 字节方式写入  
         channel.write(ByteBuffer.wrap( "hello, NIO world in java!" .getBytes()));  
         channel.close();  
           
         // 根据FileInputStream获得通道FileChannel   
         channel =  new  FileInputStream( "D:/a.txt" ).getChannel();  
         // ByteBuffer分配空间,16个字节  
         // 这里需要知道  byte是1字节, short和char是2字节,int和float是4字节   
         //                          long和double是8字节   1byte=8bit 。  基本只是还是必须记住的。  
         ByteBuffer buff = ByteBuffer.allocate( 16 );  
         // 字节数组数据装入buff,  
         channel.read(buff);  
         // 反转此缓冲区  
         buff.flip(); 
         byte  [] byt =  new  byte [ 10 ];
         System.out.println(buff.get(byt));  // 根据FileOutputStream获得通道FileChannel
     }
对我有用[0]  丢个板砖[0]  引用 |  举报 | 管理
关注
lost_guy_in_scut
lost_guy_in_scut
等级:
#4 得分:0回复于: 2012-01-20 12:52:40
少一段代码,忘记关闭了。
Java code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public  static  void  main(String[] args)  throws  IOException{
         FileChannel channel =  new  FileOutputStream( "D:/a.txt" ).getChannel();  
         // 字节方式写入  
         channel.write(ByteBuffer.wrap( "hello, NIO world in java!" .getBytes()));  
         channel.close();  
           
         // 根据FileInputStream获得通道FileChannel   
         channel =  new  FileInputStream( "D:/a.txt" ).getChannel();  
         // ByteBuffer分配空间,16个字节  
         // 这里需要知道  byte是1字节, short和char是2字节,int和float是4字节   
         //                          long和double是8字节   1byte=8bit 。  基本只是还是必须记住的。  
         ByteBuffer buff = ByteBuffer.allocate( 16 );  
         // 字节数组数据装入buff,  
         channel.read(buff);  
         // 反转此缓冲区  
         buff.flip(); 
         byte  [] byt =  new  byte [ 10 ];
       System.out.println(buff.get(byt));        
       channel.close();
     }