Java ByteBuffer wrap()方法及实例

Java ByteBuffer wrap()方法及实例

wrap(byte[] array)

java.nio.ByteBuffer 类的 wrap() 方法用于将一个字节数组包裹成一个缓冲区。新的缓冲区将以给定的字节数组为后盾,也就是说,对缓冲区的修改将导致数组的修改,反之亦然。新的缓冲区的容量和限制将是array.length,它的位置将是0,它的标记将是未定义的,它的字节顺序将是BIG_ENDIAN。它的支持数组将是给定的数组,它的数组偏移将是零。

语法

public static ByteBuffer wrap(byte[] 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,它的标记是未定义的,它的字节顺序是BIG_ENDIAN。它的支持数组将是给定的数组,它的数组偏移将是0。

语法

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

参数: 该方法需要以下参数。

  • array: 将支持新缓冲区的数组。
  • offset: 要使用的子数组的偏移量;必须是非负数,并且不大于array.length。新缓冲区的位置将被设置为这个值。
  • length: 要使用的子数组的长度;必须是非负数,并且不大于array.length – offset。新缓冲区的极限将被设置为offset + length。

返回值: 该方法返回新的字节缓冲区。

异常: 该方法抛出 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程