Questions tagged [nio]

NIO is Java 'New I/O' introduced in 1.4, providing non-blocking and multiplexed network I/O; 'direct' (native) buffers; file locks and mapped files; and character set codecs.

NIO stands for 'New I/O'. It was introduced in JDK 1.4 in the java.nio package. It comprises several elements:

  1. A family of buffers that encapsulate the current position and limit, and can contain primitive types and arrays of them, and whose data can be held at either the Java level or the native level, the latter via 'direct' buffers. Copying I/O between channels using direct buffers need not cross the JNI layer into Java at all, which has considerable speed benefits.
  2. A family of channels that can perform blocking or non-blocking I/O, or non-blocking multiplexed I/O via the Java equivalent of the Unix select() function, which avoids the necessity in java.net to create a thread per connection, and hence improves scalability. There is also a FileChannel with locking primitives and a capability to provide memory-mapped files. A Pipe class with a pair of selectable channels is also provided.
  3. A family of character set codecs.
  4. Starting in Java 1.7, NIO was extended to support non-blocking I/O for Filesystem reads and writes as well as NIO support for Multicast.

Java NIO Libraries

The NIO package is a fairly low level API but several third party libraries have emerged that simplify the development of NIO leveraging software:

  • Netty: Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
  • Apache MINA: Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract, event-driven,asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
  • Grizzly: The Grizzly NIO and Web framework has been designed to help developers to take advantage of the Java™ NIO API. Grizzly's goal is to help developers to build scalable and robust servers using NIO and we are also offering extended framework components: Web Framework (HTTP/S), Bayeux Protocol, Servlet, HttpService OSGi and Comet.
  • Vert.x: Effortless asynchronous application development for the modern web and enterprise, supporting components in JavaScript, Ruby, Groovy or Java and the ability to mix and match several programming languages in a single application.
  • xsocket: xSocket is an easy to use NIO-based network library to build high performance, highly scalable network applications. (Development discontinued)
  • NIO Framework: The NIO Framework is a library on top of NIO that hides most of the complexity of plain NIO. With the NIO Framework you can implement high-performance Java network applications without having to deal with all the nasty details of NIO.
3015 questions
19
votes
2 answers

Fastest way to incrementally read a large file

When given a buffer of MAX_BUFFER_SIZE, and a file that far exceeds it, how can one: Read the file in blocks of MAX_BUFFER_SIZE? Do it as fast as possible I tried using NIO RandomAccessFile aFile = new RandomAccessFile(fileName, "r"); …
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
19
votes
7 answers

Java NIO: transferFrom until end of stream

I'm playing around with the NIO library. I'm attempting to listen for a connection on port 8888 and once a connection is accepted, dump everything from that channel to somefile. I know how to do it with ByteBuffers, but I'd like to get it working…
aioobe
  • 413,195
  • 112
  • 811
  • 826
19
votes
4 answers

How to avoid making defensive copies of ByteBuffer?

I've got a class that takes a ByteBuffer as a constructor argument. Is there a way to avoid making defensive copies in order to ensure that the buffer doesn't get modified past that point? ByteBuffer.isReadOnly() doesn't guarantee that the original…
Gili
  • 86,244
  • 97
  • 390
  • 689
19
votes
3 answers

How does java NIO work internally, is thread pool used internally?

Nio provides async io - meaning calling thread is not blocked on IO operations. However, i am still confused how this works internally? From this answer - there just thread pool that where synch IO is submitted. Do jvm has thread pool where actually…
Oleksandr Papchenko
  • 2,071
  • 21
  • 30
19
votes
1 answer

Drawbacks of Tomcat Http11NioProtocol

With Tomcat 6.0.x, we can use Http11NioProtocol and get scalable performance. Is there any specific reason/drawback of using Http11NioProtocol, that Tomcat is not using this protocol as a default protocol?
Arun
  • 191
  • 1
  • 3
19
votes
10 answers

One thread per client. Doable?

I'm writing a Java server which uses plain sockets to accept connections from clients. I'm using the fairly simple model where each connection has its own thread reading from it in blocking mode. Pseudo code: handshake(); while(!closed) { length…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
19
votes
2 answers

How to get FileInputStream to File in assets folder

I know how to use the AssetManager to read a File from the res/raw directory with an InputStream, but for my special use case I need a FileInputStream. The reason I need a FileInputStream specifically is because I need to get the FileChannel object…
Mike Herasimov
  • 1,319
  • 3
  • 14
  • 31
19
votes
2 answers

Java NIO non-blocking mode vs node.js asynchronous operation

I have not gone through the code detail of node.js . But, going through some research about thread in Node.js, I found that it has single thread for accepting connection from multiple clients. When connected with client it fires connection events…
abishkar bhattarai
  • 7,371
  • 8
  • 49
  • 66
19
votes
5 answers

java.nio.file.Files.delete(Path path) - occasional failure to recursively delete directory using SimpleFileVisitor

Trying to troubleshoot an occasional java.nio.file.DirectoryNotEmptyException in a recursive delete method taken from Delete directories recursively in Java Code (credit to @TrevorRobinson) : static void removeRecursive(Path path) throws IOException…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
18
votes
11 answers

Java Large Files Disk IO Performance

I have two (2GB each) files on my harddisk and want to compare them with each other: Copying the original files with Windows explorer takes approx. 2-4 minutes (that is reading and writing - on the same physical and logical disk). Reading with…
Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
18
votes
4 answers

Java ByteBuffer performance issue

While processing multiple gigabyte files I noticed something odd: it seems that reading from a file using a filechannel into a re-used ByteBuffer object allocated with allocateDirect is much slower than reading from a MappedByteBuffer, in fact it is…
Folkert van Heusden
  • 433
  • 4
  • 17
  • 38
18
votes
7 answers

Extending ByteBuffer class

Is there any way to create class that extends ByteBuffer class? Some abstract methods from ByteBuffer are package private, and if I create package java.nio, security exception is thrown. I would want to do that for performance reasons - getInt for…
Sarmun
  • 2,378
  • 2
  • 22
  • 24
18
votes
3 answers

Non-blocking UDP I/O vs blocking UDP I/O in Java

Non-blocking TCP/IP SocketChannels and Selector in NIO help me to handle many TCP/IP connections with small number of threads. But how about UDP DatagramChannels? (I must admit that I'm not very familiar with UDP.) UDP send operations don't seem…
trustin
  • 12,231
  • 6
  • 42
  • 52
18
votes
4 answers

How to use servlet 3.1 in spring mvc?

There are 2 different features available: servlet 3.0 allows to process request in a thread different from the container thread. servlet 3.1 allows to read/write into socket without blocking reading/writing thread There are a lot of examples in…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
18
votes
3 answers

Is there a way for a Java app to gain root permissions?

When running Files.walk(Paths.get("/var/")).count() as an unprivileged user, the execution might throw an exception as there are folders inside /var/ that need root permission to be traversed. I am not looking for a way to execute a bash command as…
Behrang
  • 46,888
  • 25
  • 118
  • 160