當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ByteBuffer wrap()用法及代碼示例


wrap(byte[] array)

java.nio.ByteBuffer類的wrap()方法用於將字節數組包裝到緩衝區中。新緩衝區將由給定的字節數組支持,即,對緩衝區的修改將導致數組被修改,反之亦然。新緩衝區的容量和限製為array.length,位置為零,標記未定義,字節順序為BIG_ENDIAN。它的支持數組將是給定的數組,其數組偏移量將為零。

用法:

public static ByteBuffer wrap(byte[] array)

參數:此方法采用array,它是將支持此緩衝區的數組作為參數。


返回值:此方法返回新的字節緩衝區。

下麵是說明wrap()方法的示例:

範例1:

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the byte array 
        byte[] bb = { 10, 20, 30 }; 
  
        // print the byte array length 
        System.out.println("Array length:"
                           + bb.length); 
  
        // print the byte array element 
        System.out.println("\nArray element:"
                           + Arrays.toString(bb)); 
  
        // wrap the byte array into floatBuffer 
        // using wrap() method 
        ByteBuffer byteBuffer = ByteBuffer.wrap(bb); 
  
        // Rewind the bytebuffer 
        byteBuffer.rewind(); 
  
        // print the byte buffer 
        System.out.println("\nbyteBuffer:"
                           + Arrays.toString( 
                                 byteBuffer.array())); 
  
        // print the byteBuffer capacity 
        System.out.println("\nbytebuffer capacity:"
                           + byteBuffer.capacity()); 
  
        // print the byteBuffer position 
        System.out.println("\nbytebuffer position:  "
                           + byteBuffer.position()); 
    } 
}
輸出:
Array length:3

Array element:[10, 20, 30]

byteBuffer:[10, 20, 30]

bytebuffer capacity:3

bytebuffer position: 0

wrap(byte[] array, int offset, int length)

新緩衝區將由給定的字節數組支持;也就是說,對緩衝區的修改將導致數組被修改,反之亦然。新緩衝區的容量為array.length,其位置為offset,其限製為offset + length,其標記為undefined,其字節順序為BIG_ENDIAN。它的支持數組將是給定的數組,其數組偏移量將為零。

用法:

public static ByteBuffer 
    wrap(byte[] array, int offset, int length)

參數:此方法采用以下參數:

  • array:將支持新緩衝區的數組。
  • offset:要使用的子數組的偏移量;必須為非負數,且不得大於array.length。新緩衝區的位置將設置為此值。
  • length:要使用的子數組的長度;必須為非負且不大於array.length-offset。新緩衝區的限製將設置為偏移量+長度。

返回值:此方法返回新的字節緩衝區。

異常:此方法拋出IndexOutOfBoundsException(如果offset和length參數的前提不成立)。

下麵是說明wrap()方法的示例:

範例1:

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            // Declare and initialize the byte array 
            byte[] bb = { 10, 20, 30 }; 
  
            // print the byte array length 
            System.out.println("Array length:"
                               + bb.length); 
  
            // print the byte array element 
            System.out.println("\nArray element:"
                               + Arrays.toString(bb)); 
  
            // wrap the byte array into floatBuffer 
            // using wrap() method 
            ByteBuffer byteBuffer 
                = ByteBuffer.wrap(bb, 0, 
                                  bb.length); 
  
            // Rewind the bytebuffer 
            byteBuffer.rewind(); 
  
            // print the byte buffer 
            System.out.println("\nbyteBuffer:"
                               + Arrays.toString( 
                                     byteBuffer.array())); 
  
            // print the byteBuffer capacity 
            System.out.println("\nbytebuffer capacity:"
                               + byteBuffer.capacity()); 
  
            // print the byteBuffer position 
            System.out.println("\nbytebuffer position:  "
                               + byteBuffer.position()); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("\npreconditions on the"
                               + " offset and length parameters"
                               + " do not hold"); 
            System.out.println("Exception throws:  " + e); 
        } 
    } 
}
輸出:
Array length:3

Array element:[10, 20, 30]

byteBuffer:[10, 20, 30]

bytebuffer capacity:3

bytebuffer position: 0

範例2:演示IndexOutOfBoundsException

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            // Declare and initialize the byte array 
            byte[] bb = { 10, 20, 30 }; 
  
            // print the byte array length 
            System.out.println("Array length:"
                               + bb.length); 
  
            // print the byte array element 
            System.out.println("\nArray element:"
                               + Arrays.toString(bb)); 
  
            // wrap the byte array into floatBuffer 
            // using wrap() method 
            ByteBuffer byteBuffer 
                = ByteBuffer.wrap(bb, 1, 
                                  bb.length); 
  
            // Rewind the bytebuffer 
            byteBuffer.rewind(); 
  
            // print the byte buffer 
            System.out.println("\nbyteBuffer:"
                               + Arrays.toString( 
                                     byteBuffer.array())); 
  
            // print the byteBuffer capacity 
            System.out.println("\nbytebuffer capacity:"
                               + byteBuffer.capacity()); 
  
            // print the byteBuffer position 
            System.out.println("\nbytebuffer position:  "
                               + byteBuffer.position()); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("\npreconditions on the"
                               + " offset and length parameters"
                               + " do not hold"); 
            System.out.println("Exception throws:  " + e); 
        } 
    } 
}
輸出:
Array length:3

Array element:[10, 20, 30]

preconditions on the offset and length parameters do not hold
Exception throws: java.lang.IndexOutOfBoundsException


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer wrap() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。