JDK 源码学习——ByteBuffer
来源:互联网 发布:java项目的需求分析 编辑:程序博客网 时间:2024/06/05 06:01
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
- JDK 源码学习——ByteBuffer
- Java NIO学习笔记——ByteBuffer用法
- JDK并发工具类源码学习系列——介绍
- JDK并发工具类源码学习系列——ConcurrentHashMap
- JDK并发工具类源码学习系列——ConcurrentLinkedQueue
- JDK并发工具类源码学习系列——ConcurrentSkipListMap
- JDK并发工具类源码学习系列——CopyOnWriteArrayList
- JDK并发工具类源码学习系列——PriorityBlockingQueue
- JDK并发工具类源码学习系列——LinkedBlockingQueue
- JDK并发工具类源码学习系列——SynchronousQueue
- JDK并发工具类源码学习系列——PriorityBlockingQueue
- netty源码分析之-ByteBuf详解以及与JDK中ByteBuffer差异(11)
- JDK ByteBuffer & MINA IoBuffer 总结
- JDK ByteBuffer & MINA IoBuffer 总结
- JDK ByteBuffer & MINA IoBuffer 总结 .
- jdk源码学习------String
- java byteBuffer 的学习
- Bytebuffer学习笔记二
- java学习注意点
- TTP223-BAT功能简介
- 系统测试的基本认识
- Windows快速打开命令及快捷键
- ecilpse环境下一些常用的函数代码(math/string/time)
- JDK 源码学习——ByteBuffer
- C/C++异常处理机制
- Maven学习总结
- Unreal Cook Book:神奇的Spline Mesh
- 【Tyvj P1006】isbn
- Android开发之点9.png技术的使用
- 关于LNMP下pathinfo模式下THINKPHP框架U函数的地址错误问题
- 14.SpringMVC 异常处理 - HandlerExceptionResolver
- 【推荐】Java工程师如何从普通成为大神值得一读