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

Advantages of Java NIO in blocking mode versus traditional I/O?

I have pretty much already decided not to use asynchronous, non-blocking Java NIO. The complexity versus benefit is very questionable in general, and I think it's not worth it in this project particularly. But most of what I read about NIO, and…
Chad N B
  • 817
  • 9
  • 11
24
votes
3 answers

Force stop Java Files.copy() running on external thread

The answer here seemed to be a valid solution before Java 8: How to cancel Files.copy() in Java? But now it doesn't work, because ExtendedCopyOption.INTERRUPTIBLE is private. Basically, I need to download a file from some given URL and save it to…
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
24
votes
3 answers

Java High-load NIO TCP server

As a part of my research I'm writing an high-load TCP/IP echo server in Java. I want to serve about 3-4k of clients and see the maximum possible messages per second that I can squeeze out of it. Message size is quite small - up to 100 bytes. This…
Juriy
  • 5,009
  • 8
  • 37
  • 52
24
votes
4 answers

Why does WatchService generate so many operations?

import java.io.*; import java.nio.file.*; public class Tmp { public static void main(String [] args) throws IOException { int count = 0; Path path = Paths.get("C:\\tmp\\"); WatchService ws = null; try { …
Pawel P.
  • 3,731
  • 4
  • 20
  • 20
23
votes
5 answers

Why the key should be removed in `selector.selectedKeys().iterator()` in java nio?

I found some sample code of java nio: ServerSocketChannel server = ServerSocketChannel.open(); Selector selector = Selector.open(); server.socket().bind(new InetSocketAddress(8080)); server.configureBlocking(false); …
Freewind
  • 193,756
  • 157
  • 432
  • 708
22
votes
4 answers

How should I use AsynchronousServerSocketChannel for accepting connections?

I would like to write an asynchronous server using Java 7 and NIO 2. But how should I use AsynchronousServerSocketChannel? E.g. if I start with: final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind( …
Jonas
  • 121,568
  • 97
  • 310
  • 388
22
votes
2 answers

Getting FileSystemException "A required privilege is not held by the client" using Files.createSymbolicLink in Play Framework

I'm trying to use the new Java 7 Files.createSymbolicLink() method within Play! Framework, and I got the following exception: RuntimeException occured : java.nio.file.FileSystemException: c:\work\foo\bar: A required privilege is not held by the…
ripper234
  • 222,824
  • 274
  • 634
  • 905
22
votes
3 answers

Java I/O vs. Java new I/O (NIO) with Linux NPTL

My webservers use the usual Java I/O with thread per connection mechanism. Nowadays, they are getting on their knees with increased user (long polling connection). However, the connections are mostly idle. While this can be solved by adding more…
Sajid
  • 891
  • 2
  • 13
  • 24
22
votes
2 answers

What is the cause of BufferOverflowException?

The exception stack is java.nio.BufferOverflowException at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:327) at java.nio.ByteBuffer.put(ByteBuffer.java:813) mappedByteBuffer.put(bytes); The code: randomAccessFile =…
fuyou001
  • 1,594
  • 4
  • 16
  • 27
22
votes
7 answers

Netty based non-blocking REST framework

I am working on a RESTfull application which requires high scalability. I am considering Netty based frameworks for RESTfull applications. I went through some of the available options and tried to get what they can offer as a non-blocking…
Vaibhav Raj
  • 2,214
  • 4
  • 23
  • 39
22
votes
6 answers

How do I rename (not move) a file in Java 7?

I'm a bit confused with all these new File I/O classes in JDK7. Let's say, I have a Path and want to rename the file it represents. How do I specify the new name, when again a Path is expected? Path p = /* path to /home/me/file123 */; Path name =…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
22
votes
2 answers

Difference between Files#delete(Path) and File#delete()

I'm using Windows-7 with java 7 update 6 and found this weird (at least to me) behavior - I have two files E:\delete1.txt and E:\delete2.txt both are read only files, when I try to delete file like following it gets deleted without any issues -…
Premraj
  • 7,802
  • 8
  • 45
  • 66
21
votes
2 answers

How to use NIO to write InputStream to File?

I am using following way to write InputStream to File: private void writeToFile(InputStream stream) throws IOException { String filePath = "C:\\Test.jpg"; FileChannel outChannel = new FileOutputStream(filePath).getChannel(); …
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
20
votes
3 answers

Why is Files.list() parallel stream performing so much slower than using Collection.parallelStream()?

The following code fragment is part of a method that gets a directory listing, calls an extract method on each file and serializes the resulting drug object to xml. try(Stream paths = Files.list(infoDir)) { paths .parallel() …
kliron
  • 4,383
  • 4
  • 31
  • 47
20
votes
1 answer

java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows

I am using Java 7 File API. I wrote a class that is working fine on Ubuntu creating directories perfectly, but when I run same code on Windows then it is throwing error: Exception in thread "main" java.lang.UnsupportedOperationException:…
mathlearner
  • 7,509
  • 31
  • 126
  • 189