java ByteBuffer的使用


1、

 1 package cn.kongxh.nio1;
 2 import java.nio.ByteBuffer ;
 3 public class ByteBufferDemo01{
 4     public static void main(String args[]){
 5         ByteBuffer buf = ByteBuffer.allocateDirect(10) ;    // 准备出10个大小的缓冲区
 6         byte temp[] = {1,3,5,7,9} ;    // 设置内容
 7         buf.put(temp) ;    // 设置一组内容
 8         buf.flip() ;
 9         System.out.print("主缓冲区中的内容:") ;
10         while(buf.hasRemaining()){
11             int x = buf.get() ;
12             System.out.print(x + "、") ;
13         }
14     }
15 }

2、

 1 package cn.kongxh.nio1;
 2 import java.nio.IntBuffer ;
 3 public class IntBufferDemo01{
 4     public static void main(String args[]){
 5         IntBuffer buf = IntBuffer.allocate(10) ;    // 准备出10个大小的缓冲区
 6         System.out.print("1、写入数据之前的position、limit和capacity:") ;
 7         System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity()) ;
 8         int temp[] = {5,7,9} ;// 定义一个int数组
 9         buf.put(3) ;    // 设置一个数据
10         buf.put(temp) ;    // 此时已经存放了四个记录
11         System.out.print("2、写入数据之后的position、limit和capacity:") ;
12         System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity()) ;
13 
14         buf.flip() ;    // 重设缓冲区
15         // postion = 0 ,limit = 原本position
16         System.out.print("3、准备输出数据时的position、limit和capacity:") ;
17         System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity()) ;
18         System.out.print("缓冲区中的内容:") ;
19         while(buf.hasRemaining()){
20             int x = buf.get() ;
21             System.out.print(x + "、") ;
22         }
23     }
24 }

3、

 1 package cn.kongxh.nio1;
 2 import java.nio.IntBuffer ;
 3 public class IntBufferDemo02{
 4     public static void main(String args[]){
 5         IntBuffer buf = IntBuffer.allocate(10) ;    // 准备出10个大小的缓冲区
 6         IntBuffer sub = null ;    // 定义子缓冲区
 7         for(int i=0;i<10;i++){
 8             buf.put(2 * i + 1) ;    // 在主缓冲区中加入10个奇数
 9         }
10         
11         // 需要通过slice() 创建子缓冲区
12         buf.position(2) ;
13         buf.limit(6) ;
14         sub = buf.slice() ;
15         for(int i=0;i<sub.capacity();i++){
16             int temp = sub.get(i) ;
17             sub.put(temp-1) ;
18         }
19 
20         buf.flip() ;    // 重设缓冲区
21         buf.limit(buf.capacity()) ;
22         System.out.print("主缓冲区中的内容:") ;
23         while(buf.hasRemaining()){
24             int x = buf.get() ;
25             System.out.print(x + "、") ;
26         }
27     }
28 }

4、

 1 package cn.kongxh.nio1;
 2 import java.nio.IntBuffer ;
 3 public class IntBufferDemo03{
 4     public static void main(String args[]){
 5         IntBuffer buf = IntBuffer.allocate(10) ;    // 准备出10个大小的缓冲区
 6         IntBuffer read = null ;    // 定义子缓冲区
 7         for(int i=0;i<10;i++){
 8             buf.put(2 * i + 1) ;    // 在主缓冲区中加入10个奇数
 9         }
10         read = buf.asReadOnlyBuffer()  ;// 创建只读缓冲区
11         
12         read.flip() ;    // 重设缓冲区
13         System.out.print("主缓冲区中的内容:") ;
14         while(read.hasRemaining()){
15             int x = read.get() ;
16             System.out.print(x + "、") ;
17         }
18         read.put(30) ;    // 修改,错误
19     }
20 }

总结:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM