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

How to overwrite file via java nio writer?

I try files writer as follows: String content = "Test File Content"; I used as like : Files.write(path, content.getBytes(), StandardOpenOption.CREATE); If file is not create, file is created and content is written. But If file available , the…
cguzel
  • 1,673
  • 2
  • 19
  • 34
59
votes
7 answers

Wrapping a ByteBuffer with an InputStream

I have a method that takes an InputStream and reads data from it. I would like to use this method with a ByteBuffer also. Is there a way to wrap a ByteBuffer so it can be accessed as a stream?
Erik
  • 743
  • 1
  • 5
  • 11
59
votes
6 answers

Java best practice: Class with only static methods

I have an application where I have a class called PlausibilityChecker. This class has only static methods, like checkZipcodeFormat or checkMailFormat. I use them in my GUI classes to check the input before sending it to the lower layer. Is this good…
user3629892
  • 2,960
  • 9
  • 33
  • 64
57
votes
2 answers

How to get the path string from a java.nio.Path?

Rewrote question with more info I have some code that creates a Path object using relative paths, like this: Paths.get("..", "folder").resolve("filename"). Later, I want to get the path string "..\folder\filename" from it (I'm on windows, so…
Jorn
  • 20,612
  • 18
  • 79
  • 126
57
votes
4 answers

What is the difference between Tomcat's BIO Connector and NIO Connector?

I would like to know the internals of the tomcat NIO connector. How exactly are threads used when we create a servlet that implements CometProcessor?Is it still one thread per connection? From what I read, the conversation goes like this Client…
user1456150
  • 579
  • 1
  • 5
  • 3
56
votes
8 answers

Java NIO file path issue

I used the following code to get the path Path errorFilePath = FileSystems.getDefault().getPath(errorFile); When I try to move a file using the File NIO, I get the error below: java.nio.file.InvalidPathException: Illegal char <:> at…
Kathir
  • 6,136
  • 8
  • 36
  • 67
54
votes
3 answers

Get file/directory size using Java 7 new IO

How can I get the size of a file or directory using the new NIO in java 7?
clamp
  • 33,000
  • 75
  • 203
  • 299
53
votes
2 answers

Is Java 7 WatchService Slow for Anyone Else?

WatchService looks like a great technology but its been too slow to be useful on the OS X and Linux systems I've tested on. To add insult to injury, it doesn't seem to get notified of all events either. This is the case both with my own code and…
sbook
  • 841
  • 1
  • 8
  • 13
53
votes
8 answers

java.nio.file.InvalidPathException: Illegal char <:> at index 2:

I have to copy classpath resource from one package to another. My program is: public static void main(String[] args) throws IOException, URISyntaxException { ClassLoader classLoader =…
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
50
votes
5 answers

What is the exact use of java nio package when already methods are available with io package

I was learning java nio package and I realized there are lots of methods already provided by File which nio.Files is providing again by using Path class. Like that few more I got. I am actually not getting what is the actual use of nio package. I am…
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
48
votes
5 answers

Best way to write String to file using java nio

I need to write(append) huge string to flat file using java nio. The encoding is ISO-8859-1. Currently we are writing as shown below. Is there any better way to do the same ? public void writeToFile(Long limit) throws IOException{ String…
nobody
  • 1,909
  • 5
  • 31
  • 45
48
votes
3 answers

What is a "regular file" in Java?

The class BasicFileAttributes, for examining the properties of a file in the file system, has the method isRegularFile(). Unfortunately, the Javadoc description is rather lacking: Tells whether the file is a regular file with opaque content. What…
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
44
votes
9 answers

Recursively list all files within a directory using nio.file.DirectoryStream;

I want to list all the FILES within the specified directory and subdirectories within that directory. No directories should be listed. My current code is below. It does not work properly as it only lists the files and directories within the…
Danny Rancher
  • 1,923
  • 3
  • 24
  • 43
44
votes
15 answers

Java 7 WatchService - Ignoring multiple occurrences of the same event

The javadoc for StandardWatchEventKinds.ENTRY_MODIFY says: Directory entry modified. When a directory is registered for this event then the WatchKey is queued when it is observed that an entry in the directory has been modified. The event…
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
40
votes
7 answers

Java thread per connection model vs NIO

Is the non-blocking Java NIO still slower than your standard thread per connection asynchronous socket? In addition, if you were to use threads per connection, would you just create new threads or would you use a very large thread pool? I'm writing…
Kevin Jin
  • 1,536
  • 4
  • 18
  • 20