diff --git a/README.md b/README.md index 1511b81..1c21ea6 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,8 @@ 如果您觉得该项目对您有帮助,请扫描下方二维码对我进行鼓励,以便我更好的维护和更新,谢谢支持! -![支付宝](http://brianway.github.io/assets/images/alipay_small.png) -![微信](http://brianway.github.io/assets/images/wechatpay_small.png) - +![支付宝](https://brianway.github.io/img/alipay_small.png) +![微信](https://brianway.github.io/img/wechatpay_small.png) # TODO diff --git "a/blogs/img/javaSE_\344\273\243\347\220\206\346\236\266\346\236\204\345\233\276.png" "b/blogs/img/javaSE_\344\273\243\347\220\206\346\236\266\346\236\204\345\233\276.png" new file mode 100644 index 0000000..ae378bb Binary files /dev/null and "b/blogs/img/javaSE_\344\273\243\347\220\206\346\236\266\346\236\204\345\233\276.png" differ diff --git "a/blogs/img/javaSE_\345\212\250\346\200\201\344\273\243\347\220\206\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206\345\233\276.jpg" "b/blogs/img/javaSE_\345\212\250\346\200\201\344\273\243\347\220\206\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206\345\233\276.jpg" new file mode 100644 index 0000000..7b2819d Binary files /dev/null and "b/blogs/img/javaSE_\345\212\250\346\200\201\344\273\243\347\220\206\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206\345\233\276.jpg" differ diff --git "a/blogs/img/javaSE_\346\263\233\345\236\213\350\260\203\350\257\225\346\210\252\345\233\276-1.png" "b/blogs/img/javaSE_\346\263\233\345\236\213\350\260\203\350\257\225\346\210\252\345\233\276-1.png" new file mode 100644 index 0000000..365eb5f Binary files /dev/null and "b/blogs/img/javaSE_\346\263\233\345\236\213\350\260\203\350\257\225\346\210\252\345\233\276-1.png" differ diff --git "a/blogs/img/javaSE_\347\261\273\345\212\240\350\275\275\345\231\250\347\273\223\346\236\204\345\233\276.png" "b/blogs/img/javaSE_\347\261\273\345\212\240\350\275\275\345\231\250\347\273\223\346\236\204\345\233\276.png" new file mode 100644 index 0000000..5953249 Binary files /dev/null and "b/blogs/img/javaSE_\347\261\273\345\212\240\350\275\275\345\231\250\347\273\223\346\236\204\345\233\276.png" differ diff --git a/blogs/img/javaweb_HttpResponseStatus.png b/blogs/img/javaweb_HttpResponseStatus.png new file mode 100644 index 0000000..d9e83d2 Binary files /dev/null and b/blogs/img/javaweb_HttpResponseStatus.png differ diff --git a/blogs/img/javaweb_servlet-lifecycle.png b/blogs/img/javaweb_servlet-lifecycle.png new file mode 100644 index 0000000..ba13f14 Binary files /dev/null and b/blogs/img/javaweb_servlet-lifecycle.png differ diff --git "a/blogs/img/javaweb_servlet-url\345\214\271\351\205\215.png" "b/blogs/img/javaweb_servlet-url\345\214\271\351\205\215.png" new file mode 100644 index 0000000..c561a3b Binary files /dev/null and "b/blogs/img/javaweb_servlet-url\345\214\271\351\205\215.png" differ diff --git "a/blogs/img/javaweb_tomcat\344\275\223\347\263\273\347\273\223\346\236\204.png" "b/blogs/img/javaweb_tomcat\344\275\223\347\263\273\347\273\223\346\236\204.png" new file mode 100644 index 0000000..fd0037e Binary files /dev/null and "b/blogs/img/javaweb_tomcat\344\275\223\347\263\273\347\273\223\346\236\204.png" differ diff --git "a/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(2)-\346\263\233\345\236\213.md" "b/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(2)-\346\263\233\345\236\213.md" index 974faa5..4397bdc 100644 --- "a/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(2)-\346\263\233\345\236\213.md" +++ "b/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(2)-\346\263\233\345\236\213.md" @@ -177,7 +177,7 @@ Error:(17, 29) java: 不兼容的类型: 推断类型不符合上限 ``` 但是有一点没搞清楚,我在IDEA里面单步调试,发现结果如下图: -![泛型调试截图-1](http://7xph6d.com1.z0.glb.clouddn.com/javaSE_%E6%B3%9B%E5%9E%8B%E8%B0%83%E8%AF%95%E6%88%AA%E5%9B%BE-1.png) +![泛型调试截图-1](/blogs/img/javaSE_%E6%B3%9B%E5%9E%8B%E8%B0%83%E8%AF%95%E6%88%AA%E5%9B%BE-1.png) 不知道b为什么是Double类型的(但直接`Double b`接收返回值会编译报错)。不知道跟IDE有没有关系,是不是IDE在debug时会显示这个对象最精确的类型? diff --git "a/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(3)-\347\261\273\345\212\240\350\275\275\345\231\250.md" "b/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(3)-\347\261\273\345\212\240\350\275\275\345\231\250.md" index ddc2ad6..e66d0ee 100644 --- "a/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(3)-\347\261\273\345\212\240\350\275\275\345\231\250.md" +++ "b/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(3)-\347\261\273\345\212\240\350\275\275\345\231\250.md" @@ -62,7 +62,7 @@ Exception in thread "main" java.lang.NullPointerException ## 类加载器的委托机制 类加载器的树状图 -![类加载器](http://7xph6d.com1.z0.glb.clouddn.com/javaSE_%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8%E7%BB%93%E6%9E%84%E5%9B%BE.png) +![类加载器](/blogs/img/javaSE_%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8%E7%BB%93%E6%9E%84%E5%9B%BE.png) 一般加载类的顺序: diff --git "a/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(4)-\344\273\243\347\220\206.md" "b/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(4)-\344\273\243\347\220\206.md" index baeb6fc..706c592 100644 --- "a/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(4)-\344\273\243\347\220\206.md" +++ "b/blogs/javase/java\345\237\272\347\241\200\345\267\251\345\233\272\347\254\224\350\256\260(4)-\344\273\243\347\220\206.md" @@ -26,7 +26,7 @@ - 静态代理:由程序员创建代理类或特定工具自动生成源代码再对其编译,也就是说在程序运行前代理类的.class文件就已经存在。 - 动态代理:在程序运行时运用反射机制动态创建生成。 -![代理架构图](http://7xph6d.com1.z0.glb.clouddn.com/javaSE_%E4%BB%A3%E7%90%86%E6%9E%B6%E6%9E%84%E5%9B%BE.png) +![代理架构图](/blogs/img/javaSE_%E4%BB%A3%E7%90%86%E6%9E%B6%E6%9E%84%E5%9B%BE.png) *紫色箭头代表类的继承关系,红色连线表示调用关系* @@ -241,7 +241,7 @@ com.sun.proxy.$Proxy0 代理类创建时需要传入一个InvocationHandler对象,client调用代理类,代理类的相应方法调用InvocationHandler的的invoke方法,InvocationHandler的invoke方法(可在其中加入日志记录、时间统计等附加功能)再找目标类的相应方法。 -![动态代理的工作原理图](http://7xph6d.com1.z0.glb.clouddn.com/javaSE_%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%9B%BE.jpg) +![动态代理的工作原理图](/blogs/img/javaSE_%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%9B%BE.jpg) ### 面向切面编程 diff --git "a/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(1)-Tomcat.md" "b/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(1)-Tomcat.md" index cf97372..68ab208 100644 --- "a/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(1)-Tomcat.md" +++ "b/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(1)-Tomcat.md" @@ -91,7 +91,7 @@ mail---------------------------Web应用所在目录 ## Tomcat体系结构 -![Tomcat体系结构](http://7xph6d.com1.z0.glb.clouddn.com/javaweb_tomcat%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84.png) +![Tomcat体系结构](/blogs/img/javaweb_tomcat%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84.png) diff --git "a/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(2)-http\345\205\245\351\227\250.md" "b/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(2)-http\345\205\245\351\227\250.md" index 86cce6a..64562c8 100644 --- "a/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(2)-http\345\205\245\351\227\250.md" +++ "b/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(2)-http\345\205\245\351\227\250.md" @@ -48,7 +48,7 @@ ### 响应状态行 -![HTTP响应状态码简表](http://7xph6d.com1.z0.glb.clouddn.com/javaweb_HttpResponseStatus.png) +![HTTP响应状态码简表](/blogs/img/javaweb_HttpResponseStatus.png) 详情可参考 diff --git "a/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(3)-Servlet.md" "b/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(3)-Servlet.md" index 06c051f..e43df2f 100644 --- "a/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(3)-Servlet.md" +++ "b/blogs/javaweb/javaweb\345\205\245\351\227\250\347\254\224\350\256\260(3)-Servlet.md" @@ -69,7 +69,7 @@ public class FirstServlet extends GenericServlet{ 时序图 -![servlet的调用过程和生命周期](http://7xph6d.com1.z0.glb.clouddn.com/javaweb_servlet-lifecycle.png) +![servlet的调用过程和生命周期](/blogs/img/javaweb_servlet-lifecycle.png) ## servlet开发的一些细节 @@ -79,7 +79,7 @@ public class FirstServlet extends GenericServlet{ - **通配符**:``的``可以使用通配符,两种固定格式:`*.扩展名`;以`/`开头,以`/*`结尾 -![javaweb_servlet-url匹配.png](http://7xph6d.com1.z0.glb.clouddn.com/javaweb_servlet-url%E5%8C%B9%E9%85%8D.png) +![javaweb_servlet-url匹配.png](/blogs/img/javaweb_servlet-url%E5%8C%B9%E9%85%8D.png) - **对象**:servlet由servlet引擎调用,不能独立运行。客户端多次请求,服务器只创建一个servlet实例,之后驻留内存中继续服务直至web容器退出才销毁它。 - **请求**:服务器针对客户端的每一次请求都会创建新的`request`和`response`对象(它们的生命周期很短),传给`service`方法。 diff --git a/java-io/pom.xml b/java-io/pom.xml index d62726d..048ab63 100644 --- a/java-io/pom.xml +++ b/java-io/pom.xml @@ -11,4 +11,10 @@ java-io + + + junit + junit + + \ No newline at end of file diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/BufferToText.java b/java-io/src/main/java/com/brianway/learning/java/nio/example/BufferToText.java similarity index 97% rename from java-io/src/main/java/com/brianway/learning/java/nio/BufferToText.java rename to java-io/src/main/java/com/brianway/learning/java/nio/example/BufferToText.java index 6d9c5d7..718fe64 100755 Binary files a/java-io/src/main/java/com/brianway/learning/java/nio/BufferToText.java and b/java-io/src/main/java/com/brianway/learning/java/nio/example/BufferToText.java differ diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/ChannelCopy.java b/java-io/src/main/java/com/brianway/learning/java/nio/example/ChannelCopy.java similarity index 96% rename from java-io/src/main/java/com/brianway/learning/java/nio/ChannelCopy.java rename to java-io/src/main/java/com/brianway/learning/java/nio/example/ChannelCopy.java index 8054ebc..f439a48 100644 --- a/java-io/src/main/java/com/brianway/learning/java/nio/ChannelCopy.java +++ b/java-io/src/main/java/com/brianway/learning/java/nio/example/ChannelCopy.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.nio; +package com.brianway.learning.java.nio.example; import java.io.FileInputStream; import java.io.FileOutputStream; diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/Endians.java b/java-io/src/main/java/com/brianway/learning/java/nio/example/Endians.java similarity index 95% rename from java-io/src/main/java/com/brianway/learning/java/nio/Endians.java rename to java-io/src/main/java/com/brianway/learning/java/nio/example/Endians.java index f7b2404..8cd74be 100644 --- a/java-io/src/main/java/com/brianway/learning/java/nio/Endians.java +++ b/java-io/src/main/java/com/brianway/learning/java/nio/example/Endians.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.nio; +package com.brianway.learning.java.nio.example; import java.nio.ByteBuffer; import java.nio.ByteOrder; diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/GetChannel.java b/java-io/src/main/java/com/brianway/learning/java/nio/example/GetChannel.java similarity index 96% rename from java-io/src/main/java/com/brianway/learning/java/nio/GetChannel.java rename to java-io/src/main/java/com/brianway/learning/java/nio/example/GetChannel.java index 9a23798..4a0f22c 100644 --- a/java-io/src/main/java/com/brianway/learning/java/nio/GetChannel.java +++ b/java-io/src/main/java/com/brianway/learning/java/nio/example/GetChannel.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.nio; +package com.brianway.learning.java.nio.example; import java.io.FileInputStream; import java.io.FileOutputStream; diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/ViewBuffers.java b/java-io/src/main/java/com/brianway/learning/java/nio/example/ViewBuffers.java similarity index 94% rename from java-io/src/main/java/com/brianway/learning/java/nio/ViewBuffers.java rename to java-io/src/main/java/com/brianway/learning/java/nio/example/ViewBuffers.java index f95b2a5..ba703dd 100755 --- a/java-io/src/main/java/com/brianway/learning/java/nio/ViewBuffers.java +++ b/java-io/src/main/java/com/brianway/learning/java/nio/example/ViewBuffers.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.nio;//: io/ViewBuffers.java +package com.brianway.learning.java.nio.example;//: io/ViewBuffers.java import java.nio.ByteBuffer; import java.nio.CharBuffer; diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/BufferDemo.java b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/BufferDemo.java new file mode 100644 index 0000000..9eb1061 --- /dev/null +++ b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/BufferDemo.java @@ -0,0 +1,41 @@ +package com.brianway.learning.java.nio.tutorial; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +/** + * 使用Buffer的例子 + * + * @auther brian + * @since 2019/6/18 00:26 + */ +public class BufferDemo { + public static void main(String[] args) throws IOException { + String path = BufferDemo.class.getResource("/").getPath() + "buffer-demo.txt"; + RandomAccessFile aFile = new RandomAccessFile(path, "rw"); + FileChannel inChannel = aFile.getChannel(); + + //create buffer with capacity of 48 bytes + ByteBuffer buf = ByteBuffer.allocate(48); + + //read into buffer. + int bytesRead = inChannel.read(buf); + while (bytesRead != -1) { + + //make buffer ready for read + buf.flip(); + while (buf.hasRemaining()) { + // read 1 byte at a time + System.out.print((char) buf.get()); + } + + //make buffer ready for writing + // System.out.println("------"); + buf.clear(); + bytesRead = inChannel.read(buf); + } + aFile.close(); + } +} diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/ChannelDemo.java b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/ChannelDemo.java new file mode 100644 index 0000000..18adc75 --- /dev/null +++ b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/ChannelDemo.java @@ -0,0 +1,43 @@ +package com.brianway.learning.java.nio.tutorial; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +/** + * 基本的 Channel 示例 + * 一个使用FileChannel读取数据到Buffer中的示例 + * + * @auther brian + * @since 2019/6/17 23:41 + */ +public class ChannelDemo { + public static void main(String[] args) { + try { + String path = ChannelDemo.class.getResource("/").getPath() + "channel-demo.txt"; + RandomAccessFile aFile = new RandomAccessFile(path, "rw"); + FileChannel inChannel = aFile.getChannel(); + // 该值小于文件内容的长度时,结果不同 + int bufferSize = 48; + ByteBuffer buf = ByteBuffer.allocate(bufferSize); + + int bytesRead = inChannel.read(buf); + while (bytesRead != -1) { + + System.out.println("Read " + bytesRead); + buf.flip(); + + while (buf.hasRemaining()) { + System.out.print((char) buf.get()); + } + + buf.clear(); + bytesRead = inChannel.read(buf); + } + aFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java new file mode 100644 index 0000000..99bf0a8 --- /dev/null +++ b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java @@ -0,0 +1,64 @@ +package com.brianway.learning.java.nio.tutorial; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.util.Iterator; +import java.util.Set; + +/** + * Full Selector Example + * 启动后,浏览器输入 localhost:9999 + * + * @auther brian + * @since 2019/6/24 22:29 + */ +public class SelectorDemo { + + public static void main(String[] args) throws IOException { + Selector selector = Selector.open(); + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.socket().bind(new InetSocketAddress(9999)); + serverSocketChannel.configureBlocking(false); + + // SelectionKey key = + serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); + + while (true) { + + int readyChannels = selector.selectNow(); + + if (readyChannels == 0) { + // System.out.println("readyChannels == 0"); + continue; + } + + Set selectedKeys = selector.selectedKeys(); + + Iterator keyIterator = selectedKeys.iterator(); + + while (keyIterator.hasNext()) { + + SelectionKey key = keyIterator.next(); + + if (key.isAcceptable()) { + // a connection was accepted by a ServerSocketChannel. + System.out.println("accepted"); + } else if (key.isConnectable()) { + // a connection was established with a remote server. + System.out.println("connectable"); + } else if (key.isReadable()) { + // a channel is ready for reading + System.out.println("ready"); + } else if (key.isWritable()) { + // a channel is ready for writing + System.out.println("writable"); + } + + keyIterator.remove(); + } + } + } +} diff --git a/java-io/src/main/resources/buffer-demo.txt b/java-io/src/main/resources/buffer-demo.txt new file mode 100644 index 0000000..991243a --- /dev/null +++ b/java-io/src/main/resources/buffer-demo.txt @@ -0,0 +1,20 @@ +0123456789 +asdf +123456789 +asdf +23456789 +asdf +3456789 +asdf +456789 +asdf +0123456789 +asdf +123456789 +asdf +23456789 +asdf +3456789 +asdf +456789 +asdf diff --git a/java-io/src/main/resources/channel-demo.txt b/java-io/src/main/resources/channel-demo.txt new file mode 100644 index 0000000..a4f45f9 --- /dev/null +++ b/java-io/src/main/resources/channel-demo.txt @@ -0,0 +1,2 @@ +0123456789 +asdf diff --git a/java-io/src/test/java/com/brianway/java/nio/tutorial/ChannelTransferTest.java b/java-io/src/test/java/com/brianway/java/nio/tutorial/ChannelTransferTest.java new file mode 100644 index 0000000..aac8b65 --- /dev/null +++ b/java-io/src/test/java/com/brianway/java/nio/tutorial/ChannelTransferTest.java @@ -0,0 +1,60 @@ +package com.brianway.java.nio.tutorial; + +import com.brianway.learning.java.nio.tutorial.BufferDemo; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + +/** + * 通道之间的数据传输 + * + * @auther brian + * @since 2019/6/19 00:20 + */ +public class ChannelTransferTest { + private String fromPath = BufferDemo.class.getResource("/").getPath() + "fromFile.txt"; + private String toPath = BufferDemo.class.getResource("/").getPath() + "toFile.txt"; + + @Test + public void testTransferFrom() { + try { + RandomAccessFile fromFile = new RandomAccessFile(fromPath, "rw"); + FileChannel fromChannel = fromFile.getChannel(); + + RandomAccessFile toFile = new RandomAccessFile(toPath, "rw"); + FileChannel toChannel = toFile.getChannel(); + + long position = 0; + long count = fromChannel.size(); + + long transferBytesSize = + toChannel.transferFrom(fromChannel, position, count); + Assert.assertEquals(26, transferBytesSize); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testTransferTo() { + try { + RandomAccessFile fromFile = new RandomAccessFile(fromPath, "rw"); + FileChannel fromChannel = fromFile.getChannel(); + + RandomAccessFile toFile = new RandomAccessFile(toPath, "rw"); + FileChannel toChannel = toFile.getChannel(); + + long position = 0; + long count = fromChannel.size(); + + long transferBytesSize = + fromChannel.transferTo(position, count, toChannel); + Assert.assertEquals(26, transferBytesSize); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/java-io/src/test/java/com/brianway/java/nio/tutorial/FileChannelTest.java b/java-io/src/test/java/com/brianway/java/nio/tutorial/FileChannelTest.java new file mode 100644 index 0000000..ca17f16 --- /dev/null +++ b/java-io/src/test/java/com/brianway/java/nio/tutorial/FileChannelTest.java @@ -0,0 +1,31 @@ +package com.brianway.java.nio.tutorial; + +import com.brianway.learning.java.nio.tutorial.BufferDemo; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + +/** + * FileChannel测试 + * + * @auther brian + * @since 2019/6/24 23:00 + */ +public class FileChannelTest { + private String dir = BufferDemo.class.getResource("/").getPath(); + private String path = BufferDemo.class.getResource("/").getPath() + "fileChannel.txt"; + + @Test + public void testTruncate() throws IOException { + RandomAccessFile aFile = new RandomAccessFile(dir + "truncate.txt", "rw"); + FileChannel channel = aFile.getChannel(); + int size = 10; + // Truncates this channel's file to the given size. + channel.truncate(size); + long fileSize = channel.size(); + Assert.assertEquals(size, fileSize); + } +} diff --git a/java-io/src/test/java/com/brianway/java/nio/tutorial/GatherTest.java b/java-io/src/test/java/com/brianway/java/nio/tutorial/GatherTest.java new file mode 100644 index 0000000..5e04221 --- /dev/null +++ b/java-io/src/test/java/com/brianway/java/nio/tutorial/GatherTest.java @@ -0,0 +1,37 @@ +package com.brianway.java.nio.tutorial; + +import com.brianway.learning.java.nio.tutorial.BufferDemo; +import org.junit.Test; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; + +/** + * @auther brian + * @since 2019/6/18 23:51 + */ +public class GatherTest { + + private String path = BufferDemo.class.getResource("/").getPath() + "gather.txt"; + + @Test + public void testGatheringWrites() throws IOException { + RandomAccessFile aFile = new RandomAccessFile(path, "rw"); + FileChannel channel = aFile.getChannel(); + ByteBuffer header = ByteBuffer.allocate(128); + ByteBuffer body = ByteBuffer.allocate(1024); + + header.put("这是头".getBytes(Charset.forName("UTF-8"))); + body.put("this is body.".getBytes(Charset.forName("UTF-8"))); + header.flip(); + body.flip(); + //write data into buffers + ByteBuffer[] bufferArray = {header, body}; + // 注意只有position和limit之间的数据才会被写入 + channel.write(bufferArray); + channel.close(); + } +} diff --git a/java-io/src/test/java/com/brianway/java/nio/tutorial/ScatterTest.java b/java-io/src/test/java/com/brianway/java/nio/tutorial/ScatterTest.java new file mode 100644 index 0000000..a02ddbf --- /dev/null +++ b/java-io/src/test/java/com/brianway/java/nio/tutorial/ScatterTest.java @@ -0,0 +1,53 @@ +package com.brianway.java.nio.tutorial; + +import com.brianway.learning.java.nio.tutorial.BufferDemo; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +/** + * @auther brian + * @since 2019/6/18 23:24 + */ +public class ScatterTest { + + private String path = BufferDemo.class.getResource("/").getPath() + "scatter.txt"; + + @Test + public void testScatteringReads() throws IOException { + RandomAccessFile aFile = new RandomAccessFile(path, "rw"); + FileChannel fc = aFile.getChannel(); + + //create buffer with capacity of 48 bytes + ByteBuffer header = ByteBuffer.allocate(8); + ByteBuffer body = ByteBuffer.allocate(1024); + + ByteBuffer[] bufferArray = {header, body}; + long bytesRead = fc.read(bufferArray); + // System.out.println(bytesRead); + Assert.assertEquals(26, bytesRead); + //print header + System.out.println("---header(" + header.limit() + "bytes)---"); + header.flip(); + while (header.hasRemaining()) { + // read 1 byte at a time + System.out.print((char) header.get()); + } + header.clear(); + + // print body + body.flip(); + System.out.println("---body(" + body.limit() + "bytes)----"); + while (body.hasRemaining()) { + // read 1 byte at a time + System.out.print((char) body.get()); + } + header.clear(); + body.clear(); + fc.close(); + } +} diff --git a/java-io/src/test/java/com/brianway/java/nio/tutorial/ServerSocketChannelTest.java b/java-io/src/test/java/com/brianway/java/nio/tutorial/ServerSocketChannelTest.java new file mode 100644 index 0000000..30ed0f1 --- /dev/null +++ b/java-io/src/test/java/com/brianway/java/nio/tutorial/ServerSocketChannelTest.java @@ -0,0 +1,48 @@ +package com.brianway.java.nio.tutorial; + +import org.junit.Test; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +/** + * @auther brian + * @since 2019/6/25 00:31 + */ +public class ServerSocketChannelTest { + + @Test + public void testOpen() throws IOException { + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.socket().bind(new InetSocketAddress(9999)); + while (true) { + System.out.println("waiting..."); + SocketChannel socketChannel = + serverSocketChannel.accept(); + //do something with socketChannel... + System.out.println("connected: " + socketChannel.toString()); + } + } + + @Test + public void testNonBlocking() throws IOException { + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + + serverSocketChannel.socket().bind(new InetSocketAddress(9999)); + serverSocketChannel.configureBlocking(false); + + while (true) { + System.out.println("waiting..."); + SocketChannel socketChannel = + serverSocketChannel.accept(); + + if (socketChannel != null) { + //do something with socketChannel... + System.out.println("connected: " + socketChannel.toString()); + } + } + } + +} diff --git a/java-io/src/test/java/com/brianway/java/nio/tutorial/SocketChannelTest.java b/java-io/src/test/java/com/brianway/java/nio/tutorial/SocketChannelTest.java new file mode 100644 index 0000000..3146572 --- /dev/null +++ b/java-io/src/test/java/com/brianway/java/nio/tutorial/SocketChannelTest.java @@ -0,0 +1,37 @@ +package com.brianway.java.nio.tutorial; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.SocketChannel; + +/** + * @auther brian + * @since 2019/6/24 23:33 + */ +public class SocketChannelTest { + + @Test + public void testConnect() throws IOException { + SocketChannel socketChannel = SocketChannel.open(); + boolean connected = socketChannel.connect(new InetSocketAddress("www.baidu.com", 80)); + Assert.assertEquals(true, connected); + socketChannel.close(); + } + + @Test + public void testNonBlocking() throws IOException { + SocketChannel socketChannel = SocketChannel.open(); + socketChannel.configureBlocking(false); + boolean connected = socketChannel.connect(new InetSocketAddress("www.baidu.com", 80)); + System.out.println("connected: " + connected); + while (!socketChannel.finishConnect()) { + //wait, or do something else... + System.out.println("not finish"); + } + System.out.println("finished"); + } + +} diff --git a/java-io/src/test/resources/fileChannel.txt b/java-io/src/test/resources/fileChannel.txt new file mode 100644 index 0000000..32ca54f --- /dev/null +++ b/java-io/src/test/resources/fileChannel.txt @@ -0,0 +1,4 @@ +asdasdas +ahfsadkjhsdfkjl +23232323 +2323 \ No newline at end of file diff --git a/java-io/src/test/resources/fromFile.txt b/java-io/src/test/resources/fromFile.txt new file mode 100644 index 0000000..dbd2aa4 --- /dev/null +++ b/java-io/src/test/resources/fromFile.txt @@ -0,0 +1,3 @@ +1234567 +qwertyusfdf +asdsad \ No newline at end of file diff --git a/java-io/src/test/resources/scatter.txt b/java-io/src/test/resources/scatter.txt new file mode 100644 index 0000000..dbd2aa4 --- /dev/null +++ b/java-io/src/test/resources/scatter.txt @@ -0,0 +1,3 @@ +1234567 +qwertyusfdf +asdsad \ No newline at end of file diff --git a/java-io/src/test/resources/truncate.txt b/java-io/src/test/resources/truncate.txt new file mode 100644 index 0000000..32ca54f --- /dev/null +++ b/java-io/src/test/resources/truncate.txt @@ -0,0 +1,4 @@ +asdasdas +ahfsadkjhsdfkjl +23232323 +2323 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3982c6d..5965258 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ UTF-8 1.8 1.8 - 4.11 + 4.13.1