当前位置:网站首页>Read only mode of buffer
Read only mode of buffer
2022-07-27 18:24:00 【StrugglingXuYang】
package com.tdkj.DataApi.nio;
import java.nio.ByteBuffer;
/** * @author hxy * @version 1.0 * @date 2021-03-11 10:56 */
public class ReadOnlyBuffer {
public static void main(String[] args) {
//buffer read only mode
// Create a buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
for (int i = 0; i <64 ; i++) {
byteBuffer.put((byte)i);
}
// Read
byteBuffer.flip();
// Will be buff Set to read only
ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
while (readOnlyBuffer.hasRemaining()){
//hasRemaining Determine whether the next person has any data
System.out.println(readOnlyBuffer.get()); //get Would be position The pointer automatically +1
}
//readOnlyBuffer.putInt(1);
// If it is set to read-only, it cannot be written Otherwise, the error will be reported
//java.nio.ReadOnlyBufferException
}
}
Directly modify the data in the channel
package com.tdkj.DataApi.nio;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/** * @author hxy * @version 1.0 * @date 2021-03-11 11:07 */
public class MappedByteBufferTest {
//MappedByteBuffer You can modify files directly in memory
// The operating system does not need to be copied once Memory refers to off heap memory
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("D://file.txt", "rw");
// Get the corresponding channel
FileChannel channel = randomAccessFile.getChannel();
/** * Parameters 1 FileChannel.MapMode.READ_WRITE Use read-write mode * Parameters 2 0 You can directly modify the starting position of * Parameters 3(size) 5 Is the size mapped to memory ( Not the location of the index ), namely How many bytes are mapped from the file to memory * That is, the scope that can be directly modified is 0,1,2,3,4 5 Bytes * The actual type is DirectByteBuffer **/
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE,0,20);
mappedByteBuffer.put(0,(byte)'a');
mappedByteBuffer.put(1,(byte)'b');
mappedByteBuffer.put(4,(byte)'c');
mappedByteBuffer.put(7,(byte)'c');
mappedByteBuffer.put(8,(byte)'c');
mappedByteBuffer.put(9,(byte)'c');
//20 It cannot be modified in this position That is to say 0-20 but 20 It refers to the number of bytes that can be modified Instead of Subscripts
//mappedByteBuffer.put(20,(byte)'c');
// If there is Chinese in the file Then it will also be modified But it will become garbled
randomAccessFile.close();
}
}
边栏推荐
- JRS-303用法
- Prevent SQL injection
- 2. Change color space and color detection
- 贴牌“美国制造”,国产安防设备竟被装上了美航母!
- [MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6
- Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?
- Golang customize once. When error occurs, reset it for the second time
- 超实用!阿里P9私藏的Kubernetes学习笔记,看完直呼NB
- 荣耀、小米发双十一战报:都称自己是冠军
- 图形界面编程
猜你喜欢

Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?

深度识别:论文阅读_2S-AGCN CVPR2019(基于骨架的动作识别的两流自适应图卷积网络)

深度学习-论文阅读:动作结构性图卷积网络AS-GCN

Linked list storage structure of dynamic linked list 2 stack (linkedstack Implementation)
![[learning notes] advanced version of MySQL database - index optimization, slow query, locking mechanism, etc](/img/7a/7497a73b435c3ed4fa0f3a3e908298.jpg)
[learning notes] advanced version of MySQL database - index optimization, slow query, locking mechanism, etc

动态链表2栈的链表存储结构(LinkedStack实现)

Dynamic linked list 4 one-way circular linked list (loopsingle Implementation)

Deep learning: GCN diagram classification case

解决Reids不能被其他IP访问

1. opencv图片基础操作
随机推荐
Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
Es query limit 10000 data solutions
贴牌“美国制造”,国产安防设备竟被装上了美航母!
Here comes the first 5g SOC of MediaTek! A77+g77+apu3.0, officially released on November 26!
又一个时代的终结!
Prevent SQL injection
图形界面编程
小米CC9 Pro拆解:后置五摄成本是骁龙855数倍!
深度识别:论文阅读_2S-AGCN CVPR2019(基于骨架的动作识别的两流自适应图卷积网络)
Collection! 0 basic open source data visualization platform flyfish large screen development guide
【学习笔记】Redis中有序集合zset的实现原理——跳表
《华为是谁》纪录短片集登陆BBC:曝光大量任正非不为人知经历
Marvell公布旗下Arm服务器芯片路线图,下一代性能将比ThunderX2高两倍
邮件安全运营难?Coremail携手云商店打造企业邮箱办公新生态!
Local development using LWC in salesforce
fragmentTransaction.replace第二个参数报错
Jrs-303 usage
Common commands of database 1
[learning notes] advanced version of MySQL database - index optimization, slow query, locking mechanism, etc
深度学习:GCN(图卷积神经网络)理论学习总结