1.JUnit单元测试
单元测试:
Java中一个单元可以指一个方法,或是指某个类
测试,对一段代码的测试
Junit是一个单元测试的第三方框架
1.1 JUnit 的 使用步骤
不需要下载开发工具自带
具体案例:
package com.itcast.text01;
import org.junit.Test;
public class TextDemo01 {
@Test
public void test01(){
Demo01 dd = new Demo01();
int sum = dd.getSum(10, 20, 40);
System.out.println(sum);
}
@Test
public void text02(){
Demo01 dd = new Demo01();
int sum = dd.getSum(11, 22, 33);
System.out.println(sum);
}
}
只需要加上注解
1.2JUnit 的其他四个注解
@Before该方法在所有的@Test方法执前执行
@After 该方法在所有的@Test方法后执行
@BeforeClass该方法在所有的@Test方法执行前执行
@AfterClass该方法在所有的T@est方法之后执行
package com.itcast.text01;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TextDemo01 {
@Before
public void test003(){
System.out.println("我在所有的@Test方法之后执行");
}
@After
public void test001(){
System.out.println("我在所有的@Test方法之后执行");
}
@Test
public void test01(){
Demo01 dd = new Demo01();
int sum = dd.getSum(10, 20, 40);
System.out.println(sum);
}
@Test
public void text02(){
Demo01 dd = new Demo01();
int sum = dd.getSum(11, 22, 33);
System.out.println(sum);
}
}
2.NIO 介绍
2.1 阻塞与非阻塞
阻塞完成某个任务时,任务没有结束之前,当前线程无法向下执行
非阻塞: 完成某个任务时,不需要等待任务结束,线程继续向下执行,后期再通过其他方法继续向下执行
2.2 同步与异步
同步:同步可能是阻塞的也可能时非阻塞的,
同步阻塞:
完成一个任务时,当前任务没有完成,该线程无法继续进行下一个任务
同步非阻塞:
完成某个任务不需要等待,当前线程可以继续向下执行
异步,完成某个任务时,不需要等待,当前线程会继续向下执行
,后期我们不需要写代码获取结果
BIO:传统的IO,同步阻塞IO,
NIO:同步阻塞也可以是同步非阻塞,由buffer缓冲区,Channel通道,selector选择器
NIO2:异步非阻塞状态
3.NIO-buffer类
3.1介绍
buffer缓冲区,本质上使一个数组,
buffer的一般操作步骤
写入缓冲区,把数据读取到数组中
调用flip方法,切换缓冲区的 默认读写方法
读缓冲区
调用clear、方法或者compact方法清空缓冲区已经读取的数据
buffer 的种类
ByteBuffer字节缓冲区(字节数组)
charbuffer字符缓冲区(字符数组)
DoubleBufferdouble缓冲区(小数数组)
FloatBuffer FloatBuffer(小数数组)
IntBuffer长整型数组(长整型数组)
shortBuffer短整型数组
package com.itcast.text01;
import java.nio.ByteBuffer;
public class Demo03 {
public static void main(String[] args) {
//在JVM虚拟机中创建
ByteBuffer allocate = ByteBuffer.allocate(10);
//直接和操作系统申请
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(10);
//wrap属于间接缓冲区
byte[] bytes = new byte[10];
ByteBuffer wrap = ByteBuffer.wrap(bytes);
}
}
3.2 ByteBuffer 的三种数据添加方式
package com.itcast.text01;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class Demo02 {
public static void main(String[] args) {
//bytebuffer的三种数据添加方式
ByteBuffer allocate = ByteBuffer.allocate(10);
System.out.println(allocate);
System.out.println(Arrays.toString(allocate.array()));
allocate.put((byte) 10);
allocate.put((byte) 20);
allocate.put((byte) 30);
allocate.put((byte) 40);
System.out.println(Arrays.toString(allocate.array()));
//java.nio.HeapByteBuffer[pos=0 lim=10 cap=10]
//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
//[10, 20, 30, 40, 0, 0, 0, 0, 0, 0]
byte[] bs = {20, 40, 60};
allocate.put(bs, 1, 2);
System.out.println(Arrays.toString(allocate.array()));
//[10, 20, 30, 40, 40, 60, 0, 0, 0, 0]
}
}
3.3 ByteBuffer的容量capacity
什么是容器,指的是buffer最多包含多少个数,并且buffer一旦创建,容量无法更改
package com.itcast.text01;
import java.nio.ByteBuffer;
public class demo04 {
public static void main(String[] args) {
ByteBuffer allocate = ByteBuffer.allocate(10);
int capacity = allocate.capacity();
System.out.println("容量为:" + capacity);
}
}
3.4ByteBuffer de 限制limit
什么是限制,指的是第一个不能操作的元素的索引,0-capcity
限制作用:相当于认为修改,缓冲区的大小,实际上缓冲区的大小没有发生改变
package com.itcast.text01;
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.jar.Attributes;
public class demo05 {
public static void main(String[] args) {
ByteBuffer allocate = ByteBuffer.allocate(10);
System.out.println(Arrays.toString(allocate.array()));
System.out.println("缓冲区限制:" + allocate.limit());
Buffer limit = allocate.limit(3);
System.out.println(Arrays.toString(allocate.array()));
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
System.out.println(Arrays.toString(allocate.array()));
}
}
3.5ByteBuffer 的位置-position
什么是位置:,将数据读或者写入的索引,位置范围(0-capacity/limit)
package com.itcast.text01;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class demo06 {
public static void main(String[] args) {
ByteBuffer allocate = ByteBuffer.allocate(10);
System.out.println(allocate.array());
System.out.println(allocate.capacity());
System.out.println(allocate.limit());
System.out.println(allocate.position());
//添加数据
allocate.put((byte) 20);
allocate.put((byte) 20);
allocate.put((byte) 20);
allocate.put((byte) 20);
allocate.put((byte) 20);
allocate.put((byte) 20);
System.out.println(Arrays.toString(allocate.array()));
System.out.println(allocate.capacity());
System.out.println(allocate.position());
System.out.println(allocate.limit());
System.out.println("修改当前位置为2");
allocate.position(2);
System.out.println(Arrays.toString(allocate.array()));
System.out.println(allocate.capacity());
System.out.println(allocate.limit());
System.out.println(allocate.position());
}
}
3.6 ByteBufferde 标记 mark
package com.itcast.text01;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class Demo07 {
public static void main(String[] args) {
ByteBuffer allocate = ByteBuffer.allocate(15);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.put((byte) 10);
allocate.mark();
allocate.put((byte) 20);
allocate.put((byte) 20);
allocate.put((byte) 20);
allocate.put((byte) 20);
System.out.println(Arrays.toString(allocate.array()));
allocate.reset();
allocate.put((byte) 30);
allocate.put((byte) 30);
allocate.put((byte) 30);
allocate.put((byte) 30);
System.out.println(Arrays.toString(allocate.array()));
//[10, 10, 10, 10, 10, 20, 20, 20, 20, 0, 0, 0, 0, 0, 0]
//[10, 10, 10, 10, 10, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0]
}
}
3.7 ByteBuffer的其他方法:
remaining(); 获取position与limit之间的元素
isReadOnly() 获取当缓冲区是否为只读
isDirect();获取当前缓冲区是否为只读缓冲区
public Buffer clear();清空缓冲区
4 通道Chanel
4.1 通道的介绍
什么是Channel:Channel 是一个读写的数据类,与IO相似,最大的区别为IO有Input OutPut之分
通道Channel的分类
File Channel文件通道,读写文件
DATa gram Channel UDP协议通道,通过UDP收发数据
SocketChannel TCP客户端通道,给客户端读写数据
Server Socket TCP协议中服务端通道
4.2模拟文件复制:
package com.itcast.text02;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Demo01 {
public static void main(String[] args) throws IOException {
File srcFile = new File("copy01.png");
File destFile = new File("copy02.png");
FileInputStream fileInputStream = new FileInputStream(srcFile);
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
FileChannel channel = fileInputStream.getChannel();
FileChannel channel1 = fileOutputStream.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(1024);
int len =0;
while ((len = channel.read(allocate)) != -1) {
allocate.flip();
channel1.write(allocate);
allocate.clear();
}
channel.close();
channel1.close();
fileInputStream.close();
fileOutputStream.close();
}
}
4.3 FileChannel 的高效读写
案例代码:
package com.itcast.text02;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Demo02 {
public static void main(String[] args) throws IOException {
RandomAccessFile srcFile = new RandomAccessFile("E:\\daima\\JAVASE_advanced\\day01.zip", "r");
RandomAccessFile destFile = new RandomAccessFile("copy01.zip", "rw");
FileChannel inChannel = srcFile.getChannel();
FileChannel destChannel = destFile.getChannel();
int size = (int) inChannel.size();
MappedByteBuffer inmap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer outmap = destChannel.map(FileChannel.MapMode.READ_WRITE, 0, size);
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
byte b = inmap.get(i);
outmap.put(i, b);
}
long end = System.currentTimeMillis();
System.out.println("时间" + (end - start));
destChannel.close();
inChannel.close();
}
}
4.4SocketChannel和ServerSocketChannel的实现连接
代码实现:
package com.itcast.text02;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Demo04 {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8888));
System.out.println("服务器创建成功");
SocketChannel channel = serverSocketChannel.accept();
System.out.println("接收客户端请求");
channel.close();
serverSocketChannel.close();
}
}
package com.itcast.text02;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
public class Demo05 {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("127.0.0.1", 8888));
System.out.println("向服务器发送请求");
socketChannel.close();
}
}
4.5 SocketChannel和ServerSocketChannel的实现通信
代码实现:
package com.itcast.text02;
import com.sun.scenario.animation.shared.ClipEnvelope;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Demo06 {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(9999));
System.out.println("服务器启动成功");
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("有客户端连接");
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int len = socketChannel.read(byteBuffer);
byteBuffer.flip();
String s = new String(byteBuffer.array(),0, len);
System.out.println("客户端说:" + s);
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("hello我是服务器".getBytes());
buffer.flip();
socketChannel.write(buffer);
socketChannel.close();
serverSocketChannel.close();
}
}
package com.itcast.text02;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Demo07 {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
boolean b = socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
if (b) {
System.out.println("服务器连接成功");
ByteBuffer byteBuffer = ByteBuffer.wrap("hello,我是客户端".getBytes());
socketChannel.write(byteBuffer);
ByteBuffer buffer = ByteBuffer.allocate(1024);
int len = socketChannel.read(buffer);
buffer.flip();
System.out.println(new String(buffer.array(),0,len));
socketChannel.close();
System.out.println("通道关闭");
}
}
}