Java ByteBuffer put()方法与实例

Java ByteBuffer put()方法与实例

put(byte b)

java.nio.ByteBuffer 类的 put(byte b) 方法用于将给定的字节写入新创建的字节缓冲区的当前位置,然后递增该位置。

语法:

public abstract ByteBuffer put(byte f)

参数: 该方法以字节值 b 为参数,将其写入字节缓冲区中。

返回值: 该方法返回这个缓冲区,其中插入了字节值。

异常: 该方法会抛出以下异常。

  • BufferOverflowException- 如果这个缓冲区的当前位置不小于它的极限值。
  • ReadOnlyBufferException- 如果这个缓冲区是只读的。

下面是说明put(byte b)方法的例子。

例1 :

// Java program to demonstrate
// put() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer using put() method
            bb.put((byte)10)
                .put((byte)20)
                .put((byte)30)
                .rewind();
 
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

Original ByteBuffer:  [10, 20, 30]

例2: 为了证明BufferOverflowException。

// Java program to demonstrate
// put() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer using put() method
            bb.put((byte)10)
                .put((byte)20)
                .put((byte)30);
 
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
 
            // again putting the value in ByteBuffer
            // using put() method
            System.out.println("\nBuffer position : "
                               + bb.position());
            bb.put((byte)40);
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("buffer's current position "
                               + "is not smaller than its limit");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

Original ByteBuffer:  [10, 20, 30]

Buffer position : 3
buffer's current position is not smaller than its limit
Exception throws : java.nio.BufferOverflowException

实例3: 为了证明ReadOnlyBufferException。

// Java program to demonstrate
// put() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer using put() method
            bb.put((byte)10)
                .put((byte)20)
                .put((byte)30);
 
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
 
            // Creating a read-only copy of ByteBuffer
            // using asReadOnlyBuffer() method
            ByteBuffer bb1 = bb.asReadOnlyBuffer();
 
            System.out.println("\nTrying to put the byte value"
                               + " in read only buffer");
 
            // putting the value in readonly ByteBuffer
            // using put() method
            bb1.put((byte)40);
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("buffer's current position "
                               + "is not smaller than its limit");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

Original ByteBuffer:  [10, 20, 30]

Trying to put the byte value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException

参考资料 :https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#put-byte-

put(int index, byte f)

java.nio.ByteBuffer类put(int index, byte f) 方法是用来将给定的字节写入给定索引的缓冲区中。

语法

public abstract ByteBuffer put(int index, byte f)

参数: 该方法接受以下参数作为参数。

  • index : 将被写入的字节的索引
  • f : 要写入的字节值

返回值 :该方法返回这个缓冲区。

异常: 该方法会抛出以下异常。

  • IndexOutOfBoundsException- 如果索引是负数或者不小于缓冲区的极限值。
  • ReadOnlyBufferException- 如果这个缓冲区是只读的。

下面是一些例子来说明put(int index, byte f)方法。

例子1 :

// Java program to demonstrate
// put() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer using put() at  index 0
            bb.put(0, (byte)10);
 
            // putting the value in ByteBuffer using put() at  index 2
            bb.put(2, (byte)20);
 
            // putting the value in ByteBuffer using put() at  index 1
            bb.put(1, (byte)30);
 
            // rewinding the ByteBuffer
            bb.rewind();
 
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
        }
 
        catch (IndexOutOfBoundsException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

Original ByteBuffer:  [10, 30, 20]

例2: 演示IndexOutOfBoundsException。

// Java program to demonstrate
// put() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // putting the value in ByteBuffer using put() at  index 0
            bb.put(0, (byte)10);
 
            // putting the value in ByteBuffer using put() at  index 2
            bb.put(2, (byte)20);
 
            // putting the value in ByteBuffer using put() at  index 1
            bb.put(1, (byte)30);
 
            // rewinding the ByteBuffer
            bb.rewind();
 
            // print the ByteBuffer
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array()));
 
            // putting the value in ByteBuffer using put() at  index -1
            bb.put(-1, (byte)40);
        }
 
        catch (IndexOutOfBoundsException e) {
            System.out.println("\nindex is negative or not smaller "
                               + "than the buffer's limit");
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

Original ByteBuffer:  [10, 30, 20]

index is negative or not smaller than the buffer's limit
Exception throws : java.lang.IndexOutOfBoundsException

例3: 演示ReadOnlyBufferException。

// Java program to demonstrate
// put() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 3;
 
        // Creating the ByteBuffer
        try {
 
            // creating object of ByteBuffer
            // and allocating size capacity using allocate() method
            ByteBuffer bb = ByteBuffer.allocate(capacity);
 
            // Creating a read-only copy of ByteBuffer
            // using asReadOnlyBuffer() method
            ByteBuffer bb1 = bb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the byte value"
                               + " in read only buffer");
 
            // putting the value in readonly ByteBuffer
            // using put() method
            bb1.put(0, (byte)10);
        }
 
        catch (IndexOutOfBoundsException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

Trying to put the byte value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#put-int-byte-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程