zoukankan      html  css  js  c++  java
  • java.nio.Buffer flip()方法的用法详解

     

      今天在看《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-

     
     
  • 相关阅读:
    码农提高工作效率 (转)
    Python快速教程 尾声
    C#基础——谈谈.NET异步编程的演变史
    [C#]動態叫用Web Service
    零极限 核心中的核心和详解
    项目经理应该把30%的时间用在编程上
    高效能程序员的七个习惯
    我们如何进行代码审查
    工作经常使用的SQL整理,实战篇(二)
    C# Socket网络编程精华篇 (转)
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/15640491.html
Copyright © 2011-2022 走看看