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
38
votes
2 answers

java.io.File vs java.nio.Files which is the preferred in new code?

While writing answers around SO, a user tried pointing out that java.io.File should not be used in new code, instead he argues that the the new object java.nio.Files should be used instead; he linked to this article. Now I have been developing in…
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
35
votes
1 answer

Java nio: How to add extension to an absolute path?

This feels like it should be something straight forward, but I can seem to find an elegant solution to it without converting to File. Given a Path Path path = Paths.get("/a/b/foo") How to do get the path /a/b/foo.bar? subpath will return a relative…
jm1234567890
  • 1,612
  • 3
  • 21
  • 29
34
votes
3 answers

java.nio.file.Path for URLs?

Java7 ships with a default Path implementation for local files. Is there a Path implementation for URLs? For example, I should be able to copy a remote resource using the following code: Path remote = Paths.get(new…
Gili
  • 86,244
  • 97
  • 390
  • 689
33
votes
5 answers

Java WatchService not generating events while watching mapped drives

I implemented a file watcher but I noticed that java nio file watcher doesn't generate events for files being copied on mapped drives. For instance, I've run the file watcher on Unix to watch a local directory (/sharedfolder) which is mapped on…
Ramcis
  • 381
  • 1
  • 4
  • 5
33
votes
8 answers

Binary search in a sorted (memory-mapped ?) file in Java

I am struggling to port a Perl program to Java, and learning Java as I go. A central component of the original program is a Perl module that does string prefix lookups in a +500 GB sorted text file using binary search (essentially, "seek" to a byte…
sds
  • 373
  • 1
  • 4
  • 7
33
votes
4 answers

Why the odd performance curve differential between ByteBuffer.allocate() and ByteBuffer.allocateDirect()

I'm working on some SocketChannel-to-SocketChannel code which will do best with a direct byte buffer--long lived and large (tens to hundreds of megabytes per connection.) While hashing out the exact loop structure with FileChannels, I ran some…
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
32
votes
6 answers

difference between bytebuffer.flip() and bytebuffer.rewind()

I am aware that flip() set the current buffer position to 0 and set the limit to the previous buffer position whereas rewind() just set the current buffer position to 0. In the following code, either I use rewind() or flip() i get the same…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
31
votes
3 answers

Using Java nio to create a subdirectory and file

I'm creating a simple program that will try to read in "conf/conf.xml" from disk, but if this file or dir doesn't exist will instead create them. I can do this using the following code: // create subdirectory path Path confDir =…
user3341332
  • 399
  • 1
  • 3
  • 10
31
votes
1 answer

Create a ByteBuf in Netty 4.0

Two simple questions, which I am not able to solve by reading the documentation: I have a byte[] How can i convert it to a ByteBuf? I have a NIO ByteBuffer How can i convert it to a ByteBuf?
Dennis
  • 4,011
  • 7
  • 36
  • 50
30
votes
6 answers

Deep copy duplicate() of Java's ByteBuffer

java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer?
Mr. Red
  • 301
  • 1
  • 3
  • 3
30
votes
7 answers

how to choose java nio vs io?

As we had known, If we want to use traditional IO to construct server, it must block somewhere, so we had to use loop or one thread one socket mode, So nio seem it is better choice. So I want know if the nio is better choice forever?
jiafu
  • 6,338
  • 12
  • 49
  • 73
29
votes
3 answers

How does the Netty threading model work in the case of many client connections?

I intend to use Netty in an upcoming project. This project will act as both client and server. Especially it will establish and maintain many connections to various servers while at the same time serving its own clients. Now, the documentation for…
Jiddo
  • 1,256
  • 1
  • 10
  • 15
29
votes
2 answers

TCP: Server sends [RST, ACK] immediately after receiving [SYN] from Client

Host_A tries to send some data to Host_B over TCP. Host_B is listening on port 8181. Both Host_A & Host_B are Linux boxes (Red Hat Enterprise). The TCP layer is implemented using Java NIO API. Whatever Host_A sends, Host_B is unable to receive.…
Riyaz
  • 646
  • 2
  • 11
  • 15
29
votes
2 answers

Why native epoll support is introduced in Netty?

I believe Java's NIO library will use epoll on Linux machines. What are all the advantages of using Epoll instead of NIO on Linux machines.
Dhanaraj Durairaj
  • 644
  • 1
  • 6
  • 17
29
votes
1 answer

How to send and receive serialized object in socket channel

I want to transmit a serialized object over a socket channel. I want make "Hi friend" string as serialized object and then write this object in socket channel while in the other end i want to read the same object and retrieve the data. All these…
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243