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
28
votes
1 answer

How to check if the OS is POSIX compliant

I am writing a cross-platform application that creates temporary files and copies these to another location, where they need to be readable by everyone. (By default, only the owner has read access to temporary files.) I tried using the POSIX file…
Mangara
  • 1,116
  • 1
  • 10
  • 23
27
votes
10 answers

NIO Performance Improvement compared to traditional IO in Java

I have seen many articles/blogs saying that Java NIO is a better solution compared to traditional Java IO. But today one of my co-worker showed me this blog http://mailinator.blogspot.com/2008/02/kill-myth-please-nio-is-not-faster-than.html. I am…
sanjoy roy
  • 287
  • 1
  • 3
  • 4
27
votes
4 answers

Java Non-Blocking and Asynchronous IO with NIO & NIO.2 (JSR203) - Reactor/Proactor Implementations

So here I am reading one of my favorite software pattern books (Pattern-Oriented Software Architecture - Patterns for Concurrent and Networked Objects), specifically the sections on Proactor/Reactor asynchronous IO patterns. I can see how by using…
S73417H
  • 2,661
  • 3
  • 23
  • 37
27
votes
5 answers

Java in 2011: threaded sockets VS NIO: what to choose on 64bit OS and latest Java version?

I've read several posts about java.net vs java.nio here on StackOverflow and on some blogs. But I still cannot catch an idea of when should one prefer NIO over threaded sockets. Can you please examine my conclusions below and tell me which ones are…
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
27
votes
1 answer

Java byte array to ByteBuffer or ByteBuffer to byte array convertion "WITHOUT COPYING"

the biggest problem on java arrays , they require copying to edit. I am using AMD Aparapi , which i get byte array from my calculations. I need to show that byte array as a bytebuffer "without copying" byte aparapiData[]; ByteBuffer…
Kadir BASOL
  • 729
  • 2
  • 10
  • 20
27
votes
5 answers

Files.walk(), calculate total size

I'm trying to calculate the size of the files on my disc. In java-7 this could be done using Files.walkFileTree as shown in my answer here. However if i wanted to do this using java-8 streams it will work for some folders, but not for all. public…
Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
27
votes
2 answers

Java 7: get Path of resource (as object of type Path)

I'm using the features of Java 7 to read in a file. For that purpose I need an object of type Path. In my code, I use the getResource() function to get the relative path (of type URL) to a file. However, now I have the problem that I don't really…
navige
  • 2,447
  • 3
  • 27
  • 53
26
votes
3 answers

Any NIO frameworks for .NET?

Are there any non-blocking IO frameworks for .NET? I am looking for something similar to what Apache Mina and JBoss Netty provides for Java: a framework for implementing highly scalable servers - not just the low-level support that the .NET…
Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
26
votes
2 answers

Why FileChannel in Java is not non-blocking?

I wanted to write a program which writes to multiple files simultaneously; thought it will be possible with one thread by using non-blocking mode. But FileChannel does not support non-blocking mode. Does anybody know why?
user467158
  • 363
  • 4
  • 7
26
votes
3 answers

What are the Netty alternatives for high-performance networking?

I am in the process of choosing a networking library to implement a client/server system that cannot spare any microsecond. It will implement its own protocol to send and receive messages. I am looking for a good NIO framework that will allow me to…
chrisapotek
  • 6,007
  • 14
  • 51
  • 85
26
votes
2 answers

Play framework/Netty does not release socket

Have already asked similar question, but deleted it, since thought I fixed it, but I was wrong. I'm using Play framework in production for one of my web projects. From time to time Play does not render main page or does not return some of the static…
Anton
  • 5,831
  • 3
  • 35
  • 45
26
votes
1 answer

Does FileChannel.close() close the underlying stream?

Like the title; Does closing a FileChannel close the underlying file stream? From the AbstractInterruptibleChannel.close() API docs you can read: Closes this channel. If the channel has already been closed then this method returns immediately.…
dacwe
  • 43,066
  • 12
  • 116
  • 140
25
votes
1 answer

What is the difference between Vert.x and Netty?

What is the difference between Vert.x and Netty? Why should one ever prefer Netty over Vert.x? Both of them are event-driven, non-blocking and asynchronous frameworks designed for high-load I/O. Vert.x is based on Multi-Reactor pattern (Node's…
VB_
  • 45,112
  • 42
  • 145
  • 293
25
votes
7 answers

Read lines of characters and get file position

I'm reading sequential lines of characters from a text file. The encoding of the characters in the file might not be single-byte. At certain points, I'd like to get the file position at which the next line starts, so that I can re-open the file…
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
25
votes
1 answer

How do Jetty and other containers leverage NIO while sticking to the Servlet specification?

I'm new to NIO, and I am trying to figure out how Jetty leverages NIO. My understanding of how traditional servlet containers that use Blocking IO service a request is as follows: A request arrives A thread is allocated to process the request and…
rationalrevolt
  • 663
  • 1
  • 6
  • 13