Questions tagged [nio2]

NIO2 is an umbrella name for new features added to the original Java NIO package in the Java 1.7 release. Also referred to as "New I/O".

NIO.2 is an umbrella name for new features added to the original Java package in the Java 1.7 release. Also referred to as "New I/O".

  • Asynchronous Channels and Futures: A Channel that supports asynchronous I/O operations with implementations for sockets, server sockets and files.
  • Completion Handlers: The ability to register handlers that will be called on the completion of an asynchronous I/O event.
  • Asynchronous Channel Groups: A grouping of asynchronous channels for the purpose of resource sharing.
  • Asynchronous Multicast: Support for implementing asynchronous NIO I/O with callbacks for UDP multicast.
  • A New File System API for Java: Defines new interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.

Articles and Tutorials

Note: This tag and wiki should be rolled into the nio tag.

173 questions
2
votes
3 answers

Why java nio2 can`t listen a udp port?

Why "java.nio.channels.AsynchronousSocket" always listen tcp port? If it could, how can I use it? Which classes are they?
Smoke
  • 31
  • 3
2
votes
1 answer

Java NIO2 concurrent completion handlers

I am writing NIO2 server, and I need to make asynchronous read operations on an AsynchronousSocketChannel, each of these operations consists of reading an integer, and further reading from the same channel number of bytes equal to this integer.…
Z1kkurat
  • 45
  • 6
2
votes
3 answers

java.nio.file.FileSystemException: /proc: Too many open files

I'm using this code to read all folders in proc filesystem for (Path processPath : Files.newDirectoryStream(FileSystems.getDefault().getPath("/proc"), "[0-9]*")) { // Some logic } After some time I get this…
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
2
votes
4 answers

Is there a simple, NIO2 only way to convert from URL to Path

I want to copy a directory (including content) from a URL I get from Some.class.getClassLoader().getResource("folder"); which returns either jar:file:/D:/../some.jar!/someparent or file:/D:/../someparent I thought of a FileSystem (for jar intern…
Franz Ebner
  • 4,951
  • 3
  • 39
  • 57
2
votes
0 answers

Java 7 zipfs: File is also a folder

I've been using Java 7's ZipFS support. https://gist.github.com/stain/5591420 shows the behaviour, which I find a bit odd. Basically you can create a ZIP file system, make a file with a given name, and then also make a folder with the same name. The…
2
votes
1 answer

JDK 7 Watchkey pollEvents not capturing ENTRY_CREATE or ENTRY_MODIFY when large number of files are being copied

I am trying to watch all modifications that are being done to a specific folder. When I copy a folder including 2~5 subfolders with each having 2~3 files. Everything works great. The problem arises when I copy complicated folder structures. For…
Gyan
  • 1,176
  • 1
  • 12
  • 26
2
votes
1 answer

How to access a zip file inside a jar-archive using Java NIO 2

i'd like to access a zip file that is located inside of a jar archive. I thought that i should be possible with the capabilities of NIO 2, but it doesn't seem to work for me. Does somebody know how and if it is possible? I already read that it…
dajood
  • 3,758
  • 9
  • 46
  • 68
2
votes
1 answer

How to read and write integers with a SeekableByteChannel in little endian format?

I need to read and write signed and unsigned integers with a SeekableByteChannel in little endian format. This may seem silly, but I cannot find something in the JDK. Have I missed something or am I expected to roll this on my own? This would be no…
Christian Schlichtherle
  • 3,125
  • 1
  • 23
  • 47
1
vote
1 answer

Replace a file, if existing, or else create a new file, using NIO.2 in Java 7+

The Files.write & Files.writeString methods in Java 7+ are a concise handy way to write text to a file. try { Files.write ( Paths.get ( "/Users/My_UserName/Example.txt" ) , List.of( "Hello world" ,…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1
vote
1 answer

Non-blocking reading from the file using Reactor

Do you know if it's possible to create Flux of file lines really non-blocking way? The best solution I've found is next: Flux.using( () -> Files.lines(PATH), Flux::fromStream, Stream::close ); Even though it looks…
Ksu
  • 109
  • 2
  • 8
1
vote
0 answers

Can I open a zip filesystem in read-only mode?

I'm trying to unify code to list files whether they're in a directory or on the classpath. I can get a URL to the location of the resource: URI uri = FontManagerTestUtils.class.getResource("/fonts").toURI(); FileSystem fileSystem = …
Hakanai
  • 12,010
  • 10
  • 62
  • 132
1
vote
1 answer

What is the multi threading model in Java NIO 2 - (Proactor pattern)?

I am trying to build a simple Echo Service using Java NIO 2 (which is based on Proactor Pattern). In the simplest implementation, we have 4 main components; ProactorInitiator, AcceptConnectionHandler, ReadConnectionHandler and…
Pasindu Tennage
  • 1,480
  • 3
  • 14
  • 31
1
vote
1 answer

Fastest way to list files in Java

I have tens or even hundreds of thousands of files to list. I thought this would be a rather straightforward thing to do, as for example, running find -iname "*.abc" | wc -l runs instantly on my Ubuntu laptop. Unfortunately, the equivalent code in…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
1
vote
1 answer

Java NIO2 AsynchronousFileChannel returns Future with no documnetation on actual values

So I came up with the following function based on the docs and examples I found online, to write files in async way: public static Future createAndWriteToFile(String fullFileName, String content) throws IOException { Path file…
Carmageddon
  • 2,627
  • 4
  • 36
  • 56
1
vote
0 answers

How to check via static code analysis if java.nio.file is used

I am working on a Java library that is can be used both as a generic Java library and as an Android library. I want to avoid using java.nio.file.* classes because they were introduced in the Android platform just recently with API 26, while my…
Kaloyan Raev
  • 167
  • 2
  • 6