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
18
votes
3 answers

Vert.x Event loop - How is this asynchronous?

I'm playing around with Vert.x and quite new to the servers based on event loop as opposed to the thread/connection model. public void start(Future fut) { vertx .createHttpServer() .requestHandler(r -> { …
user378101
  • 649
  • 3
  • 12
  • 19
18
votes
2 answers

Memory Mapped file in C++ read in Java

I have a C++ program that uses at one point hMapFile = OpenFileMapping(dwDesiredAccess, bInheritHandle, lpName); The lpName is something like "myfile" without extensions or path, i.e. a memory mapped file. I can find the file with the…
rst
  • 2,510
  • 4
  • 21
  • 47
18
votes
2 answers

Why does 'File.exists' return true, even though 'Files.exists' in the NIO 'Files' class returns false

I am trying to determine if a file exists in a network folder: // File name is "\\QWERTY\folder\dir\A123456.TXT" Path path = Paths.get("\\\\QWERTY\\folder\\dir\\A123456.TXT") Using NIO Files: Files.exists(path) == false Using…
The Coordinator
  • 13,007
  • 11
  • 44
  • 73
18
votes
4 answers

Why use Java's AsynchronousFileChannel?

I can understand why network apps would use multiplexing (to not create too many threads), and why programs would use async calls for pipelining (more efficient). But I don't understand the efficiency purpose of AsynchronousFileChannel. Any ideas?
Joey Bell
  • 181
  • 1
  • 4
18
votes
0 answers

Servlet Container (e.g.Tomcat) vs. NIO framework (e.g. Grizzly) For RESTful applications?

What is the pros and cons of using Servlet Containers (like Apache Tomcat) in contrast with using NIO frameworks (such as Netty and Grizzly) for hosting a JAX-RS RESTful service? I'm going to develop a RESTful application using Java. Java has JAX-RS…
sajjadG
  • 2,546
  • 2
  • 30
  • 35
18
votes
2 answers

Detecting socket disconnection?

I am kinda upset that this cannot be handled in an elegant way, after trying different solutions (this, this and several others) mentioned in answers to several SO questions, I still could not manage to detect socket disconnection (by unplugging…
neevek
  • 11,760
  • 8
  • 55
  • 73
18
votes
3 answers

convert a LongBuffer/IntBuffer/ShortBuffer to ByteBuffer

I know a quick way to convert a byte/short/int/long array to ByteBuffer, and then obtain a byte array. For instance, to convert a byte array to short array I can do: byte[] bArray = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb…
logoff
  • 3,347
  • 5
  • 41
  • 58
18
votes
5 answers

Java thread blocks while registering channel with selector while select() is called. What to do?

I have a basic question. Why and how SelectableChannel's register method can be in blocking call. Let me provide a scenario. I have created a Selector object in class Register as follows. private static Selector selector = Selector.open(); I also…
Nilesh
  • 557
  • 2
  • 10
  • 21
17
votes
2 answers

Why does FileChannel.map take up to Integer.MAX_VALUE of data?

I am getting following exception when using FileChannel.map Exception in thread "main" java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE at sun.nio.ch.FileChannelImpl.map(Unknown Source) at…
siddharth178
  • 265
  • 3
  • 9
17
votes
4 answers

Servlet API implementation using Netty

Has anyone made a Servlet API implementation built on top of Netty? I'm tempted to build my own as I can't google an implementation. http://www.jboss.org/netty/community#nabble-td4752485 http://markmail.org/message/4qmvuaacxqzevqhc Basically I'm…
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
17
votes
12 answers

Jenkins Windows slave connection getting terminated with java.nio.channels.ClosedChannelException

While connecting to windows machine as slave, i am getting following error i think its some network related issue, but need some help where to start looking or what is a possible solution for this. INFO: Terminated Aug 01, 2017 10:15:54 PM…
Yogesh Kumar
  • 682
  • 1
  • 10
  • 29
17
votes
5 answers

Java, IO - fastest way to remove file

My problem is that I have an app which is writing a lot of relatively (100-500kb) small CSV files (tens and hundreds of thousands ). Content of those files then get loaded in database via sql loader call (its oracle db) and this is what I have to…
Zorkus
  • 484
  • 1
  • 4
  • 13
17
votes
3 answers

Java 7 nio list directory with wildcard

I'd like to find a file in a directory using wildcard. I have this in Java 6 but want to convert the code to Java 7 NIO: File dir = new File(mydir); FileFilter fileFilter = new WildcardFileFilter(identifier+".*"); File[] files =…
Jabda
  • 1,752
  • 5
  • 26
  • 54
17
votes
2 answers

Zipping a huge folder by using a ZipFileSystem results in OutOfMemoryError

The java.nio package has a beautiful way of handling zip files by treating them as file systems. This enables us to treat zip file contents like usual files. Thus, zipping a whole folder can be achieved by simply using Files.copy to copy all the…
gexicide
  • 38,535
  • 21
  • 92
  • 152
16
votes
1 answer

Java7 WatchService - Access Denied error trying to delete recursively watched nested directories (Windows only)

I followed the Watching a Directory for Changes Java7 nio2 tutorial to recursively monitor the entire contents of a directory using the code sample WatchDir.java. While this works well on Linux and Mac, on Windows (tested on Vista and 7), trying to…
FR.
  • 163
  • 1
  • 4