缓冲区的使用
Java IO和NIO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的。 Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方。此外,它不能前后移动流中的数据。如果需要前后移动从流中读取的数据,需要先将它缓存到一个缓冲区。 Java NIO的缓冲导向方法略有不同。数据读取到一个它稍后处理的缓冲区,需要时可在缓冲区中前后移动。这就增加了处理过程中的灵活性。但是,还需要检查是否该缓冲区中包含所有您需要处理的数据。而且,需确保当更多的数据读入缓冲区时,不要覆盖缓冲区里尚未处理的数据。
Buffer 常用操作
创建并使用Buffer
- 创建Buffer的4中方式
- ByteBuffer buffer = ByteBuffer.allocate(100);
- ByteBuffer buffer2 = ByteBuffer.allocateDirect(100);
- byte bs[]= new byte[100];ByteBuffer buffer3=ByteBuffer.wrap(bs);
- ByteBuffer buffer4=ByteBuffer.wrap(bs,0,100);
- 使用Buffer
package cn.wyj.learn.nio;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
import java.util.Arrays;
@Slf4j
public class BufferExample2 {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.limit(8);
buffer.put(new byte[]{0, 1, 2, 3});
log.info("1BufferContent :{}", Arrays.toString(buffer.array()));
buffer.mark();
buffer.put(new byte[]{4, 5, 6});
log.info("2BufferContent :{}", Arrays.toString(buffer.array()));
// 指针回退到标记点
buffer.reset();
buffer.put(new byte[]{7, 8, 9});
log.info("3BufferContent :{}", Arrays.toString(buffer.array()));
int remaining = buffer.remaining();
log.info("remaining:{}", remaining);
log.info("4BufferContent :{}", Arrays.toString(buffer.array()));
try {
buffer.put((byte) 10);
//超出limit
buffer.put((byte) 11);
} catch (Exception e) {
log.error("e: ", e);
}
log.info("5BufferContent :{}", Arrays.toString(buffer.array()));
buffer.limit(9);
log.info("info:{}", buffer);
// mark =-1, limit = position,position = 0
buffer.flip();
log.info("info:{}", buffer);
log.info("6BufferContent :{}", Arrays.toString(buffer.array()));
for (int i = 0; i < buffer.limit(); i++) {
log.info("position{},content{}", buffer.position(), buffer.get());
}
}
}
执行结果
16:15:46.168 [main] INFO cn.wyj.learn.nio.BufferExample2 - 1BufferContent :[0, 1, 2, 3, 0, 0, 0, 0, 0, 0]
16:15:46.173 [main] INFO cn.wyj.learn.nio.BufferExample2 - 2BufferContent :[0, 1, 2, 3, 4, 5, 6, 0, 0, 0]
16:15:46.174 [main] INFO cn.wyj.learn.nio.BufferExample2 - 3BufferContent :[0, 1, 2, 3, 7, 8, 9, 0, 0, 0]
16:15:46.174 [main] INFO cn.wyj.learn.nio.BufferExample2 - remaining:1
16:15:46.174 [main] INFO cn.wyj.learn.nio.BufferExample2 - 4BufferContent :[0, 1, 2, 3, 7, 8, 9, 0, 0, 0]
16:15:46.176 [main] ERROR cn.wyj.learn.nio.BufferExample2 - e:
java.nio.BufferOverflowException: null
at java.nio.Buffer.nextPutIndex(Buffer.java:521)
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:169)
at cn.wyj.learn.nio.BufferExample2.main(BufferExample2.java:27)
16:15:46.176 [main] INFO cn.wyj.learn.nio.BufferExample2 - 5BufferContent :[0, 1, 2, 3, 7, 8, 9, 10, 0, 0]
16:15:46.176 [main] INFO cn.wyj.learn.nio.BufferExample2 - info:java.nio.HeapByteBuffer[pos=8 lim=9 cap=10]
16:15:46.176 [main] INFO cn.wyj.learn.nio.BufferExample2 - info:java.nio.HeapByteBuffer[pos=0 lim=8 cap=10]
16:15:46.176 [main] INFO cn.wyj.learn.nio.BufferExample2 - 6BufferContent :[0, 1, 2, 3, 7, 8, 9, 10, 0, 0]
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position0,content0
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position1,content1
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position2,content2
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position3,content3
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position4,content7
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position5,content8
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position6,content9
16:15:46.177 [main] INFO cn.wyj.learn.nio.BufferExample2 - position7,content10