4-byte and channel加强细节

1.使用同一个byteBuffer实现内容拷贝

package com.netty.bufferandchannel;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Fighting
 * @version BufferChannelReadWrite, v 0.1 2020/8/12 19:54 Fighting
 * @Content 使用一个buffer实现将一个文件中的数据拷贝到另一个文件
 */
public class BufferChannelReadWrite {
    public static void main(String[] args) throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        File file = new File("G:\\netty-workspace\\netty-test\\src\\main\\java\\com\\netty\\bufferandchannel\\demo.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel readchannel = fileInputStream.getChannel();
        readchannel.read(byteBuffer);

        System.out.println("从文件中读取的数据为:"+new String(byteBuffer.array()));

        FileOutputStream fileOutputStream = new FileOutputStream("G:\\netty-workspace\\netty-test\\src\\main\\java\\com\\netty\\bufferandchannel\\demoCopy.txt");
        FileChannel writeChannel = fileOutputStream.getChannel();

        byteBuffer.flip();

        writeChannel.write(byteBuffer);
        fileInputStream.close();
        fileOutputStream.close();
    }
}

2.byteBuffer只读

package com.netty.bufferandchannel;

import java.nio.ByteBuffer;

/**
 * @author Fighting
 * @version ByteBufferReadOnly, v 0.1 2020/8/12 22:54 Fighting
 * @Content 只读模式下的buffer只能是读取数据,不能进行写数据的操作
 * Exception in thread "main" java.nio.ReadOnlyBufferException
 */
public class ByteBufferReadOnly {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);
//        byteBuffer.put(("abc").getBytes());
//        byteBuffer.put(("abc").getBytes());
//        byteBuffer.put(("abc").getBytes());
//        byteBuffer.put(("abc").getBytes());

        for (int i = 0; i < 64; i++) {
            byteBuffer.put((byte)i);
        }
        byteBuffer.flip();
        ByteBuffer buffer = byteBuffer.asReadOnlyBuffer();

        while(byteBuffer.hasRemaining()){
            System.out.println(byteBuffer.get());
        }
        buffer.put("123".getBytes());
    }
}

3.byteBuffer存入什么类型的数据,取出的时候还需要用对应的类型取出,否则报错

package com.netty.bufferandchannel;

import java.nio.ByteBuffer;

/**
 * @author Fighting
 * @version ByteBufferTestData, v 0.1 2020/8/12 22:55 Fighting
 * @Content Bytebuffer中存入什么类型的数据,在取出的时候就按照什么类型的方式取出
 * 如果取出的时候类型不对应,则会报出BufferUnderflowException
 */
public class ByteBufferTestData {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(20);
        buffer.putInt(1);
        buffer.putDouble(12L);
        buffer.putShort((short)4);
        buffer.put("abc".getBytes());

        buffer.flip();

        System.out.println(buffer.getInt());
        System.out.println(buffer.getDouble());
        System.out.println(buffer.getShort());
        System.out.println(buffer.getDouble());
        System.out.println(buffer.get());
    }
}

4.使用byteBuffer的子类MappedByteBuffer[DirectByteBuffer]实现在操作系统级别操作数据

package com.netty.bufferandchannel;

import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Fighting
 * @version MappedByteBufferDemo, v 0.1 2020/8/12 22:52 Fighting
 * @Content 直接操作内存数据
 */
public class MappedByteBufferDemo {
    public static void main(String[] args) throws Exception{
        RandomAccessFile randomAccessFile = new RandomAccessFile("G:\\netty-workspace\\netty-test\\src\\main\\java\\com\\netty\\bufferandchannel\\demo.txt", "rw");
        //10是可以操作多少个
        MappedByteBuffer map = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 10);
        map.put(0,(byte)'M');
        map.put(9,(byte)'~');
        randomAccessFile.close();
        System.out.println("操作内存完成...");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值