Java ShortBuffer rewind()方法及示例

Java ShortBuffer rewind()方法及示例

java.nio.ShortBuffer类的 rewind() 方法是用来倒退这个缓冲区的。通过倒退这个Buffer,将采取以下行动。

  • 当前位置被设置为零
  • 标记被丢弃(如果有的话),但是标记值没有变化。

语法

public ShortBuffer rewind()

参数。此方法不接受任何参数。

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

下面是一些例子来说明rewind()方法。

例子 1 :

// Java program to demonstrate
// rewind() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating ShortBuffer
        // using allocate() method
        ShortBuffer shortBuffer
            = ShortBuffer.allocate(4);
  
        // put short value in shortBuffer
        // using put() method
        shortBuffer.put((short)10);
        shortBuffer.put((short)20);
  
        // print the short buffer
        System.out.println(
            "Buffer before operation: "
            + Arrays.toString(
                  shortBuffer.array())
            + "\nPosition: "
            + shortBuffer.position()
            + "\nLimit: "
            + shortBuffer.limit());
  
        // rewind the Buffer
        // using rewind() method
        shortBuffer.rewind();
  
        // print the shortbuffer
        System.out.println(
            "\nBuffer after operation: "
            + Arrays.toString(
                  shortBuffer.array())
            + "\nPosition: "
            + shortBuffer.position()
            + "\nLimit: "
            + shortBuffer.limit());
    }
}

输出:

Buffer before operation: [10, 20, 0, 0]
Position: 2
Limit: 4

Buffer after operation: [10, 20, 0, 0]
Position: 0
Limit: 4

例子 2 :

// Java program to demonstrate
// rewind() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating ShortBuffer
        // using allocate() method
        ShortBuffer shortBuffer
            = ShortBuffer.allocate(5);
  
        // put short value in shortBuffer
        // using put() method
        shortBuffer.put((short)10);
        shortBuffer.put((short)20);
        shortBuffer.put((short)30);
  
        // mark will be going to discarded by rewind()
        shortBuffer.mark();
  
        // print the buffer
        System.out.println(
            "Buffer before operation: "
            + Arrays.toString(
                  shortBuffer.array())
            + "\nPosition: "
            + shortBuffer.position()
            + "\nLimit: "
            + shortBuffer.limit());
  
        // Rewind the Buffer
        // using rewind() method
        shortBuffer.rewind();
  
        // print the buffer
        System.out.println(
            "\nBuffer after operation: "
            + Arrays.toString(
                  shortBuffer.array())
            + "\nPosition: "
            + shortBuffer.position()
            + "\nLimit: "
            + shortBuffer.limit());
    }
}

输出:

Buffer before operation: [10, 20, 30, 0, 0]
Position: 3
Limit: 5

Buffer after operation: [10, 20, 30, 0, 0]
Position: 0
Limit: 5

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/ShortBuffer.html#rewind-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程