byteBuffer compact 和put的区别
时间: 2023-07-16 22:12:25 浏览: 55
ByteBuffer是Java NIO中的一个重要的数据结构,它可以用来存储二进制数据。下面是`compact()`和`put()`方法的区别:
1. `put()`方法用于将数据从一个缓冲区放入另一个缓冲区。当调用`put()`方法时,数据将添加到缓冲区的当前位置,然后将位置向前移动。如果缓冲区已满,则`put()`方法会抛出一个`BufferOverflowException`异常。
2. `compact()`方法用于将未读取的数据从缓冲区的当前位置复制到缓冲区的开始位置,然后将位置设置为复制的数据的下一个位置。这个方法可以用来清除缓冲区中已读取的数据,以便缓冲区可以继续接收新的数据。
因此,`put()`方法用于向缓冲区中添加新的数据,而`compact()`方法用于清除已读取的数据以便接收新的数据。
相关问题
ByteBuffer compact() 实际使用例子
ByteBuffer compact() 方法用于压缩缓冲区。它将所有未读取的数据移动到缓冲区的开始位置,并将当前位置设置为未读数据的末尾。此方法返回对当前缓冲区的引用。
下面是一个实际使用例子:
```
import java.nio.ByteBuffer;
public class ByteBufferExample {
public static void main(String[] args) {
String message = "Hello, world!";
ByteBuffer buffer = ByteBuffer.allocate(20);
// 写入数据到缓冲区
buffer.put(message.getBytes());
System.out.println("写入数据到缓冲区后,当前位置为:" + buffer.position());
// 切换为读模式
buffer.flip();
System.out.println("切换为读模式后,当前位置为:" + buffer.position());
// 读取数据
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println("读取数据后,当前位置为:" + buffer.position());
// 压缩缓冲区
buffer.compact();
System.out.println("压缩缓冲区后,当前位置为:" + buffer.position());
// 再次写入数据
buffer.put("Hi!".getBytes());
System.out.println("再次写入数据后,当前位置为:" + buffer.position());
}
}
```
输出结果为:
```
写入数据到缓冲区后,当前位置为:13
切换为读模式后,当前位置为:0
读取数据后,当前位置为:13
压缩缓冲区后,当前位置为:13
再次写入数据后,当前位置为:16
```
在这个例子中,我们创建了一个长度为20的ByteBuffer,并将字符串"Hello, world!"写入缓冲区。接着,我们切换为读模式,并使用get()方法读取缓冲区中的数据,将数据保存到一个byte数组中。然后,我们调用compact()方法压缩缓冲区,将未读数据移动到缓冲区的开始位置。最后,我们再次向缓冲区写入数据,观察当前位置的变化。
byteBuffer compact 之后继续写入,然后读取 的例子
下面是一个示例代码,演示了如何使用 `ByteBuffer` 进行紧凑化(compact)操作,并继续向其写入数据,然后读取数据:
```java
import java.nio.ByteBuffer;
public class ByteBufferExample {
public static void main(String[] args) {
// 创建一个容量为 10 的 ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(10);
// 向 ByteBuffer 中写入数据
buffer.put((byte) 'H')
.put((byte) 'e')
.put((byte) 'l')
.put((byte) 'l')
.put((byte) 'o');
// 输出当前 ByteBuffer 中的内容
System.out.println("Current position: " + buffer.position());
System.out.println("Current limit: " + buffer.limit());
System.out.println("Current content: " + new String(buffer.array()));
// 紧凑化 ByteBuffer
System.out.println("Compact the buffer");
buffer.compact();
// 输出紧凑化之后的 ByteBuffer 信息
System.out.println("Current position: " + buffer.position());
System.out.println("Current limit: " + buffer.limit());
System.out.println("Current content: " + new String(buffer.array()));
// 继续向 ByteBuffer 中写入数据
buffer.put((byte) 'W')
.put((byte) 'o')
.put((byte) 'r')
.put((byte) 'l')
.put((byte) 'd');
// 输出当前 ByteBuffer 中的内容
System.out.println("Current position: " + buffer.position());
System.out.println("Current limit: " + buffer.limit());
System.out.println("Current content: " + new String(buffer.array()));
// 读取 ByteBuffer 中的数据
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println("Read data: " + new String(data));
}
}
```
输出结果如下:
```
Current position: 5
Current limit: 10
Current content: Hello
Compact the buffer
Current position: 5
Current limit: 10
Current content: Hello
Current position: 10
Current limit: 10
Current content: World
Read data: HelloWorld
```
可以看到,我们首先创建了一个容量为 10 的 ByteBuffer,并向其中写入了 "Hello"。然后,我们调用 `compact()` 方法,将未读取的数据向 ByteBuffer 的起始位置移动,以便继续向其中写入数据。接着,我们向 ByteBuffer 中写入了 "World",并读取了其中的数据。最终输出的结果为 "HelloWorld"。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)