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
350
votes
4 answers

Create a Path from String in Java7

How can I create a java.nio.file.Path object from a String object in Java 7? I.e. String textPath = "c:/dir1/dir2/dir3"; Path path = ?; where ? is the missing code that uses textPath.
mat_boy
  • 12,998
  • 22
  • 72
  • 116
337
votes
30 answers

Recursively list files in Java

How do I recursively list all files under a directory in Java? Does the framework provide any utility? I saw a lot of hacky implementations. But none from the framework or nio
Quintin Par
  • 15,862
  • 27
  • 93
  • 146
238
votes
8 answers

Java: Path vs File

For new applications written in Java 7, is there any reason to use a java.io.File object any more or can we consider it deprecated? I believe a java.nio.file.Path can do everything a java.io.File can do and more.
dogbane
  • 266,786
  • 75
  • 396
  • 414
177
votes
7 answers

Java NIO FileChannel versus FileOutputstream performance / usefulness

I am trying to figure out if there is any difference in performance (or advantages) when we use nio FileChannel versus normal FileInputStream/FileOuputStream to read and write files to filesystem. I observed that on my machine both perform at the…
Keshav
  • 4,408
  • 8
  • 31
  • 50
165
votes
4 answers

ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()

To allocate() or to allocateDirect(), that is the question. For some years now I've just stuck to the thought that since DirectByteBuffers are a direct memory mapping at OS level, that it would perform quicker with get/put calls than…
user238033
115
votes
6 answers

Gets byte array from a ByteBuffer in java

Is this the recommended way to get the bytes from the ByteBuffer ByteBuffer bb =.. byte[] b = new byte[bb.remaining()] bb.get(b, 0, b.length);
kal
  • 28,545
  • 49
  • 129
  • 149
96
votes
3 answers

Java: Converting String to and from ByteBuffer and associated problems

I am using Java NIO for my socket connections, and my protocol is text based, so I need to be able to convert Strings to ByteBuffers before writing them to the SocketChannel, and convert the incoming ByteBuffers back to Strings. Currently, I am…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64
96
votes
4 answers

Java NIO: What does IOException: Broken pipe mean?

For some of my Java NIO connections, when I have a SocketChannel.write(ByteBuffer) call, it throws an IOException: "Broken pipe". What causes a "broken pipe", and, more importantly, is it possible to recover from that state? If it cannot be…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64
94
votes
3 answers

Java: what exactly is the difference between NIO and NIO.2?

I don't quite understand how different they are from each other so I have some inquiries regarding these two packages. After looking around a bit on Google, it seems like Oracle decided to update the NIO package with the newer and enhanced NIO.2…
John Huynh
  • 943
  • 1
  • 7
  • 5
78
votes
1 answer

Java 7 new IO API - Paths.exists

Does anyone know what happened to the path.exists() API method in the latest Java 7 API? I cannot find the change in the change logs, and between b123 and b130, the method has been removed from the API.I see that there is a static Files.exists…
Eugen
  • 8,523
  • 8
  • 52
  • 74
73
votes
2 answers

How to access a sub-file/folder in Java 7 java.nio.file.Path?

Java 7 introduced java.nio.file.Path as a possible replacement for java.io.File. With File, when I access a file under a specific, I would do: File parent = new File("c:\\tmp"); File child = new File(parent, "child"); // this accesses…
ripper234
  • 222,824
  • 274
  • 634
  • 905
71
votes
4 answers

Java access files in jar causes java.nio.file.FileSystemNotFoundException

While trying to copy some files in my jar file to a temp directory with my java app, the following exception is thrown: java.nio.file.FileSystemNotFoundException at…
tom91136
  • 8,662
  • 12
  • 58
  • 74
70
votes
2 answers

When to use ** (double star) in glob syntax within JAVA

Directly from this Java Oracle tutorial: Two asterisks, **, works like * but crosses directory boundaries. This syntax is generally used for matching complete paths. Could anybody do a real example out of it? What do they mean with "crosses…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
62
votes
6 answers

Java.nio: most concise recursive directory delete

I am currently trying to recursively delete a directory... Strangely enough the shortest piece of code I was able to find is the following construct, employing an ad-hoc inner class and in a visitor pattern... Path rootPath =…
fgysin
  • 11,329
  • 13
  • 61
  • 94
62
votes
3 answers

Alternative to File.deleteOnExit() in Java NIO?

Java IO has File.deleteOnExit(), which is a method that deletes the file it is called on during normal termination of the JVM. I've found this to be very useful for cleaning up temporary files, especially during unit tests. However, I don't see a…
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
1
2 3
99 100