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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。