zoukankan      html  css  js  c++  java
  • JDK 源码学习——ByteBuffer

    ByteBuffer 在NIO的作用

    Java SE4 开始引入Java NIO,相比较于老的IO,更加依赖底层实现。引入通道(Channels),选择器(selector),缓冲(Buffers).都更加接近操作系统执行I/O的方式.所以速度更快。

    NIO 是非阻塞IO,非阻塞IO的实现是基于事件的,选择器定义了一个IO通道,程序继续运行。选择器上发生事件时,会唤醒选择器并执行相应动作。IO是阻塞IO,面向流操作,顺序读写,对于小文件序列化的读写有优势。nio是面向缓冲器,对数据的偏移支持比较好。

    本文提到的ByteBuffer就是缓冲器,负责与通道进行数据交换。缓冲器总共有ByteBuffer,CharBuffer,DoubleBuffer,FloatBuffer,IntBuffer,LongBuffer,ShortBuffer.

    ByteBuffer的重要属性

    Buffer 基类

    属性
    缓冲器都继承了Buffer类,Buffer是一个固定大小的数据容器。除了存储内容之外,还有三个重要的属性。

    • capacity buffer中元素总数

    • position 写模式下就是下一个元素的index,读模式就是当前元素index

    • limit 是不应该被读/写的第一个元素index。写模式时就是capacity,读模式则是下一个元素的index.如下图:图片来自http://javapapers.com/java/java-nio-buffer/

    这里写图片描述

    方法

    • flip
      一般是切换到读操作。或者是为写操作准备一个新的序列
      public final Buffer flip() {
            limit = position;
            position = 0;
            mark = -1;
            return this;
        }

    eg.重复向一个ByteBuffer写数据的时候,赋值完毕,必须要flip.开始一个新的新序列,否则position会等于limit,返回空值

    public static void main(String[] args) {
            byte[] bytes1=new byte[]{1, 6, 3};
            ByteBuffer buffer =fromByteArray(bytes1);
            System.out.println(buffer);
            byte[] bytes2 =new byte[]{1,2,3};
            ByteBuffer buffer2=fromByteArray(bytes2);
            System.out.println(buffer2);
        }
    
        /**
         * If you are building up a ByteBuffer by repeatedly writing into it, and then want to give it away, you must remember to flip() it. 
         * If we did not flip() it, the returned ByteBuffer would be empty because the position would be equal to the limit.
         * @param bytes
         * @return
         */
        public static ByteBuffer fromByteArray(byte[] bytes) {
            final ByteBuffer ret = ByteBuffer.wrap(new byte[bytes.length]);
    
            ret.put(bytes);
            ret.flip();
    
            return ret;
        } 
    • rewind

    rewind倒回,将position 设置为0,重新读取

     public final Buffer rewind() {
            position = 0;
            mark = -1;
            return this;
        }
    • clear
      clear 并没有真正的清除数据,将position设置为0,limit设置为capacity.
      public final Buffer clear() {
            position = 0;
            limit = capacity;
            mark = -1;
            return this;
        }

    ByteBuffer

    ByteBuffer 相比较其他的缓冲器有些区别

    • 可以分配直接缓冲区
      直接缓冲区,JVM会尽可能的直接在此缓冲区执行本机IO操作。避免与中间缓冲区交互

    • 可以通过mapping将文件区域直接映射到内存来创建、

    • 访问二进制数据

    参考

    http://www.javaworld.com/article/2078654/java-se/java-se-five-ways-to-maximize-java-nio-and-nio-2.html
    http://javapapers.com/java/java-nio-buffer/
    https://docs.oracle.com/javase/7/docs/api/java/nio/package-summary.html#buffers

  • 相关阅读:
    3月14日进度博客
    构建之法阅读笔记01
    课堂练习-全国疫情统计3
    课堂练习-全球疫情统计2
    课堂练习-全国疫情统计1
    跟我一起写 Makefile(五)
    跟我一起写 Makefile(四)
    跟我一起写 Makefile(三)
    跟我一起写 Makefile(二)
    跟我一起写 Makefile(一)
  • 原文地址:https://www.cnblogs.com/stoneFang/p/6715291.html
Copyright © 2011-2022 走看看