ByteBuffer常用方法详解
来源:互联网 发布:手机怎么看淘宝卖家id 编辑:程序博客网 时间:2024/06/05 09:53
缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/O)的数据作临时存储,这部分预留的内存空间就叫做缓冲区:
使用缓冲区有这么两个好处:
1、减少实际的物理读写次数
2、缓冲区在创建时就被分配内存,这块内存区域一直被重用,可以减少动态分配和回收内存的次数
举个简单的例子,比如A地有1w块砖要搬到B地
由于没有工具(缓冲区),我们一次只能搬一本,那么就要搬1w次(实际读写次数)
如果A,B两地距离很远的话(IO性能消耗),那么性能消耗将会很大
但是要是此时我们有辆大卡车(缓冲区),一次可运5000本,那么2次就够了
相比之前,性能肯定是大大提高了。
所以,buffer在IO中很重要。在旧I/O类库中(相对java.nio包)中的BufferedInputStream、BufferedOutputStream、BufferedReader和BufferedWriter在其实现中都运用了缓冲区。java.nio包公开了Buffer API,使得Java程序可以直接控制和运用缓冲区。
在Java NIO中,缓冲区的作用也是用来临时存储数据,可以理解为是I/O操作中数据的中转站。缓冲区直接为通道(Channel)服务,写入数据到通道或从通道读取数据,这样的操利用缓冲区数据来传递就可以达到对数据高效处理的目的。在NIO中主要有八种缓冲区类(其中MappedByteBuffer是专门用于内存映射的一种ByteBuffer):
Buffer
所有缓冲区都有4个属性:capacity、limit、position、mark,并遵循:mark <= position <= limit <= capacity,下表格是对着4个属性的解释:
Method:
1、实例化
java.nio.Buffer类是一个抽象类,不能被实例化。Buffer类的直接子类,如ByteBuffer等也是抽象类,所以也不能被实例化。
但是ByteBuffer类提供了4个静态工厂方法来获得ByteBuffer的实例:
int offset, int length)在上一个方法的基础上可以指定偏移量和长度,这个offset也就是包装后byteBuffer的position,而length呢就是limit-position的大小,从而我们可以得到limit的位置为length+position(offset)
我写了这几个方法的测试方法,大家可以运行起来更容易理解
public static void main(String args[]) throws FileNotFoundException { System.out.println("----------Test allocate--------"); System.out.println("before alocate:" + Runtime.getRuntime().freeMemory()); // 如果分配的内存过小,调用Runtime.getRuntime().freeMemory()大小不会变化? // 要超过多少内存大小JVM才能感觉到? ByteBuffer buffer = ByteBuffer.allocate(102400); System.out.println("buffer = " + buffer); System.out.println("after alocate:" + Runtime.getRuntime().freeMemory()); // 这部分直接用的系统内存,所以对JVM的内存没有影响 ByteBuffer directBuffer = ByteBuffer.allocateDirect(102400); System.out.println("directBuffer = " + directBuffer); System.out.println("after direct alocate:" + Runtime.getRuntime().freeMemory()); System.out.println("----------Test wrap--------"); byte[] bytes = new byte[32]; buffer = ByteBuffer.wrap(bytes); System.out.println(buffer); buffer = ByteBuffer.wrap(bytes, 10, 10); System.out.println(buffer); }
2、另外一些方法
以下为一些测试方法:
public static void main(String args[]) throws FileNotFoundException { System.out.println("--------Test reset----------"); buffer.clear(); buffer.position(5); buffer.mark(); buffer.position(10); System.out.println("before reset:" + buffer); buffer.reset(); System.out.println("after reset:" + buffer); System.out.println("--------Test rewind--------"); buffer.clear(); buffer.position(10); buffer.limit(15); System.out.println("before rewind:" + buffer); buffer.rewind(); System.out.println("before rewind:" + buffer); System.out.println("--------Test compact--------"); buffer.clear(); buffer.put("abcd".getBytes()); System.out.println("before compact:" + buffer); System.out.println(new String(buffer.array())); buffer.flip(); System.out.println("after flip:" + buffer); System.out.println((char) buffer.get()); System.out.println((char) buffer.get()); System.out.println((char) buffer.get()); System.out.println("after three gets:" + buffer); System.out.println("\t" + new String(buffer.array())); buffer.compact(); System.out.println("after compact:" + buffer); System.out.println("\t" + new String(buffer.array())); System.out.println("------Test get-------------"); buffer = ByteBuffer.allocate(32); buffer.put((byte) 'a').put((byte) 'b').put((byte) 'c').put((byte) 'd') .put((byte) 'e').put((byte) 'f'); System.out.println("before flip()" + buffer); // 转换为读取模式 buffer.flip(); System.out.println("before get():" + buffer); System.out.println((char) buffer.get()); System.out.println("after get():" + buffer); // get(index)不影响position的值 System.out.println((char) buffer.get(2)); System.out.println("after get(index):" + buffer); byte[] dst = new byte[10]; buffer.get(dst, 0, 2); System.out.println("after get(dst, 0, 2):" + buffer); System.out.println("\t dst:" + new String(dst)); System.out.println("buffer now is:" + buffer); System.out.println("\t" + new String(buffer.array())); System.out.println("--------Test put-------"); ByteBuffer bb = ByteBuffer.allocate(32); System.out.println("before put(byte):" + bb); System.out.println("after put(byte):" + bb.put((byte) 'z')); System.out.println("\t" + bb.put(2, (byte) 'c')); // put(2,(byte) 'c')不改变position的位置 System.out.println("after put(2,(byte) 'c'):" + bb); System.out.println("\t" + new String(bb.array())); // 这里的buffer是 abcdef[pos=3 lim=6 cap=32] bb.put(buffer); System.out.println("after put(buffer):" + bb); System.out.println("\t" + new String(bb.array())); }
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer详解
- java.nio.ByteBuffer 详解
- java.nio.ByteBuffer 详解 .
- ByteBuffer使用详解
- Java之ByteBuffer详解
- NIO 之 ByteBuffer详解
- Java NIO ByteBuffer详解
- ByteBuffer.mark()方法
- ByteBuffer转String的方法
- ByteBuffer.allocateDirect 与 allocate 方法
- ByteBuffer
- Ignoring query to other database
- IQKeyboardManager
- [java源码分析]HashMap源码分析
- Android中dip、dp、sp、pt和px的区别
- 深入浅出学Hive——Hive优化
- ByteBuffer常用方法详解
- Visio图标下载链接
- 读隐私文献-1
- 平面几何常用定理、结论总结 第一篇三角形及其引线、引圆
- Hibernate连接MySQL数据库乱码相关问题
- 结构体字节对齐
- GitHub前50名的Objective-C动画相关库
- ios学习--Objective-C runtime的使用
- lxc(linux container) 命令学习