学习目标
- 能够使用Junit进行单元测试
1.在当前模块下新建lib文件夹,拷贝junit的jar包,把jar包添加到图书馆中
2.在要执行的方法上添加一个@Test注解
3.点击方法左边的绿色三角或者右键选择方法,选择run 方法名称运行方法
- 能够说出阻塞和非阻塞的概念
阻塞:等待结果,什么事也不能做
非阻塞:可以做别的事情
- 能够说出同步和异步的概念
同步:主动获取结果
异步:等待对方通知结果
- 能够创建和使用ByteBuffer
获取对象的方式:
- public static ByteBuffer allocate(int capacity):使用一个“容量”来创建一个“间接字节缓存区”——程序的“堆”空间中创建。
- public static ByteBuffer allocateDirect(int capacity):使用一个“容量”来创建一个“直接字节缓存区”——系统内存。
- public static ByteBuffer wrap(byte[] byteArray):使用一个“byte[]数组”创建一个“间接字节缓存区”。
成员方法:
向ByteBuffer添加数据
- public ByteBuffer put(byte b):向当前可用位置添加数据。
- public ByteBuffer put(byte[] byteArray):向当前可用位置添加一个byte[]数组
- public ByteBuffer put(byte[] byteArray,int offset,int len):添加一个byte[]数组的一部分
- byte[] array() 获取缓冲区中包含的字节数组
- int capacity() 返回此缓冲区的容量。
- public int limit():获取此缓冲区的限制。
- public Buffer limit(int newLimit):设置此缓冲区的限制。
- public int position():获取当前可写入位置索引。
- public Buffer position(int p):更改当前可写入位置索引。
- public Buffer mark():设置此缓冲区的标记为当前的position位置。
- public Buffer clear():还原缓冲区的状态。
- 将position设置为:0
- 将限制limit设置为容量capacity;
- 丢弃标记mark。
- public Buffer flip():缩小limit的范围。 获取读取的有效数据0到position之间的数据
- 将limit设置为当前position位置;
- 将当前position位置设置为0;
- 丢弃标记。
- 能够使用MappedByteBuffer实现高效读写
1.创建读取文件的RandomAccessFile对象,构造方法中封装要读取的数据源和只读模式
2.创建写入文件的RandomAccessFile对象,构造方法中封装要写入的目的地和读写模式
3.使用读取文件的RandomAccessFile对象中的方法getChannel,获取读取文件的FileChannel
4.使用写入文件的RandomAccessFile对象中的方法getChannel,获取写入文件的FileChannel
5.使用读取文件的FileChannel中的方法size,获取要读取文件的大小(字节)
6.使用读取文件的FileChannel中的方法map,创建读取文件的直接缓冲区MappedByteBuffer
7.使用写入文件的FileChannel中的方法map,创建写入文件的直接缓冲区MappedByteBuffer
8.创建for循环,循环size次
9.使用读取文件的直接缓冲区MappedByteBuffer中的方法get,读取指定索引处的字节
10.使用写入文件的直接缓冲区MappedByteBuffer中的方法put,把读取到的字节写入到指定索引处
11.释放资源
package com.itheima.demo03FileChannel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Demo02FileChannel {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
RandomAccessFile inRAF = new RandomAccessFile("c:\\748m.rar","r");
RandomAccessFile outRAF = new RandomAccessFile("d:\\748m.rar","rw");
FileChannel inRAFChannel = inRAF.getChannel();
FileChannel outRAFChannel = outRAF.getChannel();
long size = inRAFChannel.size();
System.out.println(size);
MappedByteBuffer inMap = inRAFChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer outMap = outRAFChannel.map(FileChannel.MapMode.READ_WRITE, 0, size);
for (int i = 0; i < size; i++) {
byte b = inMap.get(i);
outMap.put(i,b);
}
outRAFChannel.close();
inRAFChannel.close();
outRAF.close();
inRAF.close();
long e = System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
}
}
package com.itheima.demo03FileChannel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Demo03FileChannel {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
RandomAccessFile inRAF = new RandomAccessFile("c:\\2g.rar","r");
RandomAccessFile outRAF = new RandomAccessFile("d:\\2g.rar","rw");
FileChannel inRAFChannel = inRAF.getChannel();
FileChannel outRAFChannel = outRAF.getChannel();
long size = inRAFChannel.size();
System.out.println(size);
long count = 1;
long startIndex = 0;
long copySize = size;
long everSize = 512*1024*1024;
if(size>everSize){
count = size%everSize == 0 ? size/everSize : size/everSize+1;
copySize=everSize;
}
for (int i = 0; i < count; i++) {
MappedByteBuffer inMap = inRAFChannel.map(FileChannel.MapMode.READ_ONLY, startIndex, copySize);
MappedByteBuffer outMap = outRAFChannel.map(FileChannel.MapMode.READ_WRITE, startIndex, copySize);
System.out.println("每块文件的开始索引:"+startIndex);
System.out.println("每次复制文件的大小:"+copySize);
System.out.println("--------------------------------------");
for (int j = 0; j < copySize; j++) {
byte b = inMap.get(j);
outMap.put(j,b);
}
startIndex += copySize;
copySize = size-startIndex>everSize ? everSize : size-startIndex;
}
outRAFChannel.close();
inRAFChannel.close();
outRAF.close();
inRAF.close();
long e = System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
}
}