zoukankan      html  css  js  c++  java
  • make和rest用法

    位置(position):下一个要读取或写入的数据的索引。缓冲区的位置不能为负,并且不能大于其限制(limit)。

    标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中一个特定的position,之后可以通过调用reset()方法恢复到这个position。

    make与rest用法

    标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中一个特定的position,之后可以通过调用reset()方法恢复到这个position。

    还原到最初打标机的位置

    重置(reset)

    package com.toov5.Nio;
    
    import java.nio.ByteBuffer;
    
    public class BUfferTest01 {
       
         public static void main(String[] args) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(10); //10个字节
            String str="abcd";
            byteBuffer.put(str.getBytes());
            //开启读模式
            byteBuffer.flip();
            byte[] bytes = new byte[byteBuffer.limit()];
            //缓冲拿到数组
            byteBuffer.get(bytes,0,2);
            byteBuffer.mark(); //做个标记
            System.out.println(new String(bytes,0,2));  //从0到2的位置
            System.out.println(byteBuffer.position());  
            System.out.println("--------------------");
            byteBuffer.reset() ; //还原到标记位置
            byteBuffer.get(bytes,0,2);
            System.out.println(new String(bytes,0,2));
            System.out.println(byteBuffer.position());
        }
        
    }
  • 相关阅读:
    05-3. 六度空间 (PAT)
    05-2. Saving James Bond
    05-1. List Components (PAT)
    04-3. Huffman Codes (PAT)
    04-2. File Transfer (PAT)
    04-1. Root of AVL Tree (PAT)
    03-3. Tree Traversals Again (PAT)
    03-2. List Leaves (PAT)
    03-1. 二分法求多项式单根(PAT)
    CDH Namenode自动切换(active-standby)
  • 原文地址:https://www.cnblogs.com/toov5/p/9931959.html
Copyright © 2011-2022 走看看