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
1
vote
0 answers

Netty channel close

I hava a question about Netty channel. I inserted a channel handler into a channel pipeline. When connection is down (for example, the peer socket closed), channelInactive method is called: @Override public void…
ruanhao
  • 4,663
  • 6
  • 28
  • 43
1
vote
0 answers

non blocking writes between listeners

I'm reading an http response using the apache async client. Every time I read a chunk of data I want to write it to the servletoutputstream in a non-blocking mode. Something like this: // decoder.read is executed when data available for…
JBalaguero
  • 181
  • 2
  • 13
1
vote
1 answer

Not able to open selector in solaris for non root user

I have a server application which runs fine in most of the cases but in one solaris machine it not able to open the selector ,for root user it is working fine. for other user it is giving the below exception java.io.IOException: Permission denied at…
sreejith
  • 716
  • 5
  • 20
1
vote
0 answers

how to import java.nio.file in android

I'm confused.Why okio cannot find this package in IDE and android cannot use this packgae based on 'How to use java.nio.file package in android?', but okio still works? How can I do this like okio?
machinezhou
  • 679
  • 2
  • 6
  • 16
1
vote
1 answer

how to create large number of connects with netty 5.0

I want to create large number client connection to the server for the test purpose. I accomplish this by creating thread per connection, so I can only create 3000 connection on my machine. below is my code: package…
Sam Zeng
  • 11
  • 1
1
vote
0 answers

Java not allowing me to use anything under java.nio?

For some reason i get this error when trying to access anything under java.nio it throws an error? i have looked for anything of this but i cant find anything and i need to write some files. If anyone knows anything of this and knows how to fix it,…
Connor S
  • 264
  • 1
  • 3
  • 12
1
vote
2 answers

getResource() to Path issue

Within my program i have a line of code: Path toRead = new File(getClass().getResource("/data.txt").toString()).toPath(); Whenever I try to run this I get an error: Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at…
Crimson Bloom
  • 287
  • 2
  • 13
1
vote
2 answers

java nio socket not detecting when machine goes to sleep or hibernates

Here are simplified versions of my socket server and client components. The primary goal is for the client to detect when the server goes down and for the server to detect when the client goes down. This works perfectly (on Windows) when either the…
puiuvlad
  • 13
  • 3
1
vote
1 answer

What sort of Java Thread pool can I use for processing data from sockets?

I have the following code for starting a threaded server: Thread server = new Thread(new ServerRunnable(serverPort, devMode, messageQueue, database)); server.start(); Thread worker1 = new Thread( new WorkerRunnable( messageQueue, …
mal
  • 3,022
  • 5
  • 32
  • 62
1
vote
1 answer

Java NIO client causes file descriptor leakage only when remote TCP server is down

The below program acts as TCP client and uses NIO to open socket to a remote server, as below private Selector itsSelector; private SocketChannel itsChannel; public boolean getConnection(Selector selector, String host, int port) { try { …
1
vote
0 answers

Java NIO SocketChannel read() after write()

I have a client/server application where two users write to each other and also can send large files to each other, i used ServerSocket and Socket classes to do this and everything was fine except of slow file transfer, this was the code: Client…
Peter
  • 409
  • 2
  • 6
  • 14
1
vote
1 answer

Unable to read value from console in java using eclipse

I am learning Java from Java doc and I want to implement the examples, but I am facing some problems. Can anyone describe me this problem, and tell me how can i get values from console using eclipse IDE. Whenever, I execute this example, the output…
Atul Rai
  • 332
  • 1
  • 10
  • 25
1
vote
4 answers

Can Java non-blocking io be used for the described application?

I had built a java TCPServer using serversocketchannels running on one port. However, it is not very scalable as it attends to one incoming socket (blocking mode) only. I want to extend this TCPServer to service multiple incoming sockets (maximum 10…
Poliquin
  • 2,937
  • 4
  • 28
  • 32
1
vote
1 answer

How to send response without reading entire request?

I'm creating service based on Tomcat Servlets and NIO. On input there is big XML request(~100 MB), send through HTML POST method. I want to stream only first 8 KiB, and after that immediately send response to client. public class A extends…