📜  Java的ByteBuffer getChar() 方法和示例

📅  最后修改于: 2022-05-13 01:57:14.519000             🧑  作者: Mango

Java的ByteBuffer getChar() 方法和示例

获取字符()

Java.nio.ByteBuffer类的getChar()方法用于获取读取字符值的方法

在此缓冲区的当前位置读取接下来的两个字节,根据当前字节顺序将它们组合成一个 char 值,然后将位置增加 2。

句法:

public abstract char getChar()

返回值:此方法返回缓冲区当前位置的字符值

抛出:此方法抛出BufferUnderflowException – 如果缓冲区的当前位置不小于其限制,则抛出此异常。



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

示例 1:

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 50;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("Geeks");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's current position
            // using getChar() method
            char value = bb.getChar();
  
            // print the char value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            char value1 = bb.getChar();
  
            // print the char value
            System.out.print("\nNext Byte Value: " + value1);
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
    }
}
输出:
Original ByteBuffer: 
G e e k s 

Byte Value: G

Next Byte Value: e

示例 2:

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 8;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("abc");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's current position
            // using getChar() method
            char value = bb.getChar();
  
            // print the char value
            System.out.println("\n\nFirst char Value: " + value);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            char value1 = bb.getChar();
  
            // print the char value
            System.out.println("\nSecond char Value: " + value1);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            char value2 = bb.getChar();
  
            // print the char value
            System.out.println("\nThird char Value: " + value2);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            System.out.print("\nsince the buffer current position is incremented");
            System.out.print(" to greater than its limit ");
            char value3 = bb.getChar();
            char value4 = bb.getChar();
        }
  
        catch (BufferOverflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
    }
}
输出:
Original ByteBuffer: a b c 

First char Value: a

Second char Value: b

Third char Value: c

since the buffer current position is incremented to greater than its limit 
Exception Thrown: java.nio.BufferUnderflowException
获取(整数索引)

ByteBuffer 的 get(int index) 方法用于读取给定索引处的两个字节,根据当前字节顺序将它们组合成一个 char 值。

句法:

public abstract char getChar(int index)

参数:此方法将索引(将从中读取字节的索引)作为参数。



返回值:此方法返回给定索引处的字符值。

异常:此方法抛出IndexOutOfBoundsException 。如果 index 为负数或不小于缓冲区的限制,则抛出此异常。

下面是说明get(int index)方法的示例:

示例 1:

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 50;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("abc");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's at index 0
            // using getChar() method
            char value0 = bb.getChar(0);
  
            // print the char value
            System.out.println("\n\nchar Value at index 0: "
                               + value0);
  
            // Reads the  char at this buffer's at index 2
            // using getChar() method
            char value1 = bb.getChar(2);
  
            // print the char value
            System.out.println("\nchar Value at index 2: "
                               + value1);
  
            // Reads the  char at this buffer's at index 4
            // using getChar() method
            char value2 = bb.getChar(4);
  
            // print the char value
            System.out.println("\nchar Value at index 4: "
                               + value2);
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
    }
}
输出:
Original ByteBuffer: a b c 

char Value at index 0: a

char Value at index 2: b

char Value at index 4: c

示例 2:

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 50;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("abc");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's at index 0
            // using getChar() method
            char value0 = bb.getChar(0);
  
            // print the char value
            System.out.println("\n\nchar Value at index 0: "
                               + value0);
  
            // Reads the  char at this buffer's at index 2
            // using getChar() method
            char value1 = bb.getChar(2);
  
            // print the char value
            System.out.println("\nchar Value at index 2: "
                               + value1);
  
            // Reads the  char at this buffer's at index 4
            // using getChar() method
            System.out.println("\nTrying to get the char"
                               + " at negative index ");
            char value2 = bb.getChar(-4);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nException Thrown: "
                               + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: "
                               + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: "
                               + e);
        }
    }
}
输出:
Original ByteBuffer: a b c 

char Value at index 0: a

char Value at index 2: b

Trying to get the char at a negative index 

Exception Thrown: java.lang.IndexOutOfBoundsException

参考:

  • https://docs.oracle.com/javase/9/docs/api/的Java/nio/ByteBuffer.html#getChar-
  • https://docs.oracle.com/javase/9/docs/api/的Java/nio/ByteBuffer.html#getChar-int-