ByteBuffer
ByteBuffer的结构
ByteBuffer的底层其实就是一个字节数组,它有以下重要属性
- capacity:缓冲区的容量
- position:读写数据的索引
- limit:读写入数据的限制
ByteBuffer有两种模式:写模式和读模式,当ByteBuffer被创建后,默认是为写模式
如下图:
这是ByteBuffer刚被创建时的状态,在写模式下,position是写入的索引位置,limit等于容量
这是写入了4个字节后的状态,这里可以看到position的索引是4,表示下一次写入的位置,position要 小于limit,如果没有limit限制写入的位置,那position就会超出容量,而ByteBuffer的容量是在创建的时候就固定了的,后面是无法动态修改的
如果需要从ByteBuffer中读取数据,就需要使用flip方法切换为读模式
这是ByteBuffer切换为读模式的状态,position切换为读取的位置0,limit切换为读取限制4。因为position为读写数据的索引,所以如果不切换为读模式就读数据的话,那读到的就是上一个图片position位置上的空数据,同理,如果不切换为读模式,那limit限制的位置还是为上一个图片上的写入限制的位置,那就会读到空数据。切换读写模式主要是为了切换position和limit的位置,方便读写数据
ByteBuffer有两个方法可以切换为写模式:clear()和compact()
这是clear动作发生后的状态,不管里面的数据有没有读完,position都切换为0,limit等于容量。所以这个方法只适合数据已经读完或者没读完但不需要读了的情况,当数据没有读完,还需要读时,就不适合使用这个方法,就需要使用compact()
这个是使用compact动作后的状态,compact方法会把未读完的部分向前压缩,然后切换为写模式
ByteBuffer的使用步骤
- 向buffer写入数据,例如调用channel,read(buffer)
- 调用flip()切换至读模式
- 从buffer读取数据,例如调用buffer.get()
- 调用clear()或compact()切换至写模式
ByteBuffer的常见方法
分配空间
可以使用allocate方法为ByteBuffer分配空间,其他buffer类也有该方法
//创建容量为16个字节的ByteBuffer
Bytebuffer buf = ByteBuffer.allocate(16);
这里分配的空间不能动态的调整,是固定的
还有一个与allocate类似的方法也可以用来为ByteBuffer分配空间
ByteBuffer buffer = ByteBuffer.allocateDirect(10);
打印这两个方法的类
System.out.println(ByteBuffer.allocate(16).getClass());
System.out.println(ByteBuffer.allocateDirect(16).getClass());
得到的结果
class java.nio.HeapByteBuffer - java 堆内存,读写效率较低,受到 GC 的影响(会移动数据,使数据间更加紧凑)
class java.nio.DirectByteBuffer - 直接内存,读写效率高(少一次拷贝),不会受 GC 影响,分配的效率低(需要调用系统的函数来分配内存)
向buffer写入数据
两种方式
- 调用channel的read方法,利用通道,读取文件中的数据到缓冲区
int readBytes = channel.read(buf);
- 调用buffer自己的put方法
buf.put((byte)127);
因为ByteBuffer存储的字节,所以要将数据转换为字节写入
从buffer读取数据
同样有两种方法
- 调用channel的write方法,利用通道将数据从缓冲区写入到文件中
int writeBytes = channel.write(buf);
- 调用buffer自己的get方法
byte b = buf.get();
get方法会让position读指针向后走,如果想重复读取数据
- 可以调用rewind方法将position重置为0
- 或者调用get(int i)方法获取索引 i 的内容,它不会移动指针
mark和reset方法
mark方法是在读取时,做一个标记,标记当前position的位置,即使position改变,只要调用reset方法position就能回到mark位置
注意
rewind 和 flip都会清除mark位置
测试
debugAll()是一个工具类中的方法,可以模拟输出不同操作后buffer的状态
public class ByteBufferReadWrite2 {
public static void main(String[] args) {
//1.创建ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(10);//容量设置为10个字节
debugAll(buffer);
//2.向缓冲区写入数据
buffer.put(new byte[]{(byte)'a',(byte)'b',(byte)'c',(byte)'d'});
debugAll(buffer);
//3.切换为读模式
buffer.flip();
debugAll(buffer);
//4.读取数据并打印出来
System.out.println((char)buffer.get());
debugAll(buffer);
//5.使用get(int i)读取数据
System.out.println((char)buffer.get(3));
debugAll(buffer);
//6.为了更好的测试,这里多读取一次数据
System.out.println((char)buffer.get());
debugAll(buffer);
//7.使用mark方法标记当前position位置
buffer.mark();//这里的标记的位置为2
//8.rewind从头开始读
// buffer.rewind();
// debugAll(buffer);
//10.多读取一次数据测试reset方法
System.out.println((char)buffer.get());
debugAll(buffer);
//9.测试使用rewind方法、会清除mark的位置
buffer.reset();
debugAll(buffer);
}
}
结果:
+--------+-------------------- all ------------------------+----------------+
1.这是创建buffer后的状态,position为初始值,limit等于容量
position: [0], limit: [10]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 00 00 00 00 00 00 00 00 00 00 |.......... |
+--------+-------------------------------------------------+----------------+
+--------+-------------------- all ------------------------+----------------+
2.这是向缓冲区内写入abcd后的状态,position为下一次插入的位置4
position: [4], limit: [10]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
+--------+-------------------- all ------------------------+----------------+
3、切换为读模式后,position和limit切换了
position: [0], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
4、取出一个数据后position变为下次取的位置1,因为ByteBuffer存储的是字节,所以取出来也是字节,这里将字节转换成字符类型方便查看
a
+--------+-------------------- all ------------------------+----------------+
position: [1], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
5、这里使用get加下标的形式取数据,对比上一个Bytebuffer的状态,position并不会发生改变
d
+--------+-------------------- all ------------------------+----------------+
position: [1], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
6、这里使用get读取数据,可以发现position的位置改变了
b
+--------+-------------------- all ------------------------+----------------+
position: [2], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
8、这里是使用了remind方法,可以看到position回到了0,从头开始读,为了方便下面的测试,代码部分把这个注释了
+--------+-------------------- all ------------------------+----------------+
position: [0], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
10、这里读取了一次数据,为了方便测试reset方法,因为第8步被注释掉了,所以上一步是第6步,position由2变为3
c
+--------+-------------------- all ------------------------+----------------+
position: [3], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
+--------+-------------------- all ------------------------+----------------+
9、这里使用了reset方法,第7步的时候使用mark方法标记了position的位置2,可以发现使用了reset方法后,position回到了使用mark方法标记的位置2
position: [2], limit: [4]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
+--------+-------------------------------------------------+----------------+
Scattering Reads
分散读取可以将数据填充至多个 buffer
try (RandomAccessFile file = new RandomAccessFile("helloword/3parts.txt", "rw")) {
FileChannel channel = file.getChannel();
ByteBuffer a = ByteBuffer.allocate(3);
ByteBuffer b = ByteBuffer.allocate(3);
ByteBuffer c = ByteBuffer.allocate(5);
channel.read(new ByteBuffer[]{a, b, c});
a.flip();
b.flip();
c.flip();
debug(a);
debug(b);
debug(c);
} catch (IOException e) {
e.printStackTrace();
}
这里的debug也是一个工具类的方法,可以更明显的看到ByteBuffer的状态
结果
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 6f 6e 65 |one |
+--------+-------------------------------------------------+----------------+
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 74 77 6f |two |
+--------+-------------------------------------------------+----------------+
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 74 68 72 65 65 |three |
+--------+-------------------------------------------------+----------------+
Gathering Writes
集中写入可以将多个 buffer 的数据填充至 channel
try (RandomAccessFile file = new RandomAccessFile("helloword/3parts.txt", "rw")) {
FileChannel channel = file.getChannel();
ByteBuffer d = ByteBuffer.allocate(4);
ByteBuffer e = ByteBuffer.allocate(4);
channel.position(11);
d.put(new byte[]{'f', 'o', 'u', 'r'});
e.put(new byte[]{'f', 'i', 'v', 'e'});
d.flip();
e.flip();
debug(d);
debug(e);
channel.write(new ByteBuffer[]{d, e});
} catch (IOException e) {
e.printStackTrace();
}
结果
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 6f 6e 65 |one |
+--------+-------------------------------------------------+----------------+
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 74 77 6f |two |
+--------+-------------------------------------------------+----------------+
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 74 68 72 65 65 |three |
+--------+-------------------------------------------------+----------------+