📜  Java的.nio.LongBuffer类在Java中

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

Java的.nio.LongBuffer类在Java中

LongBuffer保存要在 I/O 操作中使用的一系列 long 值。 LongBuffer 类对长缓冲区提供以下四类操作:

  • 读取和写入单个 long 的绝对和相对 get 和 put 方法。
  • 将连续的 long 序列从此缓冲区传输到数组中的相对批量 get 方法。
  • 将 long 数组或其他 long 缓冲区中的连续 long 序列传输到此缓冲区的相对批量 put 方法。
  • 一种压缩长缓冲区的方法。

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

  • 分配()方法,为缓冲区的内容分配空间,
  • wrap() 方法,它将现有的长数组包装到缓冲区中,或
  • 通过创建现有字节缓冲区的视图。

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

语法:类声明

public abstract class LongBuffer
extends Buffer
implements Comparable

LongBuffer 类的所有方法如下:



MethodDescription
allocate​(int capacity)This method allocates a new long buffer.
array​()This method returns the long array that backs this buffer.
arrayOffset​()This method returns the offset within this buffer’s backing array of the first element of the buffer.
asReadOnlyBuffer​()This method creates a new, read-only long buffer that shares this buffer’s content.
clear​()This method clears this buffer.
compact​()This method compacts this buffer.
compareTo​(LongBuffer that)This method compares this buffer to another.
duplicate​()This method creates a new long buffer that shares this buffer’s content.
equals​(Object ob)This method tells whether or not this buffer is equal to another object.
flip​()This method flips this buffer.
get​()This method is a relative get method and returns the long at the buffer’s current position.
get​(int index)This method is an absolute get method and returns the long at the given index.
get​(long[] dst)This method is a relative bulk get method and returns this buffer.
get​(long[] dst, int offset, int length)This method is a relative bulk get method and returns this buffer.
hasArray​()This method tells whether this buffer is backed by an accessible long array.
hashCode​()This method returns the current hash code of this buffer.
isDirect​()This method tells whether this long buffer is direct.
limit​(int newLimit)This method sets this buffer’s limit.
mark​()This method sets this buffer’s mark at its position.
order​()This method retrieves this buffer’s byte order.
position​(int newPosition)This method sets this buffer’s position.
put​(int index, long l)This method is an absolute put method and returns this buffer.
put​(long l)This method is a relative put method and returns this buffer.
put​(long[] src)This method is a relative bulk put method and returns this buffer.
put​(long[] src, int offset, int length)This method is a relative bulk put method and returns this buffer.
put​(LongBuffer src)This method is a relative bulk put method and returns this buffer.
reset​()This method resets this buffer’s position to the previously-marked position.
rewind​()This method rewinds this buffer.
slice​()This method creates a new long 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​(long[] array)This method wraps a long array into a buffer.
wrap​(long[] array, int offset, int length)This method wraps a long array into a buffer.

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

示例 1:

Java
// Java program to demonstrate LongBuffer class
 
// Importing input output classes
import java.nio.*;
// Importing all utility classes
import java.util.*;
 
// Main Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing variable to
        // the capacity of the LongBuffer
        int capacity = 5;
 
        // try block to check for exceptions
        try {
 
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
 
            // Adding elements to the objects of Longbuffer
            // class
            // using the pur() method
            ib.put(9);
            ib.put(8);
            ib.put(5);
            ib.rewind();
 
            // print the original LongBuffer
            // using standard toString() method
            System.out.println(
                "Original LongBuffer: "
                + Arrays.toString(ib.array()));
 
            // Reads the Long at this buffer's current
            // position using get() method
            Long value = ib.get();
 
            // Print the Long value
            System.out.println("Long Value: " + value);
 
            // Reads the Long at this buffer's next position
            // using get() method
            Long value1 = ib.get();
 
            // Agan, now print the Long value
            System.out.print("Next Long Value: " + value1);
        }
 
        // Catch blocks to handle the exceptions
 
        // Catch block 1
        catch (IllegalArgumentException e) {
 
            // Print the message when there is illegal
            // arguments
            System.out.println(
                "IllegalArgumentException catched");
        }
 
        // Catch block 2
        catch (ReadOnlyBufferException e) {
 
            // Print statement when an exception is encountered
            System.out.println(
                "ReadOnlyBufferException catched");
        }
 
        // Catch block 3
        catch (BufferUnderflowException e) {
 
            // Print statement when an exception is encountered
            System.out.println("Exception throws: " + e);
        }
    }
}


Java
// Java program to demonstrate LongBuffer class
 
// Importing required libraries
import java.nio.*;
import java.util.*;
 
// Main Class
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring and initialiazing variable to
        // capacity of the LongBuffer
        int Capacity = 10;
 
        // Creating the LongBuffer
 
        // creating object of Longbuffer
        // and allocating size capacity
        LongBuffer ib = LongBuffer.allocate(Capacity);
 
        // Inserting the value in Longbuffer
        // Custom entries
        ib.put(11);
        ib.put(5, 22);
 
        // Print all the elements inside Longbuffer by
        // use of Arrays.toString() method
        System.out.println("LongBuffer: "
                           + Arrays.toString(ib.array()));
    }
}



输出
Original LongBuffer: [9, 8, 5, 0, 0]
Long Value: 9
Next Long Value: 8

示例 2:

Java

// Java program to demonstrate LongBuffer class
 
// Importing required libraries
import java.nio.*;
import java.util.*;
 
// Main Class
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring and initialiazing variable to
        // capacity of the LongBuffer
        int Capacity = 10;
 
        // Creating the LongBuffer
 
        // creating object of Longbuffer
        // and allocating size capacity
        LongBuffer ib = LongBuffer.allocate(Capacity);
 
        // Inserting the value in Longbuffer
        // Custom entries
        ib.put(11);
        ib.put(5, 22);
 
        // Print all the elements inside Longbuffer by
        // use of Arrays.toString() method
        System.out.println("LongBuffer: "
                           + Arrays.toString(ib.array()));
    }
}


输出
LongBuffer: [11, 0, 0, 0, 0, 22, 0, 0, 0, 0]