ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java.nio.Buffer flip()方法的用法详解

2021-12-03 23:00:59  阅读:245  来源: 互联网

标签:java nio Buffer buffer fc ByteBuffer import


 

  今天在看《Java编程思想》的时候,碰到java.nio.Buffer flip()方法,不明白是干什么用的,于是就赶快查看中文API,API上面翻译的是:"反转此缓冲区。首先对当前位置设置限制,然后将该位置设置为零。如果已定义了标记,则丢弃该标记。"; 

  说实话,看了几遍,真还不明白说的是什么意思,于是就查看英文API,是这样说的:Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.,意思大概是这样的:调换这个buffer的当前位置,并且设置当前位置是0。说的意思就是:将缓存字节数组的指针设置为数组的开始序列即数组下标0。这样就可以从buffer开头,对该buffer进行遍历(读取)了。 

  buffer中的flip方法涉及到bufer中的Capacity,Position和Limit三个概念。其中Capacity在读写模式下都是固定的,就是我们分配的缓冲大小,Position类似于读写指针,表示当前读(写)到什么位置,Limit在写模式下表示最多能写入多少数据,此时和Capacity相同,在读模式下表示最多能读多少数据,此时和缓存中的实际数据大小相同。在写模式下调用flip方法,那么limit就设置为了position当前的值(即当前写了多少数据),postion会被置为0,以表示读操作从缓存的头开始读。也就是说调用flip之后,读写指针指到缓存头部,并且设置了最多只能读出之前写入的数据长度(而不是整个缓存的容量大小)。
  
    实例代码(借用Java编程思想P552的代码): 

  

复制代码
 1 package cn.com.newcom.ch18;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.RandomAccessFile;
 6 import java.nio.ByteBuffer;
 7 import java.nio.channels.FileChannel;
 8 
 9 /**
10  * 获取通道
11  * 
12  * @author zhq
13  * 
14  */
15 public class GetChannel {
16     private static final int SIZE = 1024;
17 
18     public static void main(String[] args) throws Exception {
19         // 获取通道,该通道允许写操作
20         FileChannel fc = new FileOutputStream("data.txt").getChannel();
21         // 将字节数组包装到缓冲区中
22         fc.write(ByteBuffer.wrap("Some text".getBytes()));
23         // 关闭通道
24         fc.close();
25 
26         // 随机读写文件流创建的管道
27         fc = new RandomAccessFile("data.txt", "rw").getChannel();
28         // fc.position()计算从文件的开始到当前位置之间的字节数
29         System.out.println("此通道的文件位置:" + fc.position());
30         // 设置此通道的文件位置,fc.size()此通道的文件的当前大小,该条语句执行后,通道位置处于文件的末尾
31         fc.position(fc.size());
32         // 在文件末尾写入字节
33         fc.write(ByteBuffer.wrap("Some more".getBytes()));
34         fc.close();
35 
36         // 用通道读取文件
37         fc = new FileInputStream("data.txt").getChannel();
38         ByteBuffer buffer = ByteBuffer.allocate(SIZE);
39         // 将文件内容读到指定的缓冲区中
40         fc.read(buffer);
41         buffer.flip();//此行语句一定要有
42         while (buffer.hasRemaining()) {
43             System.out.print((char)buffer.get());
44         }
45                   fc.close();
46     }
47 }
复制代码

  注意:buffer.flip();一定得有,如果没有,就是从文件最后开始读取的,当然读出来的都是byte=0时候的字符。通过buffer.flip();这个语句,就能把buffer的当前位置更改为buffer缓冲区的第一个位置。

  分类: Java     _________________________________________________________________________________

java.nio.Buffer类的hasRemaining()方法用于判断当前位置和限制之间是否有任何元素。

用法:

public final boolean hasRemaining()

返回值:当且仅当此缓冲区中至少剩余一个元素时,此方法才会返回true。


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

范例1:

// Java program to demonstrate 
// hasRemaining() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 10; 
  
        // creating object of bytebuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
        // putting the value in bytebuffer 
        bb.put((byte)10); 
        bb.put((byte)20); 
        bb.rewind(); 
  
        // Typecast bytebuffer to Buffer 
        Buffer buffer = (Buffer)bb; 
  
        // checking buffer is backed by array or not 
        boolean isRemain = buffer.hasRemaining(); 
  
        // checking if else condition 
        if (isRemain) 
            System.out.println("there is at least one "
                               + "element remaining "
                               + "in this buffer"); 
        else
            System.out.println("there is no "
                               + "element remaining "
                               + "in this buffer"); 
    } 
}
  输出:
there is at least one element remaining in this buffer

范例2:

// Java program to demonstrate 
// hasRemaining() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 0; 
  
        // creating object of bytebuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
        // Typecast bytebuffer to Buffer 
        Buffer buffer = (Buffer)bb; 
  
        // checking buffer is backed by array or not 
        boolean isRemain = buffer.hasRemaining(); 
  
        // checking if else condition 
        if (isRemain) 
            System.out.println("there is at least one "
                               + "element remaining"
                               + " in this buffer"); 
        else
            System.out.println("there is no "
                               + "element remaining"
                               + " in this buffer"); 
    } 
}
  输出:
there is no element remaining in this buffer

参考: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#hasRemaining-

   

标签:java,nio,Buffer,buffer,fc,ByteBuffer,import
来源: https://www.cnblogs.com/kelelipeng/p/15640491.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有