📜  Java的.nio.ShortBuffer类在Java中

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

Java的.nio.ShortBuffer类在Java中

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

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

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

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

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

类声明:

ShortBuffer 类的方法:

MethodDescription
allocate(int capacity)This method allocates a new short buffer.
array()This method returns the short 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 short buffer that shares this buffer’s content.
compact()This method compacts this buffer.
compareTo(ShortBuffer that)This method compares this buffer to another.
duplicate()This method creates a new short 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 short at the buffer’s current position.
get(int index)This method is an absolute get method and returns the short at the given index.
get(int[] dst)This method is a relative bulk get method and returns this buffer.
get(int[] dst, int offset, int length)This method relative is a bulk get method and returns this buffer.
hasArray()This method tells whether this buffer is backed by an accessible int array. 
isDirect()This method tells whether this int buffer is direct.
order()This method retrieves this buffer’s byte order.
put(int index, short s)This method is an absolute put method and returns this buffer.  
put(short s)This method is a relative put method and returns this buffer.
put(short[] src)This method is a relative bulk put method and returns this buffer.
put(short[] src, int offset, int length)This method is a relative bulk put method and returns this buffer.
put(ShortBuffer src)This method is a relative bulk put method and returns this buffer.
slice()This method creates a new short 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(short[] array)This method wraps a short array into a buffer.
wrap(short[] array, int offset, int length)This method wraps a short array into a buffer.

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

示例 1:

Java
// Java program to demonstrate
// ShortBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer
        int capacity = 10;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of Shortbuffer
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity);
  
            // putting the value in Shortbuffer
            sb.put((short)100);
            sb.put(2, (short)9);
            sb.rewind();
  
            // getting array from fb ShortBuffer using
            // array() method
            short[] sbb = sb.array();
  
            // printing the ShortBuffer fb
            System.out.println("ShortBuffer: "
                               + Arrays.toString(sbb));
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println(
                "IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println(
                "ReadOnlyBufferException catched");
        }
    }
}


Java
// Java program to demonstrate
// ShortBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the ShortBuffer 2
        int capacity2 = 10;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of Shortbuffer 1
            // and allocating size capacity
            ShortBuffer sb1
                = ShortBuffer.allocate(capacity1);
  
            // creating object of Shortbuffer 2
            // and allocating size capacity
            ShortBuffer fb2
                = ShortBuffer.allocate(capacity2);
  
            // putting the value in Shortbuffer 1
            sb1.put((short)100);
            sb1.put(2, (short)9);
            sb1.rewind();
  
            // putting the value in Shortbuffer 2
            fb2.put((short)100);
            fb2.put(2, (short)9);
            fb2.rewind();
  
            // print the ShortBuffer 1
            System.out.println(
                "ShortBuffer 1: "
                + Arrays.toString(sb1.array()));
  
            // print the ShortBuffer 2
            System.out.println(
                "ShortBuffer 2: "
                + Arrays.toString(fb2.array()));
  
            // checking the equality of both ShortBuffer
            boolean fbb = sb1.equals(fb2);
  
            // checking if else condition
            if (fbb)
                System.out.println("both are equal");
            else
                System.out.println("both are not equal");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println(
                "IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println(
                "ReadOnlyBufferException catched");
        }
    }
}


输出
ShortBuffer: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]

示例 2:

Java

// Java program to demonstrate
// ShortBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the ShortBuffer 2
        int capacity2 = 10;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of Shortbuffer 1
            // and allocating size capacity
            ShortBuffer sb1
                = ShortBuffer.allocate(capacity1);
  
            // creating object of Shortbuffer 2
            // and allocating size capacity
            ShortBuffer fb2
                = ShortBuffer.allocate(capacity2);
  
            // putting the value in Shortbuffer 1
            sb1.put((short)100);
            sb1.put(2, (short)9);
            sb1.rewind();
  
            // putting the value in Shortbuffer 2
            fb2.put((short)100);
            fb2.put(2, (short)9);
            fb2.rewind();
  
            // print the ShortBuffer 1
            System.out.println(
                "ShortBuffer 1: "
                + Arrays.toString(sb1.array()));
  
            // print the ShortBuffer 2
            System.out.println(
                "ShortBuffer 2: "
                + Arrays.toString(fb2.array()));
  
            // checking the equality of both ShortBuffer
            boolean fbb = sb1.equals(fb2);
  
            // checking if else condition
            if (fbb)
                System.out.println("both are equal");
            else
                System.out.println("both are not equal");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println(
                "IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println(
                "ReadOnlyBufferException catched");
        }
    }
}
输出
ShortBuffer 1: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]
ShortBuffer 2: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]
both are equal