📜  Java的.nio.ByteBuffer类在Java中

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

Java的.nio.ByteBuffer类在Java中

ByteBuffer保存要在 I/O 操作中使用的整数值序列。 ByteBuffer类对长缓冲区提供以下四类操作:

  • 读取单个字节的绝对和相对获取方法。
  • 写入单个字节的绝对和相对放置方法。
  • 相对批量 put 和 get 方法将连续字节序列从 int 数组或其他一些字节缓冲区传输到此缓冲区,并从此缓冲区传输到数组。

可以通过以下方式创建短缓冲区:

  • 分配():这为缓冲区的内容分配空间。
  • wrap():将现有的长数组包装到缓冲区中。

ByteBuffer 类的大部分方法都直接类似于 ByteBuffer 定义的方法。

类声明:

CharBuffer 类的方法:

MethodDescription
allocate(int capacity)This method allocates a new byte buffer.
allocateDirect(int capacity)This method allocates a new direct byte buffer.
array()This method returns the byte array that backs this buffer
arrayOffset()This method returns the offset within this buffer’s backing array of the first element of the buffer. 
asCharBuffer()This method creates a view of this byte buffer as a char buffer.
asDoubleBuffer()This method creates a view of this byte buffer as a double buffer.
asFloatBuffer()This method creates a view of this byte buffer as a float buffer.
asIntBuffer()This method creates a view of this byte buffer as an int buffer.
asLongBuffer()This method creates a view of this byte buffer as a long buffer.
asReadOnlyBuffer()This method creates a new, read-only byte buffer that shares this buffer’s content.
asShortBuffer()This method creates a view of this byte buffer as a short buffer.
compact()This method compacts this buffer.
compareTo(ByteBuffer that)This method compares this buffer to another.
duplicate()This method creates a new byte buffer that shares this buffer’s content.
equals(Object ob)This method tells whether this buffer is equal to another object.
get()This method is a relative get method and returns the byte at the buffer’s current position.
get(byte[] dst)This method is a relative bulk get method and returns this buffer. 
get(byte[] dst, int offset, int length)This method is a Relative bulk get method and returns this buffer.
get(int index)This method is an Absolute get method and returns the byte at the given index.
getChar()This method is a Relative get method for reading a char value and returns the char value at the buffer’s current position.
getChar(int index)This method is an Absolute get method for reading a char value and returns the char value at the given index.
getDouble()This method is a Relative get method for reading a double value and returns the double value at the buffer’s current position.
getDouble(int index)This method is an Absolute get method for reading a double value and returns the double value at the given index.
getFloat()This method is a Relative get method for reading a float value and returns the float value at the buffer’s current position.
getFloat(int index)This method is an Absolute get method for reading a float value and returns the float value at the given index.
getInt()This method is a Relative get method for reading an int value and returns the int value at the buffer’s current position.
getInt(int index)This method is an Absolute get method for reading an int value and returns the int value at the given index.
getLong()This method is a Relative get method for reading a long value and returns the long value at the buffer’s current position.
getLong(int index)This method is an Absolute get method for reading a long value and returns the int value at the given index.
getShort()This method is a Relative get method for reading a short value and returns the long value at the buffer’s current position.
getShort(int index)This method is an Absolute get method for reading a short value and returns the int value at the given index.
hasArray()This method tells whether this buffer is backed by an accessible byte array.
hashCode()This method returns the current hash code of this buffer.
isDirect()This method tells whether this byte buffer is direct.
order()This method retrieves this buffer’s byte order.
order(ByteOrder bo)This method modifies this buffer’s byte order.
put(byte b)This method is a Relative put method and returns this buffer. 
put(byte[] src)This method is a Relative bulk put method and returns this buffer.
put(ByteBuffer src)This method is a Relative bulk put method and returns this buffer.
put(int index, byte b)This method is an Absolute put method and returns this buffer.
putChar(char value)This method is a Relative put method for writing a char value .
putChar(int index, char value)This method is an absolute put method for writing a char value.
putDouble(double value)This method is a relative put method for writing a double value.
putDouble(int index, double value)This method is an absolute put method for writing a double value.
putFloat(float value)This method is a relative put method for writing a float value.  
putFloat(int index, float value)This method is an absolute put method for writing a float value. 
putInt(int value)This method is a relative put method for writing an int value.
putInt(int index, int value)This method is an absolute put method for writing an int value. 
putLong(int index, long value)This method is an absolute put method for writing a long value.
putLong(long value)This method is a relative put method for writing a long value. 
putShort(int index, short value)This method is an absolute put method for writing a short value. 
putShort(short value)This method is a relative put method for writing a short value. 
slice()This method creates a new byte buffer whose content is a shared subsequence of this buffer’s content.
toString()This method returns a string summarizing the state of this buffer.
wrap(byte[] array)This method wraps a byte array into a buffer.
wrap(byte[] array, int offset, int length)This method wraps a byte array into a buffer.

以下是一些演示 CharBuffer 类及其方法的程序:

示例 1:

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


Java
// Java program to demonstrate
// ByteBuffer class
  
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);
  
            // changeing bytebuffer view as char buffer
            // and putting a string value
            bb.asCharBuffer().put("GeeksForGeeks");
  
            // Declaring char variable c
            char c;
  
            // print the ByteBuffer
            System.out.print("Char buffer : ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
            System.out.println();
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出
Original ByteBuffer: [10, 20, 30, 40]

示例 2:

Java

// Java program to demonstrate
// ByteBuffer class
  
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);
  
            // changeing bytebuffer view as char buffer
            // and putting a string value
            bb.asCharBuffer().put("GeeksForGeeks");
  
            // Declaring char variable c
            char c;
  
            // print the ByteBuffer
            System.out.print("Char buffer : ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
            System.out.println();
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出
Char buffer : G e e k s F o r G e e k s