当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CharBuffer duplicate()用法及代码示例


java.nio.CharBuffer类的duplicate()方法用于创建共享给定缓冲区内容的新char缓冲区。

新缓冲区的内容就是该缓冲区的内容。对该缓冲区内容的更改将在新缓冲区中可见,反之亦然;这两个缓冲区的位置,限制和标记值将是独立的。

新缓冲区的容量,限制,位置和标记值将与此缓冲区相同。当且仅当该缓冲区是直接缓冲区时,新缓冲区才是直接缓冲区;当且仅当该缓冲区是只读缓冲区时,新缓冲区才是只读缓冲区。


用法:

public abstract CharBuffer duplicate()

返回值:此方法返回携带先前char缓冲区内容的新char缓冲区

下面是说明duplicate()方法的示例:

示例1:使用直接字符缓冲区

// Java program to demonstrate 
// duplicate() method 
// Using direct intbuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 10; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Intbuffer 
            // and allocating size capacity 
            CharBuffer cb1 = CharBuffer.allocate(capacity); 
  
            // putting the value in Intbuffer 
            cb1.put('a'); 
            cb1.put(2, 'b'); 
            cb1.rewind(); 
  
            // print the Original CharBuffer 
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb1.array())); 
  
            // Creating a duplicate copy of CharBuffer 
            // using duplicate() method 
            CharBuffer cb2 = cb1.duplicate(); 
  
            // print the duplicate copy of CharBuffer 
            System.out.print("Duplicate CharBuffer: "
                             + Arrays.toString(cb2.array())); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
Original CharBuffer: [a, , b, , , , , , , ]
Duplicate CharBuffer: [a, , b, , , , , , , ]

示例2:使用read-onlyintbuffer

// Java program to demonstrate 
// duplicate() method 
// using read-onlyIntbuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 10; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Intbuffer 
            // and allocating size capacity 
            CharBuffer cb1 = CharBuffer.allocate(capacity); 
  
            // putting the value in Intbuffer 
            cb1.put('a'); 
            cb1.put(2, 'b'); 
            cb1.rewind(); 
  
            // print the Original CharBuffer 
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb1.array())); 
  
            // Creating a read-only copy of CharBuffer 
            // using asReadOnlyBuffer() method 
            CharBuffer readonly = cb1.asReadOnlyBuffer(); 
  
            // print the read-only copy of CharBuffer 
            System.out.print("read-only CharBuffer: "); 
            while (readonly.hasRemaining()) 
                System.out.print(readonly.get() + ", "); 
            System.out.println(""); 
  
            // Rewinding the readonly CharBuffer 
            readonly.rewind(); 
  
            // Creating a duplicate copy of CharBuffer 
            // using duplicate() method 
            CharBuffer cb2 = readonly.duplicate(); 
  
            // print the duplicate copy of CharBuffer 
            System.out.print("duplicate copy of read-only CharBuffer: "); 
  
            while (cb2.hasRemaining()) 
                System.out.print(cb2.get() + ", "); 
            System.out.println(""); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
Original CharBuffer: [a, , b, , , , , , , ]
read-only CharBuffer: a, , b, , , , , , , , 
duplicate copy of read-only CharBuffer: a, , b, , , , , , , , 


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 CharBuffer duplicate() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。