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