nio

理解Java NIO

余生长醉 提交于 2021-02-17 08:24:22
基础概念 缓冲区操作 缓冲区及操作是所有I/O的基础,进程执行I/O操作,归结起来就是向操作系统发出请求,让它要么把缓冲区里的数据排干(写),要么把缓冲区填满(读)。如下图 内核空间、用户空间 上图简单描述了数据从磁盘到用户进程的内存区域移动的过程,其间涉及到了内核空间与用户空间。这两个空间有什么区别呢? 用户空间就是常规进程(如JVM)所在区域,用户空间是非特权区域,如不能直接访问硬件设备。内核空间是操作系统所在区域,那肯定是有特权啦,如能与设备控制器通讯,控制用户区域的进程运行状态。进程执行I/O操作时,它执行一个系统调用把控制权交由内核。 虚拟内存 内存页面调度 5种I/O模型 说起I/O模型,网络上有一个错误的概念,异步非阻塞/阻塞模型,其实异步根本就没有阻不阻塞之说,异步模型就是异步模型。让我们来看一看Richard Stevens在其UNIX网络编程卷1中提出的5个I/O模型吧。 阻塞式I/O 非阻塞式I/O I/O复用(Java NIO就是这种模型) 信号驱动式I/O 异步I/O 由POSIX术语定义,同步I/O操作导致请求进程阻塞,直到I/O操作完成;异步I/O操作不导致请求进程阻塞。5种模型中的前4种都属于同步I/O模型。 Why NIO? 开始讲NIO之前,了解为什么会有NIO,相比传统流I/O的优势在哪,它可以用来做什么等等的问题,还是很有必要的。 传统流I

ServerSocketChannel stops Accepting after a while on Linux

梦想的初衷 提交于 2021-02-11 14:23:29
问题 I noticed that, when I run my application on a Linux OS, after a while, the server just stops accepting clients. This is a screenshot of wireshark when trying to connect to the Server from my host, after it stopped accepting. As you can see the first request is [FIN,ACK] and it seems like that my server can't handle that. Starting up the server, opens the selector, binds, blocking mode is set to false and the server channel registers for OP_ACCEPT. (the usual standard stuff) And this is the

Java NIO Files count() Method for Counting the Number of Lines

和自甴很熟 提交于 2021-02-10 20:20:51
问题 I was using the below code to calculate the number of lines in a file. long numberOfLines = Files.lines(filePath).count(); Based on the output of the above, if the number of lines is 1, then the record delimiter (extracted from the last character of the line) should be replaced by <delimiter>\n . The delimiter got replaced fine, but when I tried to delete this file after this operation, it did not work. if (numberOfLines == 1) { String rawData = (new String(Files.readAllBytes(filePath))).trim

Java NIO Files count() Method for Counting the Number of Lines

白昼怎懂夜的黑 提交于 2021-02-10 20:19:32
问题 I was using the below code to calculate the number of lines in a file. long numberOfLines = Files.lines(filePath).count(); Based on the output of the above, if the number of lines is 1, then the record delimiter (extracted from the last character of the line) should be replaced by <delimiter>\n . The delimiter got replaced fine, but when I tried to delete this file after this operation, it did not work. if (numberOfLines == 1) { String rawData = (new String(Files.readAllBytes(filePath))).trim

How do you filter hidden files using DirectoryStream.Filter

一笑奈何 提交于 2021-02-09 08:22:02
问题 I am trying to filter hidden files using the NIO classes. When I run the attached code on Windows 10 I get the following output: Files: c:\Documents and Settings c:\PerfLogs c:\Program Files c:\Program Files (x86) c:\Users c:\Windows Paths: c:\$Recycle.Bin c:\Config.Msi c:\Documents and Settings c:\Intel c:\IntelOptaneData c:\OEM c:\OneDriveTemp c:\PerfLogs c:\Program Files c:\Program Files (x86) c:\ProgramData c:\Recovery c:\System Volume Information c:\Users c:\Windows The list displayed

How do you filter hidden files using DirectoryStream.Filter

时间秒杀一切 提交于 2021-02-09 08:20:27
问题 I am trying to filter hidden files using the NIO classes. When I run the attached code on Windows 10 I get the following output: Files: c:\Documents and Settings c:\PerfLogs c:\Program Files c:\Program Files (x86) c:\Users c:\Windows Paths: c:\$Recycle.Bin c:\Config.Msi c:\Documents and Settings c:\Intel c:\IntelOptaneData c:\OEM c:\OneDriveTemp c:\PerfLogs c:\Program Files c:\Program Files (x86) c:\ProgramData c:\Recovery c:\System Volume Information c:\Users c:\Windows The list displayed

How do you filter hidden files using DirectoryStream.Filter

孤街浪徒 提交于 2021-02-09 08:20:06
问题 I am trying to filter hidden files using the NIO classes. When I run the attached code on Windows 10 I get the following output: Files: c:\Documents and Settings c:\PerfLogs c:\Program Files c:\Program Files (x86) c:\Users c:\Windows Paths: c:\$Recycle.Bin c:\Config.Msi c:\Documents and Settings c:\Intel c:\IntelOptaneData c:\OEM c:\OneDriveTemp c:\PerfLogs c:\Program Files c:\Program Files (x86) c:\ProgramData c:\Recovery c:\System Volume Information c:\Users c:\Windows The list displayed

Highly Concurrent Apache Async HTTP Client IOReactor issues

天大地大妈咪最大 提交于 2021-02-07 06:56:20
问题 Application description : I'm using Apache HTTP Async Client ( Version 4.1.1 ) Wrapped By Comsat's Quasar FiberHttpClient ( version 0.7.0 ) in order to run & execute a highly concurrent Java application that uses fibers to internally send http requests to multiple HTTP end-points The Application is running on top of tomcat( however , fibers are used only for internal request dispatching. tomcat servlet requests are still handled the standard blocking way ) Each external request opens 15-20

Files.walk skip directories [duplicate]

一笑奈何 提交于 2021-01-27 07:14:53
问题 This question already has answers here : Files.walk(), calculate total size (4 answers) Closed 4 years ago . I am trying to write a program the list files recursively in my external hd but there is this recycle bin folder that I don't have access to. I want to skip the folder but can't seem to do it. Is there anything wrong with this code below? public static void main(String[] args) throws Exception { String path = "K:\\"; Files.walk(Paths.get(path)) .filter(it -> !it.toString().startsWith(

Uniquely identify file in Java

旧时模样 提交于 2021-01-26 11:01:39
问题 Im on Linux and my Java application is not intended to be portable. I'm looking for a way to identify a file uniquely in Java. I can make use of statfs syscall since the pair (f_fsid, ino) uniquely identifies a file (not only across a file system) as specified here: http://man7.org/linux/man-pages/man2/statfs.2.html The question is if it is possible extract fsid from Java directly so I can avoid writing JNI function? inode can be extracted with NIO , but how about fsid? inode and fsid comes