文章目录

  • 1.JUnit单元测试
    • 1.1 JUnit 的 使用步骤
    • 1.2JUnit 的其他四个注解
  • 2.NIO 介绍
    • 2.1 阻塞与非阻塞
    • 2.2 同步与异步
  • 3.NIO-buffer类
    • 3.1介绍
    • 3.2 ByteBuffer 的三种数据添加方式
    • 3.3 ByteBuffer的容量capacity
    • 3.4ByteBuffer de 限制limit
    • 3.5ByteBuffer 的位置-position
    • 3.6 ByteBufferde 标记 mark
    • 3.7 ByteBuffer的其他方法:
  • 4 通道Chanel
    • 4.1 通道的介绍
    • 4.2模拟文件复制:
    • 4.3 FileChannel 的高效读写
    • 4.4SocketChannel和ServerSocketChannel的实现连接
    • 4.5 SocketChannel和ServerSocketChannel的实现通信

1.JUnit单元测试

单元测试:
Java中一个单元可以指一个方法,或是指某个类
测试,对一段代码的测试
Junit是一个单元测试的第三方框架

1.1 JUnit 的 使用步骤

不需要下载开发工具自带
具体案例:

package com.itcast.text01;import org.junit.Test;public class TextDemo01 {@Testpublic void test01(){Demo01 dd = new Demo01();int sum = dd.getSum(10, 20, 40);System.out.println(sum);}@Testpublic 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 {@Beforepublic void test003(){System.out.println("我在所有的@Test方法之后执行");}@Afterpublic void test001(){System.out.println("我在所有的@Test方法之后执行");}@Testpublic void test01(){Demo01 dd = new Demo01();int sum = dd.getSum(10, 20, 40);System.out.println(sum);}@Testpublic 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("通道关闭");}}
}

JAVA.SE JUnit单元测试 NIO相关推荐

  1. java中JUnit单元测试的使用方法

    package com.atguigu.java2;import java.sql.Date;import org.junit.Test;/** java中的JUnit单元测试* 步骤:(可以直接写@ ...

  2. Java中junit单元测试

    在junit单元测试中有一个Test注解,该注解下的方法相当于main方法一样,可以直接运行,而且在一个类中可以写多个Test注解,方便于对代码的测试,对于该测试方法,如果执行成功,则会有绿色对勾的提 ...

  3. java的Junit单元测试

    函数主要分为以下几类: 1.有固定返回值的.用assert 方法即可. 2.修改了状态. (1)修改了数据库中的数据.可以查询数据库(select  语句),看数据是否发生了改变. --原则上应该是用 ...

  4. idea中使用JUnit单元测试

    idea中 Java的JUnit单元测试 步骤操作:1.需要在project下新建一个文件夹,用于存放自动生成的测试.java文件 2.将这个文件夹设置为存放生成测试文件的目录 点击确定 确定后新建一 ...

  5. 【android】AS中使用Junit单元测试和Android JUnit 单元测试

    AS中使用Junit单元测试和Android JUnit 单元测试 在AndroidStudio中使用单元测试 1. 前言 在Android开发中,如果对一个简单的功能,每次修改代码都重新运行到设备中 ...

  6. Java基础学习总结(130)——使用WebSocket导致jUnit单元测试报No suitable default RequestUpgradeStrategy found错误的解决方法

    若在SpringMVC项目中使用了WebSocket,在运行jUnit单元测试时,可能会报如下错误: Caused by: java.lang.IllegalStateException: No su ...

  7. java day25【Junit单元测试 、反射 、 注解】

    第一章  Junit单元测试: * 测试分类: 1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 2. 白盒测试:需要写代码的.关注程序具体的执行流程. * Junit使用:白盒测 ...

  8. 黑马就业班(01.JavaSE Java语言基础-11.Java基础加强)——基础加强:Junit单元测试、反射、注解

       1.Junit单元测试 测试分类: 1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 2. 白盒测试:需要写代码的.关注程序具体的执行流程. Junit使用:白盒测试 步骤: ...

  9. Java之单元测试(JUnit单元测试框架)

    一.概述 单元测试就是针对最小的功能单元编写测试代码,Java程序最小的功能单元是方法,所以单元测试就是针对Java方法的测试,进而检查方法的正确性. 常规测试有什么问题? 只有一个main方法,如果 ...

最新文章

  1. 一个web左侧菜单例子
  2. php如何删除单个数组,如何在php中使用array_splice删除单个数组成员?
  3. oracle装了客户端怎么登陆账号,分享Oracle 11G Client 客户端安装步骤(图文详解)...
  4. CVE-2013-3897漏洞成因与利用分析
  5. java访问登录网页_===java怎样访问需要登录才能查看的网页????急!!===...
  6. openeim 成片的蝴蝶兰盛开在绿树之间
  7. 关于git pull机制和游戏开发热更新思考
  8. 步步为营-75-Cookie简介
  9. Linux装好MATLAB无法启动的解决办法
  10. 计算机学科 集体备课记录,信息技术学科组集体备课活动记录
  11. java基于springboot+vue学生考勤签到请假管理系统84y43
  12. otsu阈值分割算法原理_otsu(大津法阈值分割原理)
  13. 别找了,这就是你心心念念想要的年会活动抽奖软件
  14. Python+Opencv身份证号码区域提取及识别
  15. 使用VBA让Word或Excel文档窗口置顶
  16. ug12无法连接服务器系统,NX许可证错误:无法连接至许可证服务器系统。SPLM_LICENSE_SERVER错误[-15]...
  17. vmware虚拟机搭建网络拓扑教程
  18. arduino中u8g2汉字显示总结
  19. Dapp 众筹项目1 合约代码编写
  20. 来瞧一瞧unshift和push的用法及“性能“对比

热门文章

  1. python的excel-python操作Excel的几种方式
  2. 鸿蒙微信公众号报名,鸿蒙系统官网2.0报名分享
  3. 什么是中间层,什么是三层网络结构
  4. 织梦网站木马生成一个php文件夹,dede织梦程序网站安全设置防范木马侵袭
  5. FPGA中LUT设计
  6. 一句话可以彻底改变一个人的命运
  7. 子比主题最新6.8开心版
  8. 《趣味知识博文》小W与小L带你聊天式备考CDA Level Ⅰ(四)
  9. 重磅:小程序将可分享到朋友圈啦!
  10. LocalBroadcastManager已被废弃